Repository navigation
Stop the pure-Python MultiDict mishandling falsy and arg= keyword arguments - #1842
Conversation
The pure-Python implementation defaulted the positional argument of the constructors, extend(), update() and merge() to None and tested it by truth value, so None, 0, False and other false non-iterables were silently treated as if no argument had been passed. The C extension, like dict, raises TypeError for them. The truth test also rejected iterables whose __bool__() raises. This regressed in 6.2.0 with aio-libs#1046. Use the existing sentinel as the default and test identity against it, and drop None from MDArg, which only existed to type the old default.
The lock-pairing wrapper around the pure-Python constructors, extend(), update() and merge() did not make its first parameter positional-only, so MultiDict(arg=1) bound arg instead of adding a key named arg, and raised TypeError. The C extension adds the key. This regressed in 6.9.0 with aio-libs#1449.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1842 +/- ##
==========================================
+ Coverage 98.67% 98.68% +0.01%
==========================================
Files 51 51
Lines 10257 10352 +95
Branches 868 870 +2
==========================================
+ Hits 10121 10216 +95
Misses 132 132
Partials 4 4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
The CodeQL "Module-level cyclic import" error on The notice on |
Confidence Score: 5/5
[Low impact] The PR appears safe to merge; no new actionable issues were found. Reviews (2) · Last reviewed commit: "Update tests/test_update.py" · Reviewed by Greptile |
What do these changes do?
The pure-Python implementation (used on PyPy and with
MULTIDICT_NO_EXTENSIONS=1) decides whether the positional argument was passed by testing its truth value. So theMultiDict/CIMultiDictconstructors,extend(),update()andmerge()treat a false non-iterable (None,0,False,0.0,0j, or an object without__iter__whose__len__()returns 0) as if no argument had been given. The C extension, likedict, raisesTypeError: '<type>' object is not iterablefor all of these. Empty iterables and mappings such as[],()and{}are accepted by both implementations and are not affected. The same truth test makes the pure-Python version reject an iterable of pairs whose__bool__()raises (a NumPy array of pairs, for example), which the C extension accepts.This is a regression in 6.2.0. In 6.1.0 the pure-Python version raised
TypeErrorlike the C extension. #1046 changed the default toNoneand the check in_parse_args()toif arg:.This PR uses the module's existing
sentinelas the default, asgetall()/getone()/popone()already do, and testsarg is not sentinel. It also drops| NonefromMDArg, which only typed the old default. The C extension has never acceptedNonethere. If you would rather keepNoneaccepted, a one-lineif arg is not None:fixes every other case and the__bool__()case, and I can switch to that.A second commit fixes a related bug in the pure-Python implementation.
_locked_pair_always, the lock-pairing wrapper around these four methods, did not make its first parameter positional-only. SoMultiDict(arg=1),extend(arg=1),update(arg=1)andmerge(arg=1)boundarginstead of adding a key namedarg, and raisedTypeError. The C extension adds the key, as did the pure-Python implementation up to 6.8.0. This regressed in 6.9.0 with #1449. The wrapper now has a/afterarg.The new tests run over both implementations through the existing
any_multidict_classfixture. They cover the falsy non-iterables for each entry point, with and without keyword arguments, an iterable whose__bool__()raises, andargpassed as a keyword.Are there changes in behavior for the user?
Yes. With the pure-Python implementation, passing
None,0,Falseor another falsy non-iterable to these methods now raisesTypeError, as it already does with the C extension. An iterable whose__bool__()raises is now accepted, and anargkeyword argument becomes a key, as with the C extension. Nothing changes for C extension users.Is it a substantial burden for the maintainers to support this?
No. It is a small change to the existing argument parsing and brings the two implementations back in line.
Related issue number
None.
Checklist
CONTRIBUTORS.txt(N/A: noCONTRIBUTORS.txtin this repo)CHANGES/foldermake doc-spellingpasses and any new technical words are added todocs/spelling_wordlist.txt(not run locally: libenchant is not installed)Test runs
On
masterat 6f1f778 with this branch, CPython 3.13, macOS arm64:pytest tests -q --no-cov: 4422 passed, 184 skippedMULTIDICT_NO_EXTENSIONS=1 pytest tests -q --no-cov --no-c-extensions: 1771 passed, 24 skippedruff check,ruff format --checkandmypy(repo config) passDrafted with Claude Code (Claude Opus 5.5); reviewed by @provos.