2Tests handling of the LiteralString special form.
5# Specification: https://typing.readthedocs.io/en/latest/spec/literal.html#literalstring
20variable_annotation: LiteralString
23def my_function(literal_string: LiteralString) -> LiteralString:
24 raise NotImplementedError
28 my_attribute: LiteralString = ""
31type_argument: list[LiteralString]
33T = TypeVar("T", bound=LiteralString)
36bad_union: Literal["hello", LiteralString] # E
[invalid-type-form] Type arguments for `Literal` must be `None`, a literal value (int, bool, str, or bytes), or an enum member
37bad_nesting: Literal[LiteralString] # E
[invalid-type-form] Type arguments for `Literal` must be `None`, a literal value (int, bool, str, or bytes), or an enum member
40def func1(a: Literal["one"], b: Literal["two"]):
43 x2: Literal[""] = b # E
[invalid-assignment] Object of type `Literal["two"]` is not assignable to `Literal[""]`
46def func2(a: LiteralString, b: LiteralString):
47 # > Addition: x + y is of type LiteralString if both x and y are compatible with LiteralString.
48 assert_type(a + b, LiteralString)
50 # > Joining: sep.join(xs) is of type LiteralString if sep’s type is
51 # > compatible with LiteralString and xs’s type is compatible with Iterable[LiteralString].
52 assert_type(",".join((a, b)), LiteralString)
53 assert_type(",".join((a, str(b))), str)
Unexpected error
[type-assertion-failure] Type `str` does not match asserted type `LiteralString`
55 # > In-place addition: If s has type LiteralString and x has type compatible with
56 # > LiteralString, then s += x preserves s’s type as LiteralString.
60 # > String formatting: An f-string has type LiteralString if and only if its constituent
61 # > expressions are literal strings. s.format(...) has type LiteralString if and only if
62 # > s and the arguments have types compatible with LiteralString.
63 assert_type(f"{a} {b}", LiteralString)
Unexpected error
[type-assertion-failure] Type `LiteralString` does not match asserted type `str`
66 x1: LiteralString = f"{a} {str(variable)}" # E
[invalid-assignment] Object of type `str` is not assignable to `LiteralString`
68 assert_type(a + str(1), str)
Unexpected error
[type-assertion-failure] Type `str` does not match asserted type `LiteralString`
70 # > LiteralString is compatible with the type str
73 # > Other literal types, such as literal integers, are not compatible with LiteralString.
74 x3: LiteralString = 3 # E
[invalid-assignment] Object of type `Literal[3]` is not assignable to `LiteralString`
75 x4: LiteralString = b"test" # E
[invalid-assignment] Object of type `Literal[b"test"]` is not assignable to `LiteralString`
78# > Conditional statements and expressions work as expected.
79def condition1() -> bool:
80 raise NotImplementedError
83def return_literal_string() -> LiteralString:
84 return "foo" if condition1() else "bar" # OK
87def return_literal_str2(literal_string: LiteralString) -> LiteralString:
88 return "foo" if condition1() else literal_string # OK
91def return_literal_str3() -> LiteralString:
101# > TypeVars can be bound to LiteralString.
102TLiteral = TypeVar("TLiteral", bound=LiteralString)
105def literal_identity(s: TLiteral) -> TLiteral:
109def func3(s: Literal["hello"]):
110 y = literal_identity(s)
111 assert_type(y, Literal["hello"])
114def func4(s: LiteralString):
115 y2 = literal_identity(s)
116 assert_type(y2, LiteralString)
120 literal_identity(s) # E
[invalid-argument-type] Argument to function `literal_identity` is incorrect: Argument type `str` does not satisfy upper bound `LiteralString` of type variable `TLiteral`
123# > LiteralString can be used as a type argument for generic classes.
124class Container(Generic[T]):
125 def __init__(self, value: T) -> None:
129def func6(s: LiteralString):
130 x: Container[LiteralString] = Container(s) # OK
134 x_error: Container[LiteralString] = Container(s) # E
[invalid-argument-type] Argument to bound method `__init__` is incorrect: Argument type `str` does not satisfy upper bound `LiteralString` of type variable `T`
[invalid-argument-type] Argument to bound method `__init__` is incorrect: Expected `LiteralString`, found `str`
137# > Standard containers like List work as expected.
138xs: list[LiteralString] = ["foo", "bar", "baz"]
147def func8(x: Literal["foo"]) -> C:
152def func8(x: LiteralString) -> B:
157def func8(x: str) -> A:
161def func8(x: Any) -> Any:
165assert_type(func8("foo"), C) # First overload
166assert_type(func8("bar"), B) # Second overload
167assert_type(func8(str(1)), A) # Third overload
Unexpected error
[type-assertion-failure] Type `A` does not match asserted type `B`
170def func9(val: list[LiteralString]):
171 x1: list[str] = val # E
[invalid-assignment] Object of type `list[LiteralString]` is not assignable to `list[str]`
174def func10(val: Sequence[LiteralString]):
175 x1: Sequence[str] = val # OK