-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenums.py
52 lines (33 loc) · 1.47 KB
/
enums.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -----------------------------------------------------------------------------
# Enum
# -----------------------------------------------------------------------------
from enum import Enum
class Status(Enum):
READY = "ready"
GO = "go"
def func_enum(status: Status):
print(f"{status=}")
def test_enum(status: str):
if status in Status:
print("✅")
else:
print("❌")
func_enum(status=Status.READY)
# func_enum(status="ready") <-- Argument of type "Literal['ready']" cannot be assigned to parameter "status" of type "Status" in function "func_enum"
test_enum(status="READY") # ❌
test_enum(status="ready") # ✅
# test_enum(status=Status.READY) <-- Argument of type "Literal[Status.READY]" cannot be assigned to parameter "status" of type "str" in function "test_literal"
# -----------------------------------------------------------------------------
# Literal
# -----------------------------------------------------------------------------
from typing import Literal
STATUS = Literal["READY", "GO"]
def func_literal(status: STATUS):
print(f"{status=}")
# def test_literal(status: str):
# if status in STATUS: <-- Operator "in" not supported for types "str" and "STATUS"
# return True
# return False
func_literal("READY") # <-- autocompletion (like in TS)
func_literal("GO")
# func_literal("READDY") <-- Argument of type "Literal['READDY']" cannot be assigned to parameter "status" of type "STATUS" in function "func"