Skip to content

Passing enum directly as iterable loses type specificity #3210

Closed
python/typeshed
#1755
@FuegoFro

Description

@FuegoFro

It looks like if you pass an Enum class directly to a generic that wants an iterable (eg list, frozenset) it will forget which type of Enum you have. However if you make a no-op iterable out of it first, the enum type is preserved:

from enum import Enum

class Colors(Enum):
    RED = 0
    BLUE = 1

reveal_type(frozenset(c for c in Colors))  # <-- Is `frozenset[Colors*]`
reveal_type(frozenset(Colors))  # <-- Is `frozenset[Enum*]`

The last line here should have type frozenset[Colors*] rather than frozenset[Enum*]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions