-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
iter doesn't work on TypeVar with bound of Enum #12553
Comments
I think this is a repro that doesn't involve enums, revealing that the issue is a general one with mypy's understanding of metaclasses rather than an issue specific to the enum plugin: from typing import Iterator, TypeVar
M = TypeVar("M")
class Meta(type):
def __iter__(self: type[M]) -> Iterator[M]: ...
class Foo(metaclass=Meta): ...
F = TypeVar("F", bound=Foo)
def foo_iter(cls: type[F]):
return iter(cls) Mypy output:
|
It probably doesn't even have anything to do with from typing import Iterator, TypeVar
M = TypeVar("M")
class Meta(type):
def __iter__(self: type[M]) -> Iterator[M]: ...
class Foo(metaclass=Meta): ...
def foo_iter(cls: type[Foo]):
return iter(cls) # error: Argument 1 to "iter" has incompatible type "Type[Foo]"; expected "_SupportsIter[<nothing>]" |
I guess a good question is whether subclasses of Foo are expected to have a metaclass that is a subclass (in the Liskov substitution sense) of Meta. If not, then the error would be legitimate since |
Fixes #12553 This looks quite niche, but also it was mentioned recently couple times for a real-life use case: enum classes, and implementation looks simple.
Bug Report
A function that takes an argument of
Type[_E]
where_E = TypeVar("_E", bound=Enum)
does not accept callingiter
on that argument. Callingiter
on a concrete enum class is accepted, which is why I think this isn't purely a typeshed issue. Using the class in a loop or calling the__iter__
member is also accepted.To Reproduce
Run mypy on the following:
Expected Behavior
Expected no mypy errors.
Actual Behavior
What's even weirder is that adding an
Iterator[_E]
return annotation toenum_iter
makes it pass.Your Environment
mypy.ini
(and other config files): noneThe text was updated successfully, but these errors were encountered: