Skip to content

Fix frozendict constructor: drop the argument-less __init__ (#15985)#15989

Merged
hauntsaninja merged 2 commits into
python:mainfrom
apoorvdarshan:fix-15985-frozendict-init
Jul 8, 2026
Merged

Fix frozendict constructor: drop the argument-less __init__ (#15985)#15989
hauntsaninja merged 2 commits into
python:mainfrom
apoorvdarshan:fix-15985-frozendict-init

Conversation

@apoorvdarshan

Copy link
Copy Markdown
Contributor

Fixes #15985.

The new (3.15) frozendict builtin has __new__ overloads for keyword arguments, a mapping, and an iterable of pairs, but the stub also declared:

def __init__(self) -> None: ...

Because type checkers validate a constructor call against both __new__ and __init__, that argument-less __init__ made frozendict(a=1), frozendict({...}), and frozendict([...]) all fail with "Expected 0 positional arguments" / "Too many arguments".

Removing the redundant __init__ lets the __new__ overloads govern construction (matching other __new__-constructed builtins like tuple/frozenset, which don't declare a restrictive __init__).

Added check_frozendict-py315.py covering the keyword / mapping / iterable / mixed forms; it fails on main and passes with this change. tests/mypy_test.py stdlib/builtins.pyi succeeds.

Disclosure: prepared with AI assistance; reviewed and verified locally.

frozendict has __new__ overloads for keyword arguments, a mapping and an
iterable of pairs, but also declared 'def __init__(self) -> None: ...',
so type checkers rejected 'frozendict(a=1)' etc. with 'Expected 0
positional arguments'. Remove the redundant __init__ (like other
__new__-constructed builtins) so the __new__ overloads govern
construction. Added a test case.
@github-actions

This comment has been minimized.

from typing import Any
from typing_extensions import assert_type

# Regression test for gh-15985: ``frozendict`` can be constructed from keyword

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might need an if sys.version_info >= (3, 15): top level block

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — done. Wrapped the assertions in an if sys.version_info >= (3, 15): block (matching check_exception_group-py311.py), so the file also type-checks cleanly on earlier target versions instead of tripping on the undefined frozendict name.

@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@hauntsaninja hauntsaninja left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@hauntsaninja hauntsaninja merged commit f76037a into python:main Jul 8, 2026
90 of 91 checks passed
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.

New frozendict __init__ annotated improperly

2 participants