← 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
"""
2
Tests
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
7
from
enum
import
Enum
8
from
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
13
14
15
class
Color
(
Enum
):
16
RED
=
1
17
GREEN
=
2
18
BLUE
=
3
19
20
21
assert_type
(
Color
.
RED
.
_name_
,
Literal
[
"RED"
])
# E?
22
assert_type
(
Color
.
RED
.
name
,
Literal
[
"RED"
])
# E?
23
24
25
def
func1
(
red_or_blue
:
Literal
[
Color
.
RED
,
Color
.
BLUE
]):
26
assert_type
(
red_or_blue
.
name
,
Literal
[
"RED"
,
"BLUE"
])
# E?
27
28
29
def
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`