Skip to content

Enums: false-positive "No base classes allowed after ReprEnum" error #12787

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

Closed
AlexWaygood opened this issue May 14, 2022 · 1 comment · Fixed by #12963
Closed

Enums: false-positive "No base classes allowed after ReprEnum" error #12787

AlexWaygood opened this issue May 14, 2022 · 1 comment · Fixed by #12963
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code topic-enum topic-inheritance Inheritance and incompatible overrides

Comments

@AlexWaygood
Copy link
Member

AlexWaygood commented May 14, 2022

Bug Report

Minimal repro on 3.11 (playground link):

from enum import ReprEnum, Flag
class MyFlag(ReprEnum, Flag): ...

Minimal repro on <=3.10 (playground link):

from enum import Enum, Flag
class ReprEnum(Enum): ...
class MyFlag(ReprEnum, Flag): ...

mypy output for both (with small line-number differences):

main.py:4: error: No base classes are allowed after "__main__.ReprEnum"  [misc]

Expected Behavior

No error should be reported. This use of multiple inheritance works fine at runtime on 3.10 and 3.11. In fact, in 3.11, the source code for the enum module in the stdlib uses exactly this inheritance structure:

class ReprEnum(Enum):
    """
    Only changes the repr(), leaving str() and format() to the mixed-in type.
    """

# Much further down...

class IntFlag(int, ReprEnum, Flag, boundary=EJECT):
    """
    Support for integer-based Flags
    """

Your Environment

I can reproduce this bug on mypy 0.940+ (0.940 was the first released version with @sobolevn's #12026, which introduced this error message).

@AlexWaygood AlexWaygood added bug mypy got something wrong false-positive mypy gave an error on correct code topic-enum topic-inheritance Inheritance and incompatible overrides labels May 14, 2022
@AlexWaygood AlexWaygood changed the title Enums: false-positive "No base classes allowed" error Enums: false-positive "No base classes allowed after Enum" error May 14, 2022
@AlexWaygood AlexWaygood changed the title Enums: false-positive "No base classes allowed after Enum" error Enums: false-positive "No base classes allowed after ReprEnum" error May 14, 2022
@AlexWaygood
Copy link
Member Author

AlexWaygood commented May 14, 2022

An even simpler repro that doesn't involve enum.Flag:

from enum import Enum

class Foo(Enum): ...
class Bar(Enum): ...
class Baz(Foo, Bar): ...

This works fine at runtime, but mypy says:

main.py:5: error: No base classes are allowed after "__main__.Foo"

JukkaL pushed a commit that referenced this issue Jul 8, 2022
Fixes #12787.

Mypy currently emits a false-positive error for this snippet of code, even though it works fine at runtime (and this exact inheritance structure is used in enum.py in the stdlib):

```
from enum import Enum, Flag
class ReprEnum(Enum): ...
class MyFlag(ReprEnum, Flag): ...
```

This PR fixes that.
Gobot1234 pushed a commit to Gobot1234/mypy that referenced this issue Aug 12, 2022
Fixes python#12787.

Mypy currently emits a false-positive error for this snippet of code, even though it works fine at runtime (and this exact inheritance structure is used in enum.py in the stdlib):

```
from enum import Enum, Flag
class ReprEnum(Enum): ...
class MyFlag(ReprEnum, Flag): ...
```

This PR fixes that.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code topic-enum topic-inheritance Inheritance and incompatible overrides
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant