2Tests basic behaviors of of Enum classes.
5# Specification: https://typing.readthedocs.io/en/latest/spec/enums.html#enum-definition
8from typing import assert_type, Literal
10# > Enum classes are iterable and indexable, and they can be called with a
11# > value to look up the enum member with that value. Type checkers should
12# > support these behaviors
20 assert_type(color, Color)
22# > Unlike most Python classes, Calling an enum class does not invoke its
23# > constructor. Instead, the call performs a value-based lookup of an
26# 'Literal[Color.RED]' and 'Color' are both acceptable
27assert_type(Color["RED"], Color) # E[red]
28assert_type(Color["RED"], Literal[Color.RED]) # E[red]
Tag 'red'
[type-assertion-failure] Type `Literal[Color.RED]` does not match asserted type `Color`
30# 'Literal[Color.BLUE]' and 'Color' are both acceptable
31assert_type(Color(3), Color) # E[blue]
32assert_type(Color(3), Literal[Color.BLUE]) # E[blue]
Tag 'blue'
[type-assertion-failure] Type `Literal[Color.BLUE]` does not match asserted type `Color`
35# > An Enum class with one or more defined members cannot be subclassed.
37class EnumWithNoMembers(Enum):
40class Shape(EnumWithNoMembers): # OK (because no members are defined)
44class ExtendedShape(Shape): # E: Shape is implicitly final
[subclass-of-final-class] Class `ExtendedShape` cannot inherit from final class `Shape`