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

1.5.0 regression: register enum member typed as callable #15870

Closed
The-Compiler opened this issue Aug 14, 2023 · 4 comments · Fixed by python/typeshed#10577
Closed

1.5.0 regression: register enum member typed as callable #15870

The-Compiler opened this issue Aug 14, 2023 · 4 comments · Fixed by python/typeshed#10577
Labels
bug mypy got something wrong

Comments

@The-Compiler
Copy link
Contributor

Bug Report

When an enum has a register member, with mypy 1.5.0, mypy falsely considers it being of type Callable[[Type[_T]], Type[_T]] rather than the enum type.

Bisected to 21cc1c7:

To Reproduce

import enum

class E(enum.Enum):
    register = enum.auto()


def fun(a: E) -> None:
    pass

fun(E.register)

https://mypy-play.net/?mypy=latest&python=3.11&gist=6115e84d079dcb7d03fe44174ddf5fb1

Actual Behavior

test.py:10: error: Argument 1 to "fun" has incompatible type "Callable[[type[_T]], type[_T]]"; expected "E"  [arg-type]

I believe this happens somehow because in typeshed (and only there), EnumMeta inherits ABCMeta:

https://github.com/python/typeshed/blob/d82a8325faf35aa0c9d03d9e9d4a39b7fcb78f8e/stdlib/enum.pyi#L79-L84

and ABCMeta has a register method:

https://docs.python.org/3/library/abc.html#abc.ABCMeta.register

Your Environment

  • Mypy version used: mypy 1.5.0 (compiled: yes)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: Python 3.11.4
@AlexWaygood
Copy link
Member

AlexWaygood commented Aug 14, 2023

Fixed in python/typeshed#10577.

@svalentin FYI — this is another small typeshed tweak that fixes a regression in mypy 1.5.0 — it could possibly be worth cherry-picking to a 1.5 patch release if the PyPI overlords ever let us make another mypy release :-)

Having said that, idk how many people are defining enums with a register member — it seems unlikely to me that too many people will be hitting this regression!

@svalentin
Copy link
Collaborator

We should be starting 1.6 really soon since 1.5 was delayed quite a bit and there's already quite a number of changes to pick up. So it's probably best to just wait for 1.6.
If we want to pick this up for 1.5, we would need 1.5.2 as 1.5.1 is "cut" and just waiting for PyPI gods. (1.5 ended up being such an odd release)

@The-Compiler
Copy link
Contributor Author

The-Compiler commented Aug 15, 2023

Thanks for the quick fix!

Having said that, idk how many people are defining enums with a register member — it seems unlikely to me that too many people will be hitting this regression!

Agreed, especially given that it's recommended to use ALL_CAPS for enum members. I hit this by pure chance, by not following that convention for historical reasons, and by having a KeyMode.register (as in vim registers) enum member. 😅

@davidhalter
Copy link

Does anybody here know how Mypy resolves the metaclass conflict (in typeshed) for StrEnum?

StrEnum bases are:

  • str -> Sequence -> Collection -> Protocol, where Protocol is an ABCMeta
  • ReprEnum -> Enum, which is an EnumMeta

So AFAIK there's this conflict, which normally would raise Metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases. However Mypy seems to stay silent on this one.

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

Successfully merging a pull request may close this issue.

4 participants