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

Unpacking enum type generates an error #14782

Closed
eltoder opened this issue Feb 26, 2023 · 0 comments · Fixed by #14827
Closed

Unpacking enum type generates an error #14782

eltoder opened this issue Feb 26, 2023 · 0 comments · Fixed by #14827
Labels

Comments

@eltoder
Copy link

eltoder commented Feb 26, 2023

To Reproduce

from enum import Enum

class E(Enum):
    A = 1
    B = 2

A, B = E

Actual Behavior

This code generates an error:

$ mypy --strict proj/iter_enum.py
proj/iter_enum.py:7: error: "Type[E]" object is not iterable  [misc]

Expected Behavior

This should type check without errors. Note that mypy knows that E is actually iterable. For example,

A, B = list(E)

works fine and so does iterating over E in a for loop.

Your Environment

  • Mypy version used: mypy 1.1.0+dev.c23e831ab0e7ec827c38cc830d3ebd3f4c43cd75
  • Mypy command-line flags: with and without --strict
  • Python version used: 3.10.6
@eltoder eltoder added the bug mypy got something wrong label Feb 26, 2023
JukkaL pushed a commit that referenced this issue Mar 6, 2023
Fixes #14782

Currently, mypy issues a false positive if you try to unpack an enum
class:

```python
from enum import Enum

class E(Enum):
    A = 1
    B = 2

A, B = E  #  error: "Type[E]" object is not iterable  [misc]
```

This is because of a more general problem with class objects that have
`__iter__` defined on their metaclass. Mypy issues a false positive on
this code, where `Foo` is iterable by virtue of having `Meta` as its
metaclass:

```python
from typing import Iterator
class Meta(type):
    def __iter__(cls) -> Iterator[int]:
        yield from [1, 2, 3]

class Foo(metaclass=Meta): ...

a, b, c = Foo  # error: "Type[Foo]" object is not iterable  [misc]
reveal_type(a)  # error: Cannot determine type of "a"  [has-type]  # note: Revealed type is "Any"
```

This PR fixes the false positive with enums, and the more general false
positive with iterable class objects.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants