Skip to content

Ignore user-defined __init__/__new__ when calling an enum class - #22195

Open
SergiuLupaiescu wants to merge 4 commits into
python:masterfrom
SergiuLupaiescu:issue-16806
Open

SergiuLupaiescu wants to merge 4 commits into
python:masterfrom
SergiuLupaiescu:issue-16806

Conversation

@SergiuLupaiescu

@SergiuLupaiescu SergiuLupaiescu commented Oct 13, 2026 •

Copy link
Copy Markdown

Fixes part of #16806 (the type-checker problem reported in the comment NOT the stubtest part).

The bug

Calling an enum class looks up a member by value at runtime, through EnumMeta.__call__.
A user-defined __init__ or __new__ only runs while the members are being created. mypy didn't account for this and used the user's constructor signature for the call, so this valid code was rejected:

class Result(enum.StrEnum):
        PASS = enum.auto(), "green"
        def __new__(cls, value: str, color: str) -> Self: ...
        def __init__(self, value: str, color: str) -> None: ...

Result("pass")  # error: Missing positional argument "color"  [call-arg]

The proposed fix

The fix adds a small helper, enum_call_constructor, in mypy/typeops.py.
When an enum defines its own __init__ or __new__, type_object_type uses Enum.__new__(cls, value) for the call signature. Otherwise the existing logic runs unchanged.

Calling an enum class (Color(1)) performs a member lookup by value via EnumMeta.__call__. A user-defined __init__ or __new__ only runs when the members are created, so it should not affect the signature of the call. Previously mypy built the signature from the user's constructor and reported a [call-arg] error
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@SergiuLupaiescu
SergiuLupaiescu marked this pull request as ready for review October 14, 2026 08:52
@github-actions

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

scipy-stubs (https://github.com/scipy/scipy-stubs)
- tests/misc/test_submodules_empty.pyi:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

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