Skip to content

1.5.0 regression: register enum member typed as callable #15870

Closed
python/typeshed
#10577

Description

@The-Compiler

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

Activity

  1. added a commit that references this issue on Aug 14, 2023
  2. AlexWaygood commented on Aug 14, 2023

    @AlexWaygood
    Member

    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!

  3. svalentin commented on Aug 14, 2023

    @svalentin
    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)

  4. The-Compiler commented on Aug 15, 2023

    @The-Compiler
    ContributorAuthor

    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. 😅

  5. davidhalter commented on Aug 16, 2023

    @davidhalter

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions