Skip to content

🐛 fix: match the frozendict backport to the Python 3.15 built-in - #82

Merged
nstarman merged 1 commit into
GalacticDynamics:mainfrom
nstarman:fix/frozendict-backport-parity
Oct 10, 2026
Merged

nstarman merged 1 commit into
GalacticDynamics:mainfrom
nstarman:fix/frozendict-backport-parity

Conversation

@nstarman

Copy link
Copy Markdown
Contributor

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

Built-in Backport before
d.__init__(b=2) no-op replaced the contents (an immutability hole)
Subclass __init__(self, x) __new__ builds from the args first __init__ never set _data
frozendict(d) is d (exact type) True False
frozendict(a=1) | Plain(...) (Mapping without |) TypeError merged it
Plain(...) | frozendict(a=1) message names 'frozendict' named 'dict'
copy.copy(d) is d (exact type) True (3.15's copy treats it as atomic) False
Pickle protocol 0/1 TypeError: cannot pickle 'frozendict' object TypeError with a __slots__ message
__getnewargs__ / __reduce_ex__ present / (__newobj__, (cls, data), state, None, None) missing / slot state
hash(frozendict(a=[])) message unhashable type: 'list' on 3.15, cannot use 'tuple' as a set element (...)
frozendict(1, 2) message frozendict 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 frozendict and ImmutableMap). ImmutableMap behaves the same apart from the fixes above.

Not changed (documented in the module docstring)

  • The Mapping base class stays, so type checkers on 3.10–3.14 still see a Mapping. As a result the MRO, dir(), and the ABCMeta metaclass differ from the built-in, which is only registered as a Mapping.
  • .keys().mapping wraps the backing dict. Matching it would need custom view types, which would no longer be dict_keys.
  • Some messages name dict where the built-in says frozendict, e.g. for unhashable keys. The exception types match.
  • Cosmetic: __module__ and inspect.signature differ.
  • Intentional shortcut: the private _data slot can still be rebound, and a subclass's extra __slots__ aren't pickled. Both are marked ponytail:.

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 plain Mapping, the hash value and its error message, copy/deepcopy on the exact type and a subclass, __getnewargs__, and the refusal of protocols 0/1.

  • Python 3.10 and 3.13 (backport): 135 passed, 100% coverage.
  • Python 3.15.0 with jax 0.11.2 (built-in): 135 passed.
  • Hooks: prek run --all-files passes.

🤖 Generated with Claude Code

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

codecov Bot commented Oct 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@27442c0). Learn more about missing BASE report.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@nstarman nstarman added this to the v0.3.0 milestone Oct 10, 2026
@nstarman
nstarman merged commit 8a48b5e into GalacticDynamics:main Oct 10, 2026
18 checks passed
@nstarman
nstarman deleted the fix/frozendict-backport-parity branch October 10, 2026 23:33
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant