← Back to index

enums_member_names.py

True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 0
FP: 0
FN: 0
Optional: 1 / 4
1"""
2Tests that the type checker handles the `_name_` and `name` attributes correctly.
3"""
4
5# Specification: https://typing.readthedocs.io/en/latest/spec/enums.html#member-names
6
7from enum import Enum
8from typing import Literal, assert_type
9
10# > All enum member objects have an attribute _name_ that contains the member’s
11# > name. They also have a property name that returns the same name. Type
12# > checkers may infer a literal type for the name of a member
15class Color(Enum):
16 RED = 1
17 GREEN = 2
18 BLUE = 3
21assert_type(Color.RED._name_, Literal["RED"]) # E?
22assert_type(Color.RED.name, Literal["RED"]) # E?
25def func1(red_or_blue: Literal[Color.RED, Color.BLUE]):
26 assert_type(red_or_blue.name, Literal["RED", "BLUE"]) # E?
29def func2(any_color: Color):
30 assert_type(any_color.name, Literal["RED", "BLUE", "GREEN"]) # E?
[type-assertion-failure] Type `Literal["RED", "BLUE", "GREEN"]` does not match asserted type `Any`