← Back to index

literals_parameterizations.py

True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 17
FP: 0
FN: 0
Optional: 0 / 0
1"""
2Tests legal and illegal parameterizations of Literal.
3"""
4
5# > Literal must be parameterized with at least one type.
6
7from typing import Any, Literal, TypeVar
8from enum import Enum
9
11class Color(Enum):
12 RED = 0
13 GREEN = 1
14 BLUE = 2
17good1: Literal[26]
18good2: Literal[0x1A]
19good3: Literal[-4]
20good4: Literal[+5]
21good5: Literal["hello world"]
22good6: Literal[b"hello world"]
23good7: Literal["hello world"]
24good8: Literal[True]
25good9: Literal[Color.RED]
26good10: Literal[None]
28ReadOnlyMode = Literal["r", "r+"]
29WriteAndTruncateMode = Literal["w", "w+", "wt", "w+t"]
30WriteNoTruncateMode = Literal["r+", "r+t"]
31AppendMode = Literal["a", "a+", "at", "a+t"]
33AllModes = Literal[ReadOnlyMode, WriteAndTruncateMode, WriteNoTruncateMode, AppendMode]
35good11: Literal[Literal[Literal[1, 2, 3], "foo"], 5, None]
37variable = 3
38T = TypeVar("T")
40# > Arbitrary expressions [are illegal]
41bad1: Literal[3 + 4] # E
[invalid-type-form] Type arguments for `Literal` must be `None`, a literal value (int, bool, str, or bytes), or an enum member
42bad2: Literal["foo".replace("o", "b")] # E
[invalid-type-form] Type arguments for `Literal` must be `None`, a literal value (int, bool, str, or bytes), or an enum member
43bad3: Literal[4 + 3j] # E
[invalid-type-form] Type arguments for `Literal` must be `None`, a literal value (int, bool, str, or bytes), or an enum member
44bad4: Literal[~5] # E
[invalid-type-form] Type arguments for `Literal` must be `None`, a literal value (int, bool, str, or bytes), or an enum member
45bad5: Literal[not False] # E
[invalid-type-form] Type arguments for `Literal` must be `None`, a literal value (int, bool, str, or bytes), or an enum member
46bad6: Literal[(1, "foo", "bar")] # E
[invalid-type-form] Type arguments for `Literal` must be `None`, a literal value (int, bool, str, or bytes), or an enum member
47bad7: Literal[{"a": "b", "c": "d"}] # E
[invalid-type-form] Type arguments for `Literal` must be `None`, a literal value (int, bool, str, or bytes), or an enum member
48bad8: Literal[int] # E
[invalid-type-form] Type arguments for `Literal` must be `None`, a literal value (int, bool, str, or bytes), or an enum member
49bad9: Literal[variable] # E
[invalid-type-form] Type arguments for `Literal` must be `None`, a literal value (int, bool, str, or bytes), or an enum member
50bad10: Literal[T] # E
[invalid-type-form] Type arguments for `Literal` must be `None`, a literal value (int, bool, str, or bytes), or an enum member
51bad11: Literal[3.14] # E
[invalid-type-form] Type arguments for `Literal` must be `None`, a literal value (int, bool, str, or bytes), or an enum member
52bad12: Literal[Any] # E
[invalid-type-form] Type arguments for `Literal` must be `None`, a literal value (int, bool, str, or bytes), or an enum member
53bad13: Literal[...] # E
[invalid-type-form] Type arguments for `Literal` must be `None`, a literal value (int, bool, str, or bytes), or an enum member
56def my_function(x: Literal[1 + 2]) -> int: # E
[invalid-type-form] Type arguments for `Literal` must be `None`, a literal value (int, bool, str, or bytes), or an enum member
57 return x * 3
60x: Literal # E
[invalid-type-form] `typing.Literal` requires at least one argument when used in a type expression
61y: Literal[my_function] = my_function # E
[invalid-type-form] Type arguments for `Literal` must be `None`, a literal value (int, bool, str, or bytes), or an enum member
64def func2(a: Literal[Color.RED]):
65 x1: Literal["Color.RED"] = a # E
[invalid-assignment] Object of type `Literal[Color.RED]` is not assignable to `Literal["Color.RED"]`
67 x2: "Literal[Color.RED]" = a # OK