Skip to content

Cannot use Literal variable inside Literal type #8187

Closed
@intgr

Description

@intgr

Using mypy 0.761, Python 3.7.5 here. The ANIMALS2 value is highlighted as incorrect by Mypy:

COW: Literal['cow'] = 'cow'
PIG: Literal['pig'] = 'pig'
ANIMALS1: Literal['cow', 'pig']  # This is OK
ANIMALS2: Literal[COW, PIG]
#         ^^^^^^^ Mypy: Parameter 1 of Literal[...] is invalid

But it seems Mypy should be able to deduce that ANIMALS1 and ANIMALS2 are the same thing?

I'm not sure if the more lax version should be allowed as well, but seems useful:

OX = 'ox'
ANIMALS = Literal[OX]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions