Skip to content
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

Not detecting enum.auto vs enum.auto() #17650

Open
jamesbraza opened this issue Aug 7, 2024 · 1 comment
Open

Not detecting enum.auto vs enum.auto() #17650

jamesbraza opened this issue Aug 7, 2024 · 1 comment
Labels
bug mypy got something wrong topic-enum

Comments

@jamesbraza
Copy link
Contributor

Bug Report

mypy is not detecting forgetting to call enum.auto() in IntEnum and StrEnum.

To Reproduce

from enum import IntEnum, StrEnum, auto

class SomeIntEnum(IntEnum):
    CORRECT_AUTO = auto()
    INCORRECT_AUTO = auto

reveal_type(SomeIntEnum.CORRECT_AUTO.value)
reveal_type(SomeIntEnum.INCORRECT_AUTO.value)

class SomeStrEnum(StrEnum):
    CORRECT_AUTO = auto()
    INCORRECT_AUTO = auto

reveal_type(SomeStrEnum.CORRECT_AUTO.value)
reveal_type(SomeStrEnum.INCORRECT_AUTO.value)

Running this code will actually crash with a TypeError, but mypy will pass:

  • For the IntEnum.INCORRECT_AUTO: TypeError: int() argument must be a string, a bytes-like object or a real number, not 'type'
  • For the StrEnum.INCORRECT_AUTO: TypeError: <class 'enum.auto'> is not a string

Expected Behavior

I expect mypy to error when one forgets to call auto, as:

  1. The code doesn't run
  2. Even if it did run, the enum's value wouldn't be a primitive as intended

Actual Behavior

mypy passes with the below output:

a.py:7:13: note: Revealed type is "builtins.int"
a.py:8:13: note: Revealed type is "def () -> enum.auto"
a.py:14:13: note: Revealed type is "builtins.str"
a.py:15:13: note: Revealed type is "def () -> enum.auto"

Your Environment

  • Mypy version used: 1.11.1
  • Mypy command-line flags: n/a
  • Mypy configuration options from mypy.ini (and other config files): n/a
  • Python version used: 3.12.4
@jamesbraza jamesbraza added the bug mypy got something wrong label Aug 7, 2024
@jamesbraza
Copy link
Contributor Author

The issue may be related to typeshed currently typing auto with Any: https://github.com/python/typeshed/blob/8a7f09e3511f3a1d04281c60167b8dcc3b78938b/stdlib/enum.pyi#L323

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-enum
Projects
None yet
Development

No branches or pull requests

2 participants