Skip to content

Indexing a Type Alias of an Enum yields false error #7568

Open
@brianjyoung

Description

@brianjyoung

Minimum [not-]working product:

Ah, good old Enums:

from enum import Enum
class EnumClass(Enum):
    one = 1

EnumAlias = EnumClass
EnumAlias['one']
test.py:6: error: Type application targets a non-generic function or class
test.py:6: error: Name 'one' is not defined

I'm fairly certain this is not an intentional design choice. Just to be sure, according to documentation:

A type alias does not create a new type. It’s just a shorthand notation for another type – it’s equivalent to the target type except for generic aliases.

Now, Enums actually can't be generic, so I certainly expect the alias to be equivalent. So I think this code should pass (regardless of its usefulness... who wants to alias an Enum anyways?).

Possible Causes

I'm no expert, but I think this is happening because the semantic analyzer deems any IndexExpr a TypeApplication if it has a TypeAlias as a base. Once it's marked as analyzed, it doesn't reach the stage of the expression checker where the Enum type would be recognized and the indexing processed as valid. Whatever the root of the problem, I think there's only one spot where the expression checker actually looks at is_enum to handle Enum indexing, so the alias isn't making it there one way or another.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions