Repository navigation
🐛 fix: match the frozendict backport to the Python 3.15 built-in - #82
Merged
nstarman merged 1 commit intoOct 10, 2026
Merged
Conversation
A differential probe against CPython 3.15.0's frozendict found real behavioural gaps in the backport: - `d.__init__(...)` could replace the contents of an existing frozendict. Construction now happens in `__new__`, as for the built-in. - `frozendict(d) is d` for an exact frozendict, as for the built-in. - `|` merged any Mapping; the built-in merges only dict/frozendict. - `copy.copy` of an exact frozendict returns it; subclasses copy via pickle. - Pickling goes through `__getnewargs__`/`__newobj__`; protocols 0-1 are refused. - The hash error message and the too-many-arguments message match. The remaining differences are documented in the module docstring. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #82 +/- ##
========================================
Coverage ? 100.00%
========================================
Files ? 3
Lines ? 98
Branches ? 0
========================================
Hits ? 98
Misses ? 0
Partials ? 0 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This was referenced Oct 10, 2026
nstarman
added a commit
that referenced
this pull request
Oct 10, 2026
Since #82, frozendict | other merges only a dict or frozendict, matching the Python 3.15 built-in. ImmutableMap.__or__ intentionally stays looser and merges any Mapping. Document that in the docstring and README, and pin it (and that the reflected | is not loosened) with a test. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Brings
xmmutablemap._frozendict.frozendict(the backport used on Python < 3.15) in line with the PEP 814 built-in.How this was checked
I wrote a differential probe that loads the backport from its file on CPython 3.15.0 and runs ~145 operations against both it and
builtins.frozendict, including subclasses. It covers construction,==,|,|=, hashing, repr (including recursive), views,get/in/[]with bad keys, pickling at every protocol,copy/deepcopy,match, generic aliases, and introspection. Before this PR: 25 differences. After it, the only differences left are the ones listed under "Not changed" below.Behavioural fixes
d.__init__(b=2)__init__(self, x)__new__builds from the args first__init__never set_datafrozendict(d) is d(exact type)TrueFalsefrozendict(a=1) | Plain(...)(Mapping without|)TypeErrorPlain(...) | frozendict(a=1)message'frozendict''dict'copy.copy(d) is d(exact type)True(3.15'scopytreats it as atomic)FalseTypeError: cannot pickle 'frozendict' objectTypeErrorwith a__slots__message__getnewargs__/__reduce_ex__(__newobj__, (cls, data), state, None, None)hash(frozendict(a=[]))messageunhashable type: 'list'cannot use 'tuple' as a set element (...)frozendict(1, 2)messagefrozendict expected at most 1 argument…dict expected…The probe also showed that the built-in's hash is
hash(frozenset(items)), so the docstring no longer claims the hash values differ.Compatibility: pickles written by the current release still load (checked with protocol 4 for both
frozendictandImmutableMap).ImmutableMapbehaves the same apart from the fixes above.Not changed (documented in the module docstring)
Mappingbase class stays, so type checkers on 3.10–3.14 still see aMapping. As a result the MRO,dir(), and the ABCMeta metaclass differ from the built-in, which is only registered as aMapping..keys().mappingwraps the backingdict. Matching it would need custom view types, which would no longer bedict_keys.dictwhere the built-in saysfrozendict, e.g. for unhashable keys. The exception types match.__module__andinspect.signaturediffer._dataslot can still be rebound, and a subclass's extra__slots__aren't pickled. Both are markedponytail:.Tests
I added parity tests to
TestFrozenDict. That class runs against the backport on 3.10–3.14 and against the built-in on 3.15+, so every expected value comes from the built-in. They cover__init__immutability, the identity constructor, subclass__init__,|rejecting a plainMapping, the hash value and its error message,copy/deepcopyon the exact type and a subclass,__getnewargs__, and the refusal of protocols 0/1.prek run --all-filespasses.🤖 Generated with Claude Code