Skip to content

"Enum" has no attribute "__iter__" on instance attributes defined with functional API #8009

@lihu-zhong

Description

@lihu-zhong

Steps to reproduce
Run mypy against the following code:

from enum import Enum


class OuterEnum(Enum):
    foo = "bar"


class Foo:
    def __init__(self,) -> None:
        self.OuterEnum = OuterEnum
        list(self.OuterEnum)
        self.InnerEnum = Enum("InnerEnum", [("foo", "bar")])
        list(self.InnerEnum)  # Error!
        [i for i in self.InnerEnum]  # Error!
        self.DictEnum = Enum("DictEnum", {"foo": "bar"})
        list(self.DictEnum)  # Error!


OtherEnum = Enum("OtherEnum", [("foo", "bar")])
list(OtherEnum)

Observed behavior
When trying to iterate over an enumeration defined as an instance attribute using the functional API, mypy raises an error when attempting to iterate over said attribute.

The above snippet raises three mypy errors where marked:

$ poetry run mypy foo.py 
foo.py:13: error: No overload variant of "list" matches argument type "Enum"
foo.py:13: note: Possible overload variant:
foo.py:13: note:     def [_T] __init__(self, iterable: Iterable[_T]) -> List[_T]
foo.py:13: note:     <1 more non-matching overload not shown>
foo.py:14: error: "Enum" has no attribute "__iter__"; maybe "__str__" or "__dir__"? (not iterable)
foo.py:16: error: No overload variant of "list" matches argument type "Enum"
foo.py:16: note: Possible overload variant:
foo.py:16: note:     def [_T] __init__(self, iterable: Iterable[_T]) -> List[_T]
foo.py:16: note:     <1 more non-matching overload not shown>
Found 3 errors in 1 file (checked 1 source file)

Expected behavior
When testing the above code, iteration over the instance attribute Enum works fine, and mypy does not raise errors when iterating over similar Enums if they're defined using the class syntax or aren't instance attributes. I would not expect mypy to raise an error here.

Environment

$ poetry run mypy --version
mypy 0.740
$ poetry run python --version
Python 3.7.4

Also tested against master with the same behavior

$ poetry run mypy --version
mypy 0.750+dev.2af0ac021335ec4a3e84a0ba739b4aea7e659c43

Thank you for your work on this tool!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy