Skip to content

Fails to flag None passed to constructor of enum.Enum-based classes #19094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
cstigen opened this issue May 16, 2025 · 1 comment
Open

Fails to flag None passed to constructor of enum.Enum-based classes #19094

cstigen opened this issue May 16, 2025 · 1 comment

Comments

@cstigen
Copy link

cstigen commented May 16, 2025

Bug Report

Mypy fails to flag None passed to constructor of enum.Enum-based classes.

To Reproduce

import enum


class Foo(enum.Enum):
    one = 1


Foo(None)

Note that changing to class Foo(int, enum.Enum) does make mypy flag the error. However, the two aren't equivalent as the latter lets you do Foo.one + 1 while the former requires Foo.one.value + 1.

Edit: In fact, in both forms it fails to flag invalid arguments such as Foo(2). I do realize this is a slightly different case, which is why I'm focusing on None which has different behavior between the two.

Expected Behavior

I would expect mypy to flag the last line as an error:

error: No overload variant of "Foo" matches argument type "None"  [call-overload]

Actual Behavior

Mypy reports no errors.

Your Environment

Tested on

  • Mypy 1.13.0 and 1.15.0
  • CPython 3.11.10

Used mypy --strict file.py and just mypy file.py.

Contents of mypy.ini (I actually use a pyproject.toml file, but have tested with only this ini-file):

[mypy]
check_untyped_defs = true
disallow_any_generics = false
disallow_incomplete_defs = false
disallow_subclassing_any = false
disallow_untyped_calls = false
disallow_untyped_decorators = true
disallow_untyped_defs = false
enable_error_code = truthy-bool, truthy-iterable
extra_checks = true
ignore_missing_imports = false
local_partial_types = true
no_implicit_optional = true
no_implicit_reexport = false
no_namespace_packages = true
python_version = 3.11
strict_equality = true
warn_redundant_casts = true
warn_return_any = false
warn_unused_configs = true
warn_unused_ignores = true
@cstigen cstigen added the bug mypy got something wrong label May 16, 2025
@cstigen cstigen changed the title Fails to flag None being passed to enum.Enum constructor Fails to flag None passed to constructor of enum.Enum-based classes. May 16, 2025
@cstigen cstigen changed the title Fails to flag None passed to constructor of enum.Enum-based classes. Fails to flag None passed to constructor of enum.Enum-based classes May 16, 2025
@sterliakov
Copy link
Collaborator

Okay, we just don't check enum constructors, this is not about None but about any incompatible type. Here's the relevant typeshed bit:

# It's not true that `__new__` will accept any argument type,
# so ideally we'd use `Any` to indicate that the argument type is inexpressible.
# However, using `Any` causes too many false-positives for those using mypy's `--disallow-any-expr`
# (see #7752, #2539, mypy/#5788),
# and in practice using `object` here has the same effect as using `Any`.
def __new__(cls, value: object) -> Self: ...

I agree this is a nice feature proposal, mypy should be able to refine enum's __new__ to a more specific signature. Maybe even accepting a union of literals in simple cases when enum values are numbers/strings (which are also the most popular, enums with complicated types are much more rare).

@sterliakov sterliakov added feature and removed bug mypy got something wrong labels May 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants