| 1 |
-- |
| 2 |
-- Check to see how we handle raw types, error handling, and other |
| 3 |
-- semantic analysis shenanigans |
| 4 |
-- |
| 5 |
|
| 6 |
[case testLiteralInvalidString] |
| 7 |
from typing import Literal |
| 8 |
def f1(x: 'A[') -> None: pass # E: Invalid type comment or annotation |
| 9 |
def g1(x: Literal['A[']) -> None: pass |
| 10 |
reveal_type(f1) # N: Revealed type is "def (x: Any)" |
| 11 |
reveal_type(g1) # N: Revealed type is "def (x: Literal['A['])" |
| 12 |
|
| 13 |
def f2(x: 'A B') -> None: pass # E: Invalid type comment or annotation |
| 14 |
def g2(x: Literal['A B']) -> None: pass |
| 15 |
def h2(x: 'A|int') -> None: pass # E: Name "A" is not defined |
| 16 |
def i2(x: Literal['A|B']) -> None: pass |
| 17 |
reveal_type(f2) # N: Revealed type is "def (x: Any)" |
| 18 |
reveal_type(g2) # N: Revealed type is "def (x: Literal['A B'])" |
| 19 |
reveal_type(h2) # N: Revealed type is "def (x: Any | builtins.int)" |
| 20 |
reveal_type(i2) # N: Revealed type is "def (x: Literal['A|B'])" |
| 21 |
[builtins fixtures/tuple.pyi] |
| 22 |
[out] |
| 23 |
|
| 24 |
[case testLiteralInvalidTypeComment] |
| 25 |
from typing import Literal |
| 26 |
def f(x): # E: Syntax error in type comment "(A[) -> None" |
| 27 |
# type: (A[) -> None |
| 28 |
pass |
| 29 |
|
| 30 |
[case testLiteralInvalidTypeComment2] |
| 31 |
from typing import Literal |
| 32 |
def f(x): # E: Invalid type comment or annotation |
| 33 |
# type: ("A[") -> None |
| 34 |
pass |
| 35 |
|
| 36 |
def g(x): |
| 37 |
# type: (Literal["A["]) -> None |
| 38 |
pass |
| 39 |
|
| 40 |
reveal_type(f) # N: Revealed type is "def (x: Any)" |
| 41 |
reveal_type(g) # N: Revealed type is "def (x: Literal['A['])" |
| 42 |
[builtins fixtures/tuple.pyi] |
| 43 |
[out] |
| 44 |
|
| 45 |
[case testLiteralFromTypingWorks] |
| 46 |
from typing import Literal |
| 47 |
|
| 48 |
x: Literal[42] |
| 49 |
x = 43 # E: Incompatible types in assignment (expression has type "Literal[43]", variable has type "Literal[42]") |
| 50 |
|
| 51 |
y: Literal[43] |
| 52 |
y = 43 |
| 53 |
[typing fixtures/typing-medium.pyi] |
| 54 |
|
| 55 |
[case testLiteralFromTypingExtensionsWorks] |
| 56 |
from typing_extensions import Literal |
| 57 |
|
| 58 |
x: Literal[42] |
| 59 |
x = 43 # E: Incompatible types in assignment (expression has type "Literal[43]", variable has type "Literal[42]") |
| 60 |
|
| 61 |
y: Literal[43] |
| 62 |
y = 43 |
| 63 |
[builtins fixtures/tuple.pyi] |
| 64 |
|
| 65 |
[case testLiteralInsideOtherTypes] |
| 66 |
from typing import Literal, Tuple |
| 67 |
|
| 68 |
x: Tuple[1] # E: Invalid type: try using Literal[1] instead? |
| 69 |
def foo(x: Tuple[1]) -> None: ... # E: Invalid type: try using Literal[1] instead? |
| 70 |
|
| 71 |
y: Tuple[Literal[2]] |
| 72 |
def bar(x: Tuple[Literal[2]]) -> None: ... |
| 73 |
reveal_type(x) # N: Revealed type is "tuple[Any]" |
| 74 |
reveal_type(y) # N: Revealed type is "tuple[Literal[2]]" |
| 75 |
reveal_type(bar) # N: Revealed type is "def (x: tuple[Literal[2]])" |
| 76 |
[builtins fixtures/tuple.pyi] |
| 77 |
[out] |
| 78 |
|
| 79 |
[case testLiteralInsideOtherTypesTypeCommentsPython3] |
| 80 |
from typing import Literal, Tuple, Optional |
| 81 |
|
| 82 |
x = None # type: Optional[Tuple[1]] # E: Invalid type: try using Literal[1] instead? |
| 83 |
def foo(x): # E: Invalid type: try using Literal[1] instead? |
| 84 |
# type: (Tuple[1]) -> None |
| 85 |
pass |
| 86 |
|
| 87 |
y = None # type: Optional[Tuple[Literal[2]]] |
| 88 |
def bar(x): |
| 89 |
# type: (Tuple[Literal[2]]) -> None |
| 90 |
pass |
| 91 |
reveal_type(x) # N: Revealed type is "tuple[Any] | None" |
| 92 |
reveal_type(y) # N: Revealed type is "tuple[Literal[2]] | None" |
| 93 |
reveal_type(bar) # N: Revealed type is "def (x: tuple[Literal[2]])" |
| 94 |
[builtins fixtures/tuple.pyi] |
| 95 |
[out] |
| 96 |
|
| 97 |
[case testLiteralValidExpressionsInStringsPython3] |
| 98 |
from wrapper import * |
| 99 |
|
| 100 |
[file wrapper.pyi] |
| 101 |
from typing import Literal |
| 102 |
|
| 103 |
alias_1 = Literal['a+b'] |
| 104 |
alias_2 = Literal['1+2'] |
| 105 |
alias_3 = Literal['3'] |
| 106 |
alias_4 = Literal['True'] |
| 107 |
alias_5 = Literal['None'] |
| 108 |
alias_6 = Literal['"foo"'] |
| 109 |
expr_of_alias_1: alias_1 |
| 110 |
expr_of_alias_2: alias_2 |
| 111 |
expr_of_alias_3: alias_3 |
| 112 |
expr_of_alias_4: alias_4 |
| 113 |
expr_of_alias_5: alias_5 |
| 114 |
expr_of_alias_6: alias_6 |
| 115 |
reveal_type(expr_of_alias_1) # N: Revealed type is "Literal['a+b']" |
| 116 |
reveal_type(expr_of_alias_2) # N: Revealed type is "Literal['1+2']" |
| 117 |
reveal_type(expr_of_alias_3) # N: Revealed type is "Literal['3']" |
| 118 |
reveal_type(expr_of_alias_4) # N: Revealed type is "Literal['True']" |
| 119 |
reveal_type(expr_of_alias_5) # N: Revealed type is "Literal['None']" |
| 120 |
reveal_type(expr_of_alias_6) # N: Revealed type is "Literal['"foo"']" |
| 121 |
|
| 122 |
expr_ann_1: Literal['a+b'] |
| 123 |
expr_ann_2: Literal['1+2'] |
| 124 |
expr_ann_3: Literal['3'] |
| 125 |
expr_ann_4: Literal['True'] |
| 126 |
expr_ann_5: Literal['None'] |
| 127 |
expr_ann_6: Literal['"foo"'] |
| 128 |
reveal_type(expr_ann_1) # N: Revealed type is "Literal['a+b']" |
| 129 |
reveal_type(expr_ann_2) # N: Revealed type is "Literal['1+2']" |
| 130 |
reveal_type(expr_ann_3) # N: Revealed type is "Literal['3']" |
| 131 |
reveal_type(expr_ann_4) # N: Revealed type is "Literal['True']" |
| 132 |
reveal_type(expr_ann_5) # N: Revealed type is "Literal['None']" |
| 133 |
reveal_type(expr_ann_6) # N: Revealed type is "Literal['"foo"']" |
| 134 |
|
| 135 |
expr_str_1: "Literal['a+b']" |
| 136 |
expr_str_2: "Literal['1+2']" |
| 137 |
expr_str_3: "Literal['3']" |
| 138 |
expr_str_4: "Literal['True']" |
| 139 |
expr_str_5: "Literal['None']" |
| 140 |
expr_str_6: "Literal['\"foo\"']" |
| 141 |
reveal_type(expr_str_1) # N: Revealed type is "Literal['a+b']" |
| 142 |
reveal_type(expr_str_2) # N: Revealed type is "Literal['1+2']" |
| 143 |
reveal_type(expr_str_3) # N: Revealed type is "Literal['3']" |
| 144 |
reveal_type(expr_str_4) # N: Revealed type is "Literal['True']" |
| 145 |
reveal_type(expr_str_5) # N: Revealed type is "Literal['None']" |
| 146 |
reveal_type(expr_str_6) # N: Revealed type is "Literal['"foo"']" |
| 147 |
|
| 148 |
expr_com_1 = ... # type: Literal['a+b'] |
| 149 |
expr_com_2 = ... # type: Literal['1+2'] |
| 150 |
expr_com_3 = ... # type: Literal['3'] |
| 151 |
expr_com_4 = ... # type: Literal['True'] |
| 152 |
expr_com_5 = ... # type: Literal['None'] |
| 153 |
expr_com_6 = ... # type: Literal['"foo"'] |
| 154 |
reveal_type(expr_com_1) # N: Revealed type is "Literal['a+b']" |
| 155 |
reveal_type(expr_com_2) # N: Revealed type is "Literal['1+2']" |
| 156 |
reveal_type(expr_com_3) # N: Revealed type is "Literal['3']" |
| 157 |
reveal_type(expr_com_4) # N: Revealed type is "Literal['True']" |
| 158 |
reveal_type(expr_com_5) # N: Revealed type is "Literal['None']" |
| 159 |
reveal_type(expr_com_6) # N: Revealed type is "Literal['"foo"']" |
| 160 |
[builtins fixtures/bool.pyi] |
| 161 |
[out] |
| 162 |
|
| 163 |
[case testLiteralMixingUnicodeAndBytesPython3] |
| 164 |
from typing import Literal |
| 165 |
|
| 166 |
a_ann: Literal[u"foo"] |
| 167 |
b_ann: Literal["foo"] |
| 168 |
c_ann: Literal[b"foo"] |
| 169 |
|
| 170 |
a_hint = u"foo" # type: Literal[u"foo"] |
| 171 |
b_hint = "foo" # type: Literal["foo"] |
| 172 |
c_hint = b"foo" # type: Literal[b"foo"] |
| 173 |
|
| 174 |
AAlias = Literal[u"foo"] |
| 175 |
BAlias = Literal["foo"] |
| 176 |
CAlias = Literal[b"foo"] |
| 177 |
a_alias: AAlias |
| 178 |
b_alias: BAlias |
| 179 |
c_alias: CAlias |
| 180 |
|
| 181 |
def accepts_str_1(x: Literal[u"foo"]) -> None: pass |
| 182 |
def accepts_str_2(x: Literal["foo"]) -> None: pass |
| 183 |
def accepts_bytes(x: Literal[b"foo"]) -> None: pass |
| 184 |
|
| 185 |
reveal_type(a_ann) # N: Revealed type is "Literal['foo']" |
| 186 |
reveal_type(b_ann) # N: Revealed type is "Literal['foo']" |
| 187 |
reveal_type(c_ann) # N: Revealed type is "Literal[b'foo']" |
| 188 |
reveal_type(a_hint) # N: Revealed type is "Literal['foo']" |
| 189 |
reveal_type(b_hint) # N: Revealed type is "Literal['foo']" |
| 190 |
reveal_type(c_hint) # N: Revealed type is "Literal[b'foo']" |
| 191 |
reveal_type(a_alias) # N: Revealed type is "Literal['foo']" |
| 192 |
reveal_type(b_alias) # N: Revealed type is "Literal['foo']" |
| 193 |
reveal_type(c_alias) # N: Revealed type is "Literal[b'foo']" |
| 194 |
|
| 195 |
accepts_str_1(a_ann) |
| 196 |
accepts_str_1(b_ann) |
| 197 |
accepts_str_1(c_ann) # E: Argument 1 to "accepts_str_1" has incompatible type "Literal[b'foo']"; expected "Literal['foo']" |
| 198 |
accepts_str_1(a_hint) |
| 199 |
accepts_str_1(b_hint) |
| 200 |
accepts_str_1(c_hint) # E: Argument 1 to "accepts_str_1" has incompatible type "Literal[b'foo']"; expected "Literal['foo']" |
| 201 |
accepts_str_1(a_alias) |
| 202 |
accepts_str_1(b_alias) |
| 203 |
accepts_str_1(c_alias) # E: Argument 1 to "accepts_str_1" has incompatible type "Literal[b'foo']"; expected "Literal['foo']" |
| 204 |
|
| 205 |
accepts_str_2(a_ann) |
| 206 |
accepts_str_2(b_ann) |
| 207 |
accepts_str_2(c_ann) # E: Argument 1 to "accepts_str_2" has incompatible type "Literal[b'foo']"; expected "Literal['foo']" |
| 208 |
accepts_str_2(a_hint) |
| 209 |
accepts_str_2(b_hint) |
| 210 |
accepts_str_2(c_hint) # E: Argument 1 to "accepts_str_2" has incompatible type "Literal[b'foo']"; expected "Literal['foo']" |
| 211 |
accepts_str_2(a_alias) |
| 212 |
accepts_str_2(b_alias) |
| 213 |
accepts_str_2(c_alias) # E: Argument 1 to "accepts_str_2" has incompatible type "Literal[b'foo']"; expected "Literal['foo']" |
| 214 |
|
| 215 |
accepts_bytes(a_ann) # E: Argument 1 to "accepts_bytes" has incompatible type "Literal['foo']"; expected "Literal[b'foo']" |
| 216 |
accepts_bytes(b_ann) # E: Argument 1 to "accepts_bytes" has incompatible type "Literal['foo']"; expected "Literal[b'foo']" |
| 217 |
accepts_bytes(c_ann) |
| 218 |
accepts_bytes(a_hint) # E: Argument 1 to "accepts_bytes" has incompatible type "Literal['foo']"; expected "Literal[b'foo']" |
| 219 |
accepts_bytes(b_hint) # E: Argument 1 to "accepts_bytes" has incompatible type "Literal['foo']"; expected "Literal[b'foo']" |
| 220 |
accepts_bytes(c_hint) |
| 221 |
accepts_bytes(a_alias) # E: Argument 1 to "accepts_bytes" has incompatible type "Literal['foo']"; expected "Literal[b'foo']" |
| 222 |
accepts_bytes(b_alias) # E: Argument 1 to "accepts_bytes" has incompatible type "Literal['foo']"; expected "Literal[b'foo']" |
| 223 |
accepts_bytes(c_alias) |
| 224 |
[builtins fixtures/tuple.pyi] |
| 225 |
[out] |
| 226 |
|
| 227 |
[case testLiteralMixingUnicodeAndBytesPython3ForwardStrings_no_native_parse] |
| 228 |
from typing import Literal, TypeVar, Generic |
| 229 |
|
| 230 |
a_unicode_wrapper: u"Literal[u'foo']" |
| 231 |
b_unicode_wrapper: u"Literal['foo']" |
| 232 |
c_unicode_wrapper: u"Literal[b'foo']" |
| 233 |
|
| 234 |
a_str_wrapper: "Literal[u'foo']" |
| 235 |
b_str_wrapper: "Literal['foo']" |
| 236 |
c_str_wrapper: "Literal[b'foo']" |
| 237 |
|
| 238 |
# In Python 3, forward references MUST be str, not bytes |
| 239 |
a_bytes_wrapper: b"Literal[u'foo']" # E: Invalid type comment or annotation |
| 240 |
b_bytes_wrapper: b"Literal['foo']" # E: Invalid type comment or annotation |
| 241 |
c_bytes_wrapper: b"Literal[b'foo']" # E: Invalid type comment or annotation |
| 242 |
|
| 243 |
reveal_type(a_unicode_wrapper) # N: Revealed type is "Literal['foo']" |
| 244 |
reveal_type(b_unicode_wrapper) # N: Revealed type is "Literal['foo']" |
| 245 |
reveal_type(c_unicode_wrapper) # N: Revealed type is "Literal[b'foo']" |
| 246 |
|
| 247 |
reveal_type(a_str_wrapper) # N: Revealed type is "Literal['foo']" |
| 248 |
reveal_type(b_str_wrapper) # N: Revealed type is "Literal['foo']" |
| 249 |
reveal_type(c_str_wrapper) # N: Revealed type is "Literal[b'foo']" |
| 250 |
|
| 251 |
T = TypeVar('T') |
| 252 |
class Wrap(Generic[T]): pass |
| 253 |
|
| 254 |
AUnicodeWrapperAlias = Wrap[u"Literal[u'foo']"] |
| 255 |
BUnicodeWrapperAlias = Wrap[u"Literal['foo']"] |
| 256 |
CUnicodeWrapperAlias = Wrap[u"Literal[b'foo']"] |
| 257 |
a_unicode_wrapper_alias: AUnicodeWrapperAlias |
| 258 |
b_unicode_wrapper_alias: BUnicodeWrapperAlias |
| 259 |
c_unicode_wrapper_alias: CUnicodeWrapperAlias |
| 260 |
|
| 261 |
AStrWrapperAlias = Wrap["Literal[u'foo']"] |
| 262 |
BStrWrapperAlias = Wrap["Literal['foo']"] |
| 263 |
CStrWrapperAlias = Wrap["Literal[b'foo']"] |
| 264 |
a_str_wrapper_alias: AStrWrapperAlias |
| 265 |
b_str_wrapper_alias: BStrWrapperAlias |
| 266 |
c_str_wrapper_alias: CStrWrapperAlias |
| 267 |
|
| 268 |
ABytesWrapperAlias = Wrap[b"Literal[u'foo']"] |
| 269 |
BBytesWrapperAlias = Wrap[b"Literal['foo']"] |
| 270 |
CBytesWrapperAlias = Wrap[b"Literal[b'foo']"] |
| 271 |
a_bytes_wrapper_alias: ABytesWrapperAlias |
| 272 |
b_bytes_wrapper_alias: BBytesWrapperAlias |
| 273 |
c_bytes_wrapper_alias: CBytesWrapperAlias |
| 274 |
|
| 275 |
# In Python 3, we assume that Literal['foo'] and Literal[u'foo'] are always |
| 276 |
# equivalent, no matter what. |
| 277 |
reveal_type(a_unicode_wrapper_alias) # N: Revealed type is "__main__.Wrap[Literal['foo']]" |
| 278 |
reveal_type(b_unicode_wrapper_alias) # N: Revealed type is "__main__.Wrap[Literal['foo']]" |
| 279 |
reveal_type(c_unicode_wrapper_alias) # N: Revealed type is "__main__.Wrap[Literal[b'foo']]" |
| 280 |
|
| 281 |
reveal_type(a_str_wrapper_alias) # N: Revealed type is "__main__.Wrap[Literal['foo']]" |
| 282 |
reveal_type(b_str_wrapper_alias) # N: Revealed type is "__main__.Wrap[Literal['foo']]" |
| 283 |
reveal_type(c_str_wrapper_alias) # N: Revealed type is "__main__.Wrap[Literal[b'foo']]" |
| 284 |
|
| 285 |
reveal_type(a_bytes_wrapper_alias) # N: Revealed type is "__main__.Wrap[Literal['foo']]" |
| 286 |
reveal_type(b_bytes_wrapper_alias) # N: Revealed type is "__main__.Wrap[Literal['foo']]" |
| 287 |
reveal_type(c_bytes_wrapper_alias) # N: Revealed type is "__main__.Wrap[Literal[b'foo']]" |
| 288 |
[builtins fixtures/tuple.pyi] |
| 289 |
[out] |
| 290 |
|
| 291 |
[case testLiteralUnicodeWeirdCharacters-skip_path_normalization] |
| 292 |
from typing import Any, Literal |
| 293 |
|
| 294 |
a1: Literal["\x00\xAC\x62 \u2227 \u03bb(p)"] |
| 295 |
b1: Literal["\x00¬b ∧ λ(p)"] |
| 296 |
c1: Literal["¬b ∧ λ(p)"] |
| 297 |
d1: Literal["\U0001F600"] |
| 298 |
e1: Literal["😀"] |
| 299 |
|
| 300 |
Alias1 = Literal["\x00\xAC\x62 \u2227 \u03bb(p)"] |
| 301 |
Alias2 = Literal["\x00¬b ∧ λ(p)"] |
| 302 |
Alias3 = Literal["¬b ∧ λ(p)"] |
| 303 |
Alias4 = Literal["\U0001F600"] |
| 304 |
Alias5 = Literal["😀"] |
| 305 |
a2: Alias1 |
| 306 |
b2: Alias2 |
| 307 |
c2: Alias3 |
| 308 |
d2: Alias4 |
| 309 |
e2: Alias5 |
| 310 |
|
| 311 |
blah: Any |
| 312 |
a3 = blah # type: Literal["\x00\xAC\x62 \u2227 \u03bb(p)"] |
| 313 |
b3 = blah # type: Literal["\x00¬b ∧ λ(p)"] |
| 314 |
c3 = blah # type: Literal["¬b ∧ λ(p)"] |
| 315 |
d3 = blah # type: Literal["\U0001F600"] |
| 316 |
e3 = blah # type: Literal["😀"] |
| 317 |
|
| 318 |
reveal_type(a1) # N: Revealed type is "Literal['\x00¬b ∧ λ(p)']" |
| 319 |
reveal_type(b1) # N: Revealed type is "Literal['\x00¬b ∧ λ(p)']" |
| 320 |
reveal_type(c1) # N: Revealed type is "Literal['¬b ∧ λ(p)']" |
| 321 |
reveal_type(d1) # N: Revealed type is "Literal['😀']" |
| 322 |
reveal_type(e1) # N: Revealed type is "Literal['😀']" |
| 323 |
|
| 324 |
reveal_type(a2) # N: Revealed type is "Literal['\x00¬b ∧ λ(p)']" |
| 325 |
reveal_type(b2) # N: Revealed type is "Literal['\x00¬b ∧ λ(p)']" |
| 326 |
reveal_type(c2) # N: Revealed type is "Literal['¬b ∧ λ(p)']" |
| 327 |
reveal_type(d2) # N: Revealed type is "Literal['😀']" |
| 328 |
reveal_type(e2) # N: Revealed type is "Literal['😀']" |
| 329 |
|
| 330 |
reveal_type(a3) # N: Revealed type is "Literal['\x00¬b ∧ λ(p)']" |
| 331 |
reveal_type(b3) # N: Revealed type is "Literal['\x00¬b ∧ λ(p)']" |
| 332 |
reveal_type(c3) # N: Revealed type is "Literal['¬b ∧ λ(p)']" |
| 333 |
reveal_type(d3) # N: Revealed type is "Literal['😀']" |
| 334 |
reveal_type(e3) # N: Revealed type is "Literal['😀']" |
| 335 |
|
| 336 |
a1 = b1 |
| 337 |
a1 = c1 # E: Incompatible types in assignment (expression has type "Literal['¬b ∧ λ(p)']", variable has type "Literal['\x00¬b ∧ λ(p)']") |
| 338 |
a1 = a2 |
| 339 |
a1 = b2 |
| 340 |
a1 = c2 # E: Incompatible types in assignment (expression has type "Literal['¬b ∧ λ(p)']", variable has type "Literal['\x00¬b ∧ λ(p)']") |
| 341 |
a1 = a3 |
| 342 |
a1 = b3 |
| 343 |
a1 = c3 # E: Incompatible types in assignment (expression has type "Literal['¬b ∧ λ(p)']", variable has type "Literal['\x00¬b ∧ λ(p)']") |
| 344 |
[builtins fixtures/tuple.pyi] |
| 345 |
|
| 346 |
[out] |
| 347 |
|
| 348 |
[case testLiteralRenamingImportWorks] |
| 349 |
from typing import Literal as Foo |
| 350 |
|
| 351 |
x: Foo[3] |
| 352 |
reveal_type(x) # N: Revealed type is "Literal[3]" |
| 353 |
|
| 354 |
y: Foo["hello"] |
| 355 |
reveal_type(y) # N: Revealed type is "Literal['hello']" |
| 356 |
[builtins fixtures/tuple.pyi] |
| 357 |
[out] |
| 358 |
|
| 359 |
[case testLiteralRenamingImportViaAnotherImportWorks] |
| 360 |
from other_module import Foo, Bar |
| 361 |
|
| 362 |
x: Foo[3] |
| 363 |
y: Bar |
| 364 |
|
| 365 |
reveal_type(x) # N: Revealed type is "Literal[3]" |
| 366 |
reveal_type(y) # N: Revealed type is "Literal[4]" |
| 367 |
|
| 368 |
[file other_module.py] |
| 369 |
from typing import Literal as Foo |
| 370 |
Bar = Foo[4] |
| 371 |
[builtins fixtures/tuple.pyi] |
| 372 |
[out] |
| 373 |
|
| 374 |
[case testLiteralRenamingImportNameConfusion] |
| 375 |
from typing import Literal as Foo |
| 376 |
|
| 377 |
x: Foo["Foo"] |
| 378 |
reveal_type(x) # N: Revealed type is "Literal['Foo']" |
| 379 |
|
| 380 |
y: Foo[Foo] # E: Literal[...] must have at least one parameter |
| 381 |
[builtins fixtures/tuple.pyi] |
| 382 |
[out] |
| 383 |
|
| 384 |
[case testLiteralBadRawExpressionWithBadType] |
| 385 |
|
| 386 |
NotAType = 3 |
| 387 |
def f() -> NotAType['also' + 'not' + 'a' + 'type']: ... # E: Variable "__main__.NotAType" is not valid as a type \ |
| 388 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases \ |
| 389 |
# E: Invalid type comment or annotation |
| 390 |
|
| 391 |
# Note: this makes us re-inspect the type (e.g. via '_patch_indirect_dependencies' |
| 392 |
# in build.py) so we can confirm the RawExpressionType did not leak out. |
| 393 |
indirect = f() |
| 394 |
[out] |
| 395 |
|
| 396 |
-- |
| 397 |
-- Check to make sure we can construct the correct range of literal |
| 398 |
-- types (and correctly reject invalid literal types) |
| 399 |
-- |
| 400 |
-- Note: the assignment tests exercise the logic in 'fastparse.py'; |
| 401 |
-- the type alias tests exercise the logic in 'exprtotype.py'. |
| 402 |
-- |
| 403 |
|
| 404 |
[case testLiteralBasicIntUsage] |
| 405 |
from typing import Literal |
| 406 |
|
| 407 |
a1: Literal[4] |
| 408 |
b1: Literal[0x2a] |
| 409 |
c1: Literal[-300] |
| 410 |
d1: Literal[+8] |
| 411 |
|
| 412 |
reveal_type(a1) # N: Revealed type is "Literal[4]" |
| 413 |
reveal_type(b1) # N: Revealed type is "Literal[42]" |
| 414 |
reveal_type(c1) # N: Revealed type is "Literal[-300]" |
| 415 |
reveal_type(d1) # N: Revealed type is "Literal[8]" |
| 416 |
|
| 417 |
a2t = Literal[4] |
| 418 |
b2t = Literal[0x2a] |
| 419 |
c2t = Literal[-300] |
| 420 |
d2t = Literal[+8] |
| 421 |
a2: a2t |
| 422 |
b2: b2t |
| 423 |
c2: c2t |
| 424 |
d2: d2t |
| 425 |
|
| 426 |
reveal_type(a2) # N: Revealed type is "Literal[4]" |
| 427 |
reveal_type(b2) # N: Revealed type is "Literal[42]" |
| 428 |
reveal_type(c2) # N: Revealed type is "Literal[-300]" |
| 429 |
reveal_type(d2) # N: Revealed type is "Literal[8]" |
| 430 |
|
| 431 |
def f1(x: Literal[4]) -> Literal[4]: pass |
| 432 |
def f2(x: Literal[0x2a]) -> Literal[0x2a]: pass |
| 433 |
def f3(x: Literal[-300]) -> Literal[-300]: pass |
| 434 |
def f4(x: Literal[+8]) -> Literal[+8]: pass |
| 435 |
|
| 436 |
reveal_type(f1) # N: Revealed type is "def (x: Literal[4]) -> Literal[4]" |
| 437 |
reveal_type(f2) # N: Revealed type is "def (x: Literal[42]) -> Literal[42]" |
| 438 |
reveal_type(f3) # N: Revealed type is "def (x: Literal[-300]) -> Literal[-300]" |
| 439 |
reveal_type(f4) # N: Revealed type is "def (x: Literal[8]) -> Literal[8]" |
| 440 |
[builtins fixtures/tuple.pyi] |
| 441 |
[out] |
| 442 |
|
| 443 |
[case testLiteralBasicBoolUsage] |
| 444 |
from typing import Literal |
| 445 |
|
| 446 |
a1: Literal[True] |
| 447 |
b1: Literal[False] |
| 448 |
|
| 449 |
reveal_type(a1) # N: Revealed type is "Literal[True]" |
| 450 |
reveal_type(b1) # N: Revealed type is "Literal[False]" |
| 451 |
|
| 452 |
a2t = Literal[True] |
| 453 |
b2t = Literal[False] |
| 454 |
a2: a2t |
| 455 |
b2: b2t |
| 456 |
|
| 457 |
reveal_type(a2) # N: Revealed type is "Literal[True]" |
| 458 |
reveal_type(b2) # N: Revealed type is "Literal[False]" |
| 459 |
|
| 460 |
def f1(x: Literal[True]) -> Literal[True]: pass |
| 461 |
def f2(x: Literal[False]) -> Literal[False]: pass |
| 462 |
|
| 463 |
reveal_type(f1) # N: Revealed type is "def (x: Literal[True]) -> Literal[True]" |
| 464 |
reveal_type(f2) # N: Revealed type is "def (x: Literal[False]) -> Literal[False]" |
| 465 |
[builtins fixtures/bool.pyi] |
| 466 |
[out] |
| 467 |
|
| 468 |
[case testLiteralBasicStrUsage] |
| 469 |
from typing import Literal |
| 470 |
|
| 471 |
a: Literal[""] |
| 472 |
b: Literal[" foo bar "] |
| 473 |
c: Literal[' foo bar '] |
| 474 |
d: Literal["foo"] |
| 475 |
e: Literal['foo'] |
| 476 |
|
| 477 |
reveal_type(a) # N: Revealed type is "Literal['']" |
| 478 |
reveal_type(b) # N: Revealed type is "Literal[' foo bar ']" |
| 479 |
reveal_type(c) # N: Revealed type is "Literal[' foo bar ']" |
| 480 |
reveal_type(d) # N: Revealed type is "Literal['foo']" |
| 481 |
reveal_type(e) # N: Revealed type is "Literal['foo']" |
| 482 |
|
| 483 |
def f1(x: Literal[""]) -> Literal[""]: pass |
| 484 |
def f2(x: Literal[" foo bar "]) -> Literal[" foo bar "]: pass |
| 485 |
def f3(x: Literal[' foo bar ']) -> Literal[' foo bar ']: pass |
| 486 |
def f4(x: Literal["foo"]) -> Literal["foo"]: pass |
| 487 |
def f5(x: Literal['foo']) -> Literal['foo']: pass |
| 488 |
|
| 489 |
reveal_type(f1) # N: Revealed type is "def (x: Literal['']) -> Literal['']" |
| 490 |
reveal_type(f2) # N: Revealed type is "def (x: Literal[' foo bar ']) -> Literal[' foo bar ']" |
| 491 |
reveal_type(f3) # N: Revealed type is "def (x: Literal[' foo bar ']) -> Literal[' foo bar ']" |
| 492 |
reveal_type(f4) # N: Revealed type is "def (x: Literal['foo']) -> Literal['foo']" |
| 493 |
reveal_type(f5) # N: Revealed type is "def (x: Literal['foo']) -> Literal['foo']" |
| 494 |
[builtins fixtures/tuple.pyi] |
| 495 |
[out] |
| 496 |
|
| 497 |
[case testLiteralBasicStrUsageSlashes-skip_path_normalization] |
| 498 |
from typing import Literal |
| 499 |
|
| 500 |
a: Literal[r"foo\nbar"] |
| 501 |
b: Literal["foo\nbar"] |
| 502 |
|
| 503 |
reveal_type(a) |
| 504 |
reveal_type(b) |
| 505 |
[builtins fixtures/tuple.pyi] |
| 506 |
[out] |
| 507 |
main:6: note: Revealed type is "Literal['foo\\nbar']" |
| 508 |
main:7: note: Revealed type is "Literal['foo\nbar']" |
| 509 |
|
| 510 |
[case testLiteralBasicNoneUsage] |
| 511 |
# Note: Literal[None] and None are equivalent |
| 512 |
from typing import Literal |
| 513 |
a: Literal[None] |
| 514 |
reveal_type(a) # N: Revealed type is "None" |
| 515 |
|
| 516 |
def f1(x: Literal[None]) -> None: pass |
| 517 |
def f2(x: None) -> Literal[None]: pass |
| 518 |
def f3(x: Literal[None]) -> Literal[None]: pass |
| 519 |
|
| 520 |
reveal_type(f1) # N: Revealed type is "def (x: None)" |
| 521 |
reveal_type(f2) # N: Revealed type is "def (x: None)" |
| 522 |
reveal_type(f3) # N: Revealed type is "def (x: None)" |
| 523 |
[builtins fixtures/tuple.pyi] |
| 524 |
[out] |
| 525 |
|
| 526 |
[case testLiteralCallingUnionFunction] |
| 527 |
from typing import Literal |
| 528 |
|
| 529 |
def func(x: Literal['foo', 'bar', ' foo ']) -> None: ... |
| 530 |
|
| 531 |
func('foo') |
| 532 |
func('bar') |
| 533 |
func(' foo ') |
| 534 |
func('baz') # E: Argument 1 to "func" has incompatible type "Literal['baz']"; expected "Literal['foo', 'bar', ' foo ']" |
| 535 |
|
| 536 |
a: Literal['foo'] |
| 537 |
b: Literal['bar'] |
| 538 |
c: Literal[' foo '] |
| 539 |
d: Literal['foo', 'bar'] |
| 540 |
e: Literal['foo', 'bar', ' foo '] |
| 541 |
f: Literal['foo', 'bar', 'baz'] |
| 542 |
|
| 543 |
func(a) |
| 544 |
func(b) |
| 545 |
func(c) |
| 546 |
func(d) |
| 547 |
func(e) |
| 548 |
func(f) # E: Argument 1 to "func" has incompatible type "Literal['foo', 'bar', 'baz']"; expected "Literal['foo', 'bar', ' foo ']" |
| 549 |
[builtins fixtures/tuple.pyi] |
| 550 |
[out] |
| 551 |
|
| 552 |
[case testLiteralDisallowAny] |
| 553 |
from typing import Any, Literal |
| 554 |
from missing_module import BadAlias # E: Cannot find implementation or library stub for module named "missing_module" \ |
| 555 |
# N: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports |
| 556 |
|
| 557 |
a: Literal[Any] # E: Parameter 1 of Literal[...] cannot be of type "Any" |
| 558 |
b: Literal[BadAlias] # E: Parameter 1 of Literal[...] cannot be of type "Any" |
| 559 |
|
| 560 |
reveal_type(a) # N: Revealed type is "Any" |
| 561 |
reveal_type(b) # N: Revealed type is "Any" |
| 562 |
[builtins fixtures/tuple.pyi] |
| 563 |
[out] |
| 564 |
|
| 565 |
[case testLiteralDisallowActualTypes] |
| 566 |
from typing import Literal |
| 567 |
|
| 568 |
a: Literal[int] # E: Parameter 1 of Literal[...] is invalid |
| 569 |
b: Literal[float] # E: Parameter 1 of Literal[...] is invalid |
| 570 |
c: Literal[bool] # E: Parameter 1 of Literal[...] is invalid |
| 571 |
d: Literal[str] # E: Parameter 1 of Literal[...] is invalid |
| 572 |
|
| 573 |
reveal_type(a) # N: Revealed type is "Any" |
| 574 |
reveal_type(b) # N: Revealed type is "Any" |
| 575 |
reveal_type(c) # N: Revealed type is "Any" |
| 576 |
reveal_type(d) # N: Revealed type is "Any" |
| 577 |
[builtins fixtures/primitives.pyi] |
| 578 |
[out] |
| 579 |
|
| 580 |
[case testLiteralDisallowFloatsAndComplex_no_native_parse] |
| 581 |
|
| 582 |
from typing import Literal |
| 583 |
a1: Literal[3.14] # E: Parameter 1 of Literal[...] cannot be of type "float" |
| 584 |
b1: 3.14 # E: Invalid type: float literals cannot be used as a type |
| 585 |
c1: Literal[3j] # E: Parameter 1 of Literal[...] cannot be of type "complex" |
| 586 |
d1: 3j # E: Invalid type: complex literals cannot be used as a type |
| 587 |
|
| 588 |
a2t = Literal[3.14] # E: Parameter 1 of Literal[...] cannot be of type "float" |
| 589 |
b2t = 3.14 |
| 590 |
c2t = Literal[3j] # E: Parameter 1 of Literal[...] cannot be of type "complex" |
| 591 |
d2t = 3j |
| 592 |
|
| 593 |
a2: a2t |
| 594 |
reveal_type(a2) # N: Revealed type is "Any" |
| 595 |
b2: b2t # E: Variable "__main__.b2t" is not valid as a type \ |
| 596 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases |
| 597 |
c2: c2t |
| 598 |
reveal_type(c2) # N: Revealed type is "Any" |
| 599 |
d2: d2t # E: Variable "__main__.d2t" is not valid as a type \ |
| 600 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases |
| 601 |
[builtins fixtures/complex_tuple.pyi] |
| 602 |
[out] |
| 603 |
|
| 604 |
[case testLiteralDisallowComplexExpressions_no_native_parse] |
| 605 |
from typing import Literal |
| 606 |
def dummy() -> int: return 3 |
| 607 |
a: Literal[3 + 4] # E: Invalid type: Literal[...] cannot contain arbitrary expressions |
| 608 |
b: Literal[" foo ".trim()] # E: Invalid type: Literal[...] cannot contain arbitrary expressions |
| 609 |
d: Literal[~12] # E: Invalid type: Literal[...] cannot contain arbitrary expressions |
| 610 |
e: Literal[dummy()] # E: Invalid type: Literal[...] cannot contain arbitrary expressions |
| 611 |
[builtins fixtures/tuple.pyi] |
| 612 |
[out] |
| 613 |
|
| 614 |
[case testLiteralDisallowCollections] |
| 615 |
from typing import Literal |
| 616 |
a: Literal[{"a": 1, "b": 2}] # E: Parameter 1 of Literal[...] is invalid |
| 617 |
b: Literal[{1, 2, 3}] # E: Invalid type: Literal[...] cannot contain arbitrary expressions |
| 618 |
c: {"a": 1, "b": 2} # E: Inline TypedDict is experimental, must be enabled with --enable-incomplete-feature=InlineTypedDict \ |
| 619 |
# E: Invalid type: try using Literal[1] instead? \ |
| 620 |
# E: Invalid type: try using Literal[2] instead? |
| 621 |
d: {1, 2, 3} # E: Invalid type comment or annotation |
| 622 |
[builtins fixtures/tuple.pyi] |
| 623 |
[typing fixtures/typing-full.pyi] |
| 624 |
|
| 625 |
[case testLiteralDisallowCollections2] |
| 626 |
from typing import Literal |
| 627 |
a: (1, 2, 3) # E: Syntax error in type annotation \ |
| 628 |
# N: Suggestion: Use Tuple[T1, ..., Tn] instead of (T1, ..., Tn) |
| 629 |
b: Literal[[1, 2, 3]] # E: Parameter 1 of Literal[...] is invalid |
| 630 |
c: [1, 2, 3] # E: Bracketed expression "[...]" is not valid as a type |
| 631 |
[builtins fixtures/tuple.pyi] |
| 632 |
|
| 633 |
[case testLiteralDisallowCollectionsTypeAlias] |
| 634 |
from typing import Literal |
| 635 |
at = Literal[{"a": 1, "b": 2}] # E: Parameter 1 of Literal[...] is invalid |
| 636 |
bt = {"a": 1, "b": 2} |
| 637 |
a: at |
| 638 |
reveal_type(a) # N: Revealed type is "Any" |
| 639 |
b: bt # E: Variable "__main__.bt" is not valid as a type \ |
| 640 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases |
| 641 |
[builtins fixtures/dict.pyi] |
| 642 |
[typing fixtures/typing-typeddict.pyi] |
| 643 |
|
| 644 |
[case testLiteralDisallowCollectionsTypeAlias2] |
| 645 |
from typing import Literal |
| 646 |
at = Literal[{1, 2, 3}] # E: Invalid type alias: expression is not a valid type |
| 647 |
bt = {1, 2, 3} |
| 648 |
a: at # E: Variable "__main__.at" is not valid as a type \ |
| 649 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases |
| 650 |
b: bt # E: Variable "__main__.bt" is not valid as a type \ |
| 651 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases |
| 652 |
[builtins fixtures/set.pyi] |
| 653 |
[typing fixtures/typing-full.pyi] |
| 654 |
[out] |
| 655 |
|
| 656 |
[case testLiteralDisallowTypeVar] |
| 657 |
from typing import Literal, TypeVar, Tuple |
| 658 |
|
| 659 |
T = TypeVar('T') |
| 660 |
|
| 661 |
at = Literal[T] # E: Parameter 1 of Literal[...] is invalid |
| 662 |
a: at |
| 663 |
|
| 664 |
def foo(b: Literal[T]) -> Tuple[T]: pass # E: Parameter 1 of Literal[...] is invalid |
| 665 |
[builtins fixtures/tuple.pyi] |
| 666 |
[out] |
| 667 |
|
| 668 |
|
| 669 |
-- |
| 670 |
-- Test mixing and matching literals with other types |
| 671 |
-- |
| 672 |
|
| 673 |
[case testLiteralMultipleValues] |
| 674 |
from typing import Literal |
| 675 |
a: Literal[1, 2, 3] |
| 676 |
b: Literal["a", "b", "c"] |
| 677 |
c: Literal[1, "b", True, None] |
| 678 |
d: Literal[1, 1, 1] |
| 679 |
e: Literal[None, None, None] |
| 680 |
reveal_type(a) # N: Revealed type is "Literal[1] | Literal[2] | Literal[3]" |
| 681 |
reveal_type(b) # N: Revealed type is "Literal['a'] | Literal['b'] | Literal['c']" |
| 682 |
reveal_type(c) # N: Revealed type is "Literal[1] | Literal['b'] | Literal[True] | None" |
| 683 |
|
| 684 |
# Note: I was thinking these should be simplified, but it seems like |
| 685 |
# mypy doesn't simplify unions with duplicate values with other types. |
| 686 |
reveal_type(d) # N: Revealed type is "Literal[1] | Literal[1] | Literal[1]" |
| 687 |
reveal_type(e) # N: Revealed type is "None | None | None" |
| 688 |
[builtins fixtures/bool.pyi] |
| 689 |
[out] |
| 690 |
|
| 691 |
[case testLiteralMultipleValuesExplicitTuple] |
| 692 |
from typing import Literal |
| 693 |
# Unfortunately, it seems like typed_ast is unable to distinguish this from |
| 694 |
# Literal[1, 2, 3]. So we treat the two as being equivalent for now. |
| 695 |
a: Literal[1, 2, 3] |
| 696 |
b: Literal[(1, 2, 3)] |
| 697 |
reveal_type(a) # N: Revealed type is "Literal[1] | Literal[2] | Literal[3]" |
| 698 |
reveal_type(b) # N: Revealed type is "Literal[1] | Literal[2] | Literal[3]" |
| 699 |
[builtins fixtures/tuple.pyi] |
| 700 |
[out] |
| 701 |
|
| 702 |
[case testLiteralNestedUsage] |
| 703 |
|
| 704 |
from typing import Literal |
| 705 |
a: Literal[Literal[3], 4, Literal["foo"]] |
| 706 |
reveal_type(a) # N: Revealed type is "Literal[3] | Literal[4] | Literal['foo']" |
| 707 |
|
| 708 |
alias_for_literal = Literal[5] |
| 709 |
b: Literal[alias_for_literal] |
| 710 |
reveal_type(b) # N: Revealed type is "Literal[5]" |
| 711 |
|
| 712 |
another_alias = Literal[1, None] |
| 713 |
c: Literal[alias_for_literal, another_alias, "r"] |
| 714 |
reveal_type(c) # N: Revealed type is "Literal[5] | Literal[1] | None | Literal['r']" |
| 715 |
|
| 716 |
basic_mode = Literal["r", "w", "a"] |
| 717 |
basic_with_plus = Literal["r+", "w+", "a+"] |
| 718 |
combined: Literal[basic_mode, basic_with_plus] |
| 719 |
reveal_type(combined) # N: Revealed type is "Literal['r'] | Literal['w'] | Literal['a'] | Literal['r+'] | Literal['w+'] | Literal['a+']" |
| 720 |
[builtins fixtures/tuple.pyi] |
| 721 |
[out] |
| 722 |
|
| 723 |
[case testLiteralBiasTowardsAssumingForwardReference] |
| 724 |
from typing import Literal |
| 725 |
|
| 726 |
a: "Foo" |
| 727 |
reveal_type(a) # N: Revealed type is "__main__.Foo" |
| 728 |
|
| 729 |
b: Literal["Foo"] |
| 730 |
reveal_type(b) # N: Revealed type is "Literal['Foo']" |
| 731 |
|
| 732 |
c: "Literal[Foo]" # E: Parameter 1 of Literal[...] is invalid |
| 733 |
|
| 734 |
d: "Literal['Foo']" |
| 735 |
reveal_type(d) # N: Revealed type is "Literal['Foo']" |
| 736 |
|
| 737 |
class Foo: pass |
| 738 |
[builtins fixtures/tuple.pyi] |
| 739 |
[out] |
| 740 |
|
| 741 |
[case testLiteralBiasTowardsAssumingForwardReferenceForTypeAliases] |
| 742 |
from typing import Literal |
| 743 |
|
| 744 |
a: "Foo" |
| 745 |
reveal_type(a) # N: Revealed type is "Literal[5]" |
| 746 |
|
| 747 |
b: Literal["Foo"] |
| 748 |
reveal_type(b) # N: Revealed type is "Literal['Foo']" |
| 749 |
|
| 750 |
c: "Literal[Foo]" |
| 751 |
reveal_type(c) # N: Revealed type is "Literal[5]" |
| 752 |
|
| 753 |
d: "Literal['Foo']" |
| 754 |
reveal_type(d) # N: Revealed type is "Literal['Foo']" |
| 755 |
|
| 756 |
e: Literal[Foo, 'Foo'] |
| 757 |
reveal_type(e) # N: Revealed type is "Literal[5] | Literal['Foo']" |
| 758 |
|
| 759 |
Foo = Literal[5] |
| 760 |
[builtins fixtures/tuple.pyi] |
| 761 |
[out] |
| 762 |
|
| 763 |
[case testLiteralBiasTowardsAssumingForwardReferencesForTypeComments] |
| 764 |
from typing import Literal |
| 765 |
|
| 766 |
a: Foo |
| 767 |
reveal_type(a) # N: Revealed type is "__main__.Foo" |
| 768 |
|
| 769 |
b: "Foo" |
| 770 |
reveal_type(b) # N: Revealed type is "__main__.Foo" |
| 771 |
|
| 772 |
c: Literal["Foo"] |
| 773 |
reveal_type(c) # N: Revealed type is "Literal['Foo']" |
| 774 |
|
| 775 |
d: Literal[Foo] # E: Parameter 1 of Literal[...] is invalid |
| 776 |
|
| 777 |
class Foo: pass |
| 778 |
[builtins fixtures/tuple.pyi] |
| 779 |
[out] |
| 780 |
|
| 781 |
|
| 782 |
-- |
| 783 |
-- Check how we handle very basic subtyping and other useful things |
| 784 |
-- |
| 785 |
|
| 786 |
[case testLiteralCallingFunction] |
| 787 |
from typing import Literal |
| 788 |
def foo(x: Literal[3]) -> None: pass |
| 789 |
|
| 790 |
a: Literal[1] |
| 791 |
b: Literal[2] |
| 792 |
c: int |
| 793 |
|
| 794 |
foo(a) # E: Argument 1 to "foo" has incompatible type "Literal[1]"; expected "Literal[3]" |
| 795 |
foo(b) # E: Argument 1 to "foo" has incompatible type "Literal[2]"; expected "Literal[3]" |
| 796 |
foo(c) # E: Argument 1 to "foo" has incompatible type "int"; expected "Literal[3]" |
| 797 |
[builtins fixtures/tuple.pyi] |
| 798 |
[out] |
| 799 |
|
| 800 |
[case testLiteralCallingFunctionWithUnionLiteral] |
| 801 |
from typing import Literal |
| 802 |
def foo(x: Literal[1, 2, 3]) -> None: pass |
| 803 |
|
| 804 |
a: Literal[1] |
| 805 |
b: Literal[2, 3] |
| 806 |
c: Literal[4, 5] |
| 807 |
d: int |
| 808 |
|
| 809 |
foo(a) |
| 810 |
foo(b) |
| 811 |
foo(c) # E: Argument 1 to "foo" has incompatible type "Literal[4, 5]"; expected "Literal[1, 2, 3]" |
| 812 |
foo(d) # E: Argument 1 to "foo" has incompatible type "int"; expected "Literal[1, 2, 3]" |
| 813 |
[builtins fixtures/tuple.pyi] |
| 814 |
[out] |
| 815 |
|
| 816 |
[case testLiteralCallingFunctionWithStandardBase] |
| 817 |
from typing import Literal |
| 818 |
def foo(x: int) -> None: pass |
| 819 |
|
| 820 |
a: Literal[1] |
| 821 |
b: Literal[1, -4] |
| 822 |
c: Literal[4, 'foo'] |
| 823 |
|
| 824 |
foo(a) |
| 825 |
foo(b) |
| 826 |
foo(c) # E: Argument 1 to "foo" has incompatible type "Literal[4, 'foo']"; expected "int" |
| 827 |
[builtins fixtures/tuple.pyi] |
| 828 |
[out] |
| 829 |
|
| 830 |
[case testLiteralCheckSubtypingStrictOptional] |
| 831 |
from typing import Any, Literal, NoReturn |
| 832 |
|
| 833 |
lit: Literal[1] |
| 834 |
def f_lit(x: Literal[1]) -> None: pass |
| 835 |
|
| 836 |
def fa(x: Any) -> None: pass |
| 837 |
def fb(x: NoReturn) -> None: pass |
| 838 |
def fc(x: None) -> None: pass |
| 839 |
|
| 840 |
a: Any |
| 841 |
b: NoReturn |
| 842 |
c: None |
| 843 |
|
| 844 |
fa(lit) |
| 845 |
fb(lit) # E: Argument 1 to "fb" has incompatible type "Literal[1]"; expected "Never" |
| 846 |
fc(lit) # E: Argument 1 to "fc" has incompatible type "Literal[1]"; expected "None" |
| 847 |
|
| 848 |
f_lit(a) |
| 849 |
f_lit(b) |
| 850 |
f_lit(c) # E: Argument 1 to "f_lit" has incompatible type "None"; expected "Literal[1]" |
| 851 |
[builtins fixtures/tuple.pyi] |
| 852 |
|
| 853 |
[case testLiteralCheckSubtypingNoStrictOptional] |
| 854 |
# flags: --no-strict-optional |
| 855 |
from typing import Any, Literal, NoReturn |
| 856 |
|
| 857 |
lit: Literal[1] |
| 858 |
def f_lit(x: Literal[1]) -> None: pass |
| 859 |
|
| 860 |
def fa(x: Any) -> None: pass |
| 861 |
def fb(x: NoReturn) -> None: pass |
| 862 |
def fc(x: None) -> None: pass |
| 863 |
|
| 864 |
a: Any |
| 865 |
b: NoReturn |
| 866 |
c: None |
| 867 |
|
| 868 |
fa(lit) |
| 869 |
fb(lit) # E: Argument 1 to "fb" has incompatible type "Literal[1]"; expected "Never" |
| 870 |
fc(lit) # E: Argument 1 to "fc" has incompatible type "Literal[1]"; expected "None" |
| 871 |
|
| 872 |
f_lit(a) |
| 873 |
f_lit(b) |
| 874 |
f_lit(c) |
| 875 |
[builtins fixtures/tuple.pyi] |
| 876 |
|
| 877 |
[case testLiteralCallingOverloadedFunction] |
| 878 |
from typing import overload, Generic, Literal, TypeVar, Any |
| 879 |
|
| 880 |
T = TypeVar('T') |
| 881 |
class IOLike(Generic[T]): pass |
| 882 |
|
| 883 |
@overload |
| 884 |
def foo(x: Literal[1]) -> IOLike[int]: ... |
| 885 |
@overload |
| 886 |
def foo(x: Literal[2]) -> IOLike[str]: ... |
| 887 |
@overload |
| 888 |
def foo(x: int) -> IOLike[Any]: ... |
| 889 |
def foo(x: int) -> IOLike[Any]: |
| 890 |
if x == 1: |
| 891 |
return IOLike[int]() |
| 892 |
elif x == 2: |
| 893 |
return IOLike[str]() |
| 894 |
else: |
| 895 |
return IOLike() |
| 896 |
|
| 897 |
a: Literal[1] |
| 898 |
b: Literal[2] |
| 899 |
c: int |
| 900 |
d: Literal[3] |
| 901 |
|
| 902 |
reveal_type(foo(a)) # N: Revealed type is "__main__.IOLike[builtins.int]" |
| 903 |
reveal_type(foo(b)) # N: Revealed type is "__main__.IOLike[builtins.str]" |
| 904 |
reveal_type(foo(c)) # N: Revealed type is "__main__.IOLike[Any]" |
| 905 |
foo(d) |
| 906 |
[builtins fixtures/ops.pyi] |
| 907 |
[out] |
| 908 |
|
| 909 |
[case testLiteralVariance] |
| 910 |
from typing import Generic, Literal, TypeVar |
| 911 |
|
| 912 |
T = TypeVar('T') |
| 913 |
T_co = TypeVar('T_co', covariant=True) |
| 914 |
T_contra = TypeVar('T_contra', contravariant=True) |
| 915 |
|
| 916 |
class Invariant(Generic[T]): pass |
| 917 |
class Covariant(Generic[T_co]): pass |
| 918 |
class Contravariant(Generic[T_contra]): pass |
| 919 |
|
| 920 |
a1: Invariant[Literal[1]] |
| 921 |
a2: Invariant[Literal[1, 2]] |
| 922 |
a3: Invariant[Literal[1, 2, 3]] |
| 923 |
a2 = a1 # E: Incompatible types in assignment (expression has type "Invariant[Literal[1]]", variable has type "Invariant[Literal[1, 2]]") |
| 924 |
a2 = a3 # E: Incompatible types in assignment (expression has type "Invariant[Literal[1, 2, 3]]", variable has type "Invariant[Literal[1, 2]]") |
| 925 |
|
| 926 |
b1: Covariant[Literal[1]] |
| 927 |
b2: Covariant[Literal[1, 2]] |
| 928 |
b3: Covariant[Literal[1, 2, 3]] |
| 929 |
b2 = b1 |
| 930 |
b2 = b3 # E: Incompatible types in assignment (expression has type "Covariant[Literal[1, 2, 3]]", variable has type "Covariant[Literal[1, 2]]") |
| 931 |
|
| 932 |
c1: Contravariant[Literal[1]] |
| 933 |
c2: Contravariant[Literal[1, 2]] |
| 934 |
c3: Contravariant[Literal[1, 2, 3]] |
| 935 |
c2 = c1 # E: Incompatible types in assignment (expression has type "Contravariant[Literal[1]]", variable has type "Contravariant[Literal[1, 2]]") |
| 936 |
c2 = c3 |
| 937 |
[builtins fixtures/tuple.pyi] |
| 938 |
[out] |
| 939 |
|
| 940 |
[case testLiteralInListAndSequence] |
| 941 |
from typing import List, Literal, Sequence |
| 942 |
|
| 943 |
def foo(x: List[Literal[1, 2]]) -> None: pass |
| 944 |
def bar(x: Sequence[Literal[1, 2]]) -> None: pass |
| 945 |
|
| 946 |
a: List[Literal[1]] |
| 947 |
b: List[Literal[1, 2, 3]] |
| 948 |
|
| 949 |
foo(a) # E: Argument 1 to "foo" has incompatible type "list[Literal[1]]"; expected "list[Literal[1, 2]]" \ |
| 950 |
# N: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \ |
| 951 |
# N: Consider using "Sequence" instead, which is covariant |
| 952 |
foo(b) # E: Argument 1 to "foo" has incompatible type "list[Literal[1, 2, 3]]"; expected "list[Literal[1, 2]]" |
| 953 |
bar(a) |
| 954 |
bar(b) # E: Argument 1 to "bar" has incompatible type "list[Literal[1, 2, 3]]"; expected "Sequence[Literal[1, 2]]" |
| 955 |
[builtins fixtures/list.pyi] |
| 956 |
[out] |
| 957 |
|
| 958 |
[case testLiteralRenamingDoesNotChangeTypeChecking] |
| 959 |
from typing import Literal as Foo |
| 960 |
from other_module import Bar1, Bar2, c |
| 961 |
|
| 962 |
def func(x: Foo[15]) -> None: pass |
| 963 |
|
| 964 |
a: Bar1 |
| 965 |
b: Bar2 |
| 966 |
func(a) |
| 967 |
func(b) # E: Argument 1 to "func" has incompatible type "Literal[14]"; expected "Literal[15]" |
| 968 |
func(c) |
| 969 |
|
| 970 |
[file other_module.py] |
| 971 |
from typing import Literal |
| 972 |
|
| 973 |
Bar1 = Literal[15] |
| 974 |
Bar2 = Literal[14] |
| 975 |
c: Literal[15] |
| 976 |
[builtins fixtures/tuple.pyi] |
| 977 |
|
| 978 |
|
| 979 |
-- |
| 980 |
-- Check to make sure we handle inference of literal values correctly, |
| 981 |
-- especially when doing assignments or calls |
| 982 |
-- |
| 983 |
|
| 984 |
[case testLiteralInferredInAssignment] |
| 985 |
from typing import Literal |
| 986 |
|
| 987 |
int1: Literal[1] = 1 |
| 988 |
int2 = 1 |
| 989 |
int3: int = 1 |
| 990 |
|
| 991 |
str1: Literal["foo"] = "foo" |
| 992 |
str2 = "foo" |
| 993 |
str3: str = "foo" |
| 994 |
|
| 995 |
bool1: Literal[True] = True |
| 996 |
bool2 = True |
| 997 |
bool3: bool = True |
| 998 |
|
| 999 |
none1: Literal[None] = None |
| 1000 |
none2 = None |
| 1001 |
none3: None = None |
| 1002 |
|
| 1003 |
reveal_type(int1) # N: Revealed type is "Literal[1]" |
| 1004 |
reveal_type(int2) # N: Revealed type is "builtins.int" |
| 1005 |
reveal_type(int3) # N: Revealed type is "builtins.int" |
| 1006 |
reveal_type(str1) # N: Revealed type is "Literal['foo']" |
| 1007 |
reveal_type(str2) # N: Revealed type is "builtins.str" |
| 1008 |
reveal_type(str3) # N: Revealed type is "builtins.str" |
| 1009 |
reveal_type(bool1) # N: Revealed type is "Literal[True]" |
| 1010 |
reveal_type(bool2) # N: Revealed type is "builtins.bool" |
| 1011 |
reveal_type(bool3) # N: Revealed type is "builtins.bool" |
| 1012 |
reveal_type(none1) # N: Revealed type is "None" |
| 1013 |
reveal_type(none2) # N: Revealed type is "None" |
| 1014 |
reveal_type(none3) # N: Revealed type is "None" |
| 1015 |
[builtins fixtures/primitives.pyi] |
| 1016 |
|
| 1017 |
[case testLiteralInferredOnlyForActualLiterals] |
| 1018 |
from typing import Literal |
| 1019 |
|
| 1020 |
w: Literal[1] |
| 1021 |
x: Literal["foo"] |
| 1022 |
y: Literal[True] |
| 1023 |
z: Literal[None] |
| 1024 |
combined: Literal[1, "foo", True, None] |
| 1025 |
|
| 1026 |
a = 1 |
| 1027 |
b = "foo" |
| 1028 |
c = True |
| 1029 |
d = None |
| 1030 |
|
| 1031 |
w = a # E: Incompatible types in assignment (expression has type "int", variable has type "Literal[1]") |
| 1032 |
x = b # E: Incompatible types in assignment (expression has type "str", variable has type "Literal['foo']") |
| 1033 |
y = c # E: Incompatible types in assignment (expression has type "bool", variable has type "Literal[True]") |
| 1034 |
z = d # This is ok: Literal[None] and None are equivalent. |
| 1035 |
|
| 1036 |
combined = a # E: Incompatible types in assignment (expression has type "int", variable has type "Literal[1, 'foo', True] | None") |
| 1037 |
combined = b # E: Incompatible types in assignment (expression has type "str", variable has type "Literal[1, 'foo', True] | None") |
| 1038 |
combined = c # E: Incompatible types in assignment (expression has type "bool", variable has type "Literal[1, 'foo', True] | None") |
| 1039 |
combined = d # Also ok, for similar reasons. |
| 1040 |
|
| 1041 |
e: Literal[1] = 1 |
| 1042 |
f: Literal["foo"] = "foo" |
| 1043 |
g: Literal[True] = True |
| 1044 |
h: Literal[None] = None |
| 1045 |
|
| 1046 |
w = e |
| 1047 |
x = f |
| 1048 |
y = g |
| 1049 |
z = h |
| 1050 |
combined = e |
| 1051 |
combined = f |
| 1052 |
combined = g |
| 1053 |
combined = h |
| 1054 |
|
| 1055 |
[builtins fixtures/primitives.pyi] |
| 1056 |
|
| 1057 |
[case testLiteralInferredTypeMustMatchExpected] |
| 1058 |
from typing import Literal |
| 1059 |
|
| 1060 |
a: Literal[1] = 2 # E: Incompatible types in assignment (expression has type "Literal[2]", variable has type "Literal[1]") |
| 1061 |
b: Literal["foo"] = "bar" # E: Incompatible types in assignment (expression has type "Literal['bar']", variable has type "Literal['foo']") |
| 1062 |
c: Literal[True] = False # E: Incompatible types in assignment (expression has type "Literal[False]", variable has type "Literal[True]") |
| 1063 |
|
| 1064 |
d: Literal[1, 2] = 3 # E: Incompatible types in assignment (expression has type "Literal[3]", variable has type "Literal[1, 2]") |
| 1065 |
e: Literal["foo", "bar"] = "baz" # E: Incompatible types in assignment (expression has type "Literal['baz']", variable has type "Literal['foo', 'bar']") |
| 1066 |
f: Literal[True, 4] = False # E: Incompatible types in assignment (expression has type "Literal[False]", variable has type "Literal[True, 4]") |
| 1067 |
|
| 1068 |
[builtins fixtures/primitives.pyi] |
| 1069 |
|
| 1070 |
[case testLiteralInferredInCall] |
| 1071 |
from typing import Literal |
| 1072 |
|
| 1073 |
def f_int_lit(x: Literal[1]) -> None: pass |
| 1074 |
def f_int(x: int) -> None: pass |
| 1075 |
|
| 1076 |
def f_str_lit(x: Literal["foo"]) -> None: pass |
| 1077 |
def f_str(x: str) -> None: pass |
| 1078 |
|
| 1079 |
def f_bool_lit(x: Literal[True]) -> None: pass |
| 1080 |
def f_bool(x: bool) -> None: pass |
| 1081 |
|
| 1082 |
def f_none_lit(x: Literal[None]) -> None: pass |
| 1083 |
def f_none(x: None) -> None: pass |
| 1084 |
|
| 1085 |
i1: Literal[1] |
| 1086 |
i2: Literal[2] |
| 1087 |
f_int_lit(1) |
| 1088 |
f_int_lit(2) # E: Argument 1 to "f_int_lit" has incompatible type "Literal[2]"; expected "Literal[1]" |
| 1089 |
f_int(1) |
| 1090 |
f_int_lit(i1) |
| 1091 |
f_int_lit(i2) # E: Argument 1 to "f_int_lit" has incompatible type "Literal[2]"; expected "Literal[1]" |
| 1092 |
|
| 1093 |
s1: Literal["foo"] |
| 1094 |
s2: Literal["bar"] |
| 1095 |
f_str_lit("foo") |
| 1096 |
f_str_lit("bar") # E: Argument 1 to "f_str_lit" has incompatible type "Literal['bar']"; expected "Literal['foo']" |
| 1097 |
f_str("baz") |
| 1098 |
f_str_lit(s1) |
| 1099 |
f_str_lit(s2) # E: Argument 1 to "f_str_lit" has incompatible type "Literal['bar']"; expected "Literal['foo']" |
| 1100 |
|
| 1101 |
b1: Literal[True] |
| 1102 |
b2: Literal[False] |
| 1103 |
f_bool_lit(True) |
| 1104 |
f_bool_lit(False) # E: Argument 1 to "f_bool_lit" has incompatible type "Literal[False]"; expected "Literal[True]" |
| 1105 |
f_bool(True) |
| 1106 |
f_bool_lit(b1) |
| 1107 |
f_bool_lit(b2) # E: Argument 1 to "f_bool_lit" has incompatible type "Literal[False]"; expected "Literal[True]" |
| 1108 |
|
| 1109 |
n1: Literal[None] |
| 1110 |
f_none_lit(None) |
| 1111 |
f_none(None) |
| 1112 |
f_none_lit(n1) |
| 1113 |
[builtins fixtures/primitives.pyi] |
| 1114 |
|
| 1115 |
[case testLiteralInferredInReturnContext] |
| 1116 |
from typing import Literal |
| 1117 |
|
| 1118 |
def f1() -> int: |
| 1119 |
return 1 |
| 1120 |
|
| 1121 |
def f2() -> Literal[1]: |
| 1122 |
return 1 |
| 1123 |
|
| 1124 |
def f3() -> Literal[1]: |
| 1125 |
return 2 # E: Incompatible return value type (got "Literal[2]", expected "Literal[1]") |
| 1126 |
|
| 1127 |
def f4(x: Literal[1]) -> Literal[1]: |
| 1128 |
return x |
| 1129 |
|
| 1130 |
def f5(x: Literal[2]) -> Literal[1]: |
| 1131 |
return x # E: Incompatible return value type (got "Literal[2]", expected "Literal[1]") |
| 1132 |
[builtins fixtures/tuple.pyi] |
| 1133 |
|
| 1134 |
[out] |
| 1135 |
|
| 1136 |
[case testLiteralInferredInListContext] |
| 1137 |
from typing import List, Literal |
| 1138 |
|
| 1139 |
a: List[Literal[1]] = [1, 1, 1] |
| 1140 |
b = [1, 1, 1] |
| 1141 |
c: List[Literal[1, 2, 3]] = [1, 2, 3] |
| 1142 |
d = [1, 2, 3] |
| 1143 |
e: List[Literal[1, "x"]] = [1, "x"] |
| 1144 |
f = [1, "x"] |
| 1145 |
g: List[List[List[Literal[1, 2, 3]]]] = [[[1, 2, 3], [3]]] |
| 1146 |
h: List[Literal[1]] = [] |
| 1147 |
|
| 1148 |
reveal_type(a) # N: Revealed type is "builtins.list[Literal[1]]" |
| 1149 |
reveal_type(b) # N: Revealed type is "builtins.list[builtins.int]" |
| 1150 |
reveal_type(c) # N: Revealed type is "builtins.list[Literal[1] | Literal[2] | Literal[3]]" |
| 1151 |
reveal_type(d) # N: Revealed type is "builtins.list[builtins.int]" |
| 1152 |
reveal_type(e) # N: Revealed type is "builtins.list[Literal[1] | Literal['x']]" |
| 1153 |
reveal_type(f) # N: Revealed type is "builtins.list[builtins.object]" |
| 1154 |
reveal_type(g) # N: Revealed type is "builtins.list[builtins.list[builtins.list[Literal[1] | Literal[2] | Literal[3]]]]" |
| 1155 |
reveal_type(h) # N: Revealed type is "builtins.list[Literal[1]]" |
| 1156 |
|
| 1157 |
lit1: Literal[1] |
| 1158 |
lit2: Literal[2] |
| 1159 |
lit3: Literal["foo"] |
| 1160 |
|
| 1161 |
arr1 = [lit1, lit1, lit1] |
| 1162 |
arr2 = [lit1, lit2] |
| 1163 |
arr3 = [lit1, 4, 5] |
| 1164 |
arr4 = [lit1, lit2, lit3] |
| 1165 |
arr5 = [object(), lit1] |
| 1166 |
|
| 1167 |
reveal_type(arr1) # N: Revealed type is "builtins.list[Literal[1]]" |
| 1168 |
reveal_type(arr2) # N: Revealed type is "builtins.list[builtins.int]" |
| 1169 |
reveal_type(arr3) # N: Revealed type is "builtins.list[builtins.int]" |
| 1170 |
reveal_type(arr4) # N: Revealed type is "builtins.list[builtins.object]" |
| 1171 |
reveal_type(arr5) # N: Revealed type is "builtins.list[builtins.object]" |
| 1172 |
|
| 1173 |
bad: List[Literal[1, 2]] = [1, 2, 3] # E: List item 2 has incompatible type "Literal[3]"; expected "Literal[1, 2]" |
| 1174 |
|
| 1175 |
[builtins fixtures/list.pyi] |
| 1176 |
[out] |
| 1177 |
|
| 1178 |
[case testLiteralInferredInTupleContext] |
| 1179 |
# Note: most of the 'are we handling context correctly' tests should have been |
| 1180 |
# handled up above, so we keep things comparatively simple for tuples and dicts. |
| 1181 |
from typing import Literal, Tuple |
| 1182 |
|
| 1183 |
a: Tuple[Literal[1], Literal[2]] = (1, 2) |
| 1184 |
b: Tuple[int, Literal[1, 2], Literal[3], Tuple[Literal["foo"]]] = (1, 2, 3, ("foo",)) |
| 1185 |
c: Tuple[Literal[1], Literal[2]] = (2, 1) # E: Incompatible types in assignment (expression has type "tuple[Literal[2], Literal[1]]", variable has type "tuple[Literal[1], Literal[2]]") |
| 1186 |
d = (1, 2) |
| 1187 |
|
| 1188 |
reveal_type(d) # N: Revealed type is "tuple[builtins.int, builtins.int]" |
| 1189 |
|
| 1190 |
[builtins fixtures/tuple.pyi] |
| 1191 |
[out] |
| 1192 |
|
| 1193 |
[case testLiteralInferredInDictContext] |
| 1194 |
from typing import Dict, Literal |
| 1195 |
|
| 1196 |
a = {"x": 1, "y": 2} |
| 1197 |
b: Dict[str, Literal[1, 2]] = {"x": 1, "y": 2} |
| 1198 |
c: Dict[Literal["x", "y"], int] = {"x": 1, "y": 2} |
| 1199 |
|
| 1200 |
reveal_type(a) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]" |
| 1201 |
|
| 1202 |
[builtins fixtures/dict.pyi] |
| 1203 |
[out] |
| 1204 |
|
| 1205 |
[case testLiteralInferredInOverloadContextBasic] |
| 1206 |
from typing import Literal, overload |
| 1207 |
|
| 1208 |
@overload |
| 1209 |
def func(x: Literal[1]) -> str: ... |
| 1210 |
@overload |
| 1211 |
def func(x: Literal[2]) -> int: ... |
| 1212 |
@overload |
| 1213 |
def func(x: int) -> object: ... |
| 1214 |
def func(x: int) -> object: pass |
| 1215 |
|
| 1216 |
a: Literal[1] |
| 1217 |
b: Literal[2] |
| 1218 |
c: Literal[1, 2] |
| 1219 |
|
| 1220 |
reveal_type(func(1)) # N: Revealed type is "builtins.str" |
| 1221 |
reveal_type(func(2)) # N: Revealed type is "builtins.int" |
| 1222 |
reveal_type(func(3)) # N: Revealed type is "builtins.object" |
| 1223 |
reveal_type(func(a)) # N: Revealed type is "builtins.str" |
| 1224 |
reveal_type(func(b)) # N: Revealed type is "builtins.int" |
| 1225 |
|
| 1226 |
# Note: the fact that we don't do union math here is consistent |
| 1227 |
# with the output we would have gotten if we replaced int and the |
| 1228 |
# Literal types here with regular classes/subclasses. |
| 1229 |
reveal_type(func(c)) # N: Revealed type is "builtins.object" |
| 1230 |
[builtins fixtures/tuple.pyi] |
| 1231 |
[out] |
| 1232 |
|
| 1233 |
[case testLiteralOverloadProhibitUnsafeOverlaps] |
| 1234 |
from typing import Literal, overload |
| 1235 |
|
| 1236 |
@overload |
| 1237 |
def func1(x: Literal[1]) -> str: ... # E: Overloaded function signatures 1 and 2 overlap with incompatible return types |
| 1238 |
@overload |
| 1239 |
def func1(x: int) -> int: ... |
| 1240 |
def func1(x): pass |
| 1241 |
|
| 1242 |
@overload |
| 1243 |
def func2(x: Literal['a']) -> int: ... # E: Overloaded function signatures 1 and 2 overlap with incompatible return types |
| 1244 |
@overload |
| 1245 |
def func2(x: str) -> Literal[2]: ... |
| 1246 |
def func2(x): pass |
| 1247 |
|
| 1248 |
# This one is typesafe |
| 1249 |
@overload |
| 1250 |
def func3(x: Literal['a']) -> Literal[2]: ... |
| 1251 |
@overload |
| 1252 |
def func3(x: str) -> int: ... |
| 1253 |
def func3(x): pass |
| 1254 |
[builtins fixtures/tuple.pyi] |
| 1255 |
[out] |
| 1256 |
|
| 1257 |
[case testLiteralInferredInOverloadContextUnionMath] |
| 1258 |
from typing import overload, Literal, Union |
| 1259 |
|
| 1260 |
class A: pass |
| 1261 |
class B: pass |
| 1262 |
class C: pass |
| 1263 |
|
| 1264 |
@overload |
| 1265 |
def func(x: Literal[-40]) -> A: ... |
| 1266 |
@overload |
| 1267 |
def func(x: Literal[3, 4, 5, 6]) -> B: ... |
| 1268 |
@overload |
| 1269 |
def func(x: Literal["foo"]) -> C: ... |
| 1270 |
def func(x: Union[int, str]) -> Union[A, B, C]: pass |
| 1271 |
|
| 1272 |
a: Literal[-40, "foo"] |
| 1273 |
b: Literal[3] |
| 1274 |
c: Literal[3, -40] |
| 1275 |
d: Literal[6, 7] |
| 1276 |
e: int |
| 1277 |
f: Literal[7, "bar"] |
| 1278 |
|
| 1279 |
reveal_type(func(a)) # N: Revealed type is "__main__.A | __main__.C" |
| 1280 |
reveal_type(func(b)) # N: Revealed type is "__main__.B" |
| 1281 |
reveal_type(func(c)) # N: Revealed type is "__main__.B | __main__.A" |
| 1282 |
reveal_type(func(d)) # N: Revealed type is "__main__.B" \ |
| 1283 |
# E: Argument 1 to "func" has incompatible type "Literal[6, 7]"; expected "Literal[3, 4, 5, 6]" |
| 1284 |
|
| 1285 |
reveal_type(func(e)) # E: No overload variant of "func" matches argument type "int" \ |
| 1286 |
# N: Possible overload variants: \ |
| 1287 |
# N: def func(x: Literal[-40]) -> A \ |
| 1288 |
# N: def func(x: Literal[3, 4, 5, 6]) -> B \ |
| 1289 |
# N: def func(x: Literal['foo']) -> C \ |
| 1290 |
# N: Revealed type is "Any" |
| 1291 |
|
| 1292 |
reveal_type(func(f)) # E: No overload variant of "func" matches argument type "Literal[7, 'bar']" \ |
| 1293 |
# N: Possible overload variants: \ |
| 1294 |
# N: def func(x: Literal[-40]) -> A \ |
| 1295 |
# N: def func(x: Literal[3, 4, 5, 6]) -> B \ |
| 1296 |
# N: def func(x: Literal['foo']) -> C \ |
| 1297 |
# N: Revealed type is "Any" |
| 1298 |
[builtins fixtures/tuple.pyi] |
| 1299 |
[out] |
| 1300 |
|
| 1301 |
[case testLiteralInferredInOverloadContextUnionMathOverloadingReturnsBestType] |
| 1302 |
# This test is a transliteration of check-overloading::testUnionMathOverloadingReturnsBestType |
| 1303 |
from typing import Literal, overload |
| 1304 |
|
| 1305 |
@overload |
| 1306 |
def f(x: Literal[1, 2]) -> int: ... |
| 1307 |
@overload |
| 1308 |
def f(x: int) -> object: ... |
| 1309 |
def f(x): |
| 1310 |
pass |
| 1311 |
|
| 1312 |
x: Literal[1, 2] |
| 1313 |
y: Literal[1, 2, 3] |
| 1314 |
z: Literal[1, 2, "three"] |
| 1315 |
reveal_type(f(x)) # N: Revealed type is "builtins.int" |
| 1316 |
reveal_type(f(1)) # N: Revealed type is "builtins.int" |
| 1317 |
reveal_type(f(2)) # N: Revealed type is "builtins.int" |
| 1318 |
reveal_type(f(y)) # N: Revealed type is "builtins.object" |
| 1319 |
reveal_type(f(z)) # N: Revealed type is "builtins.int" \ |
| 1320 |
# E: Argument 1 to "f" has incompatible type "Literal[1, 2, 'three']"; expected "Literal[1, 2]" |
| 1321 |
[builtins fixtures/tuple.pyi] |
| 1322 |
[out] |
| 1323 |
|
| 1324 |
[case testLiteralInferredInOverloadContextWithTypevars] |
| 1325 |
from typing import Literal, TypeVar, overload, Union |
| 1326 |
|
| 1327 |
T = TypeVar('T') |
| 1328 |
|
| 1329 |
@overload |
| 1330 |
def f1(x: T, y: int) -> T: ... |
| 1331 |
@overload |
| 1332 |
def f1(x: T, y: str) -> Union[T, str]: ... |
| 1333 |
def f1(x, y): pass |
| 1334 |
|
| 1335 |
a: Literal[1] |
| 1336 |
reveal_type(f1(1, 1)) # N: Revealed type is "builtins.int" |
| 1337 |
reveal_type(f1(a, 1)) # N: Revealed type is "Literal[1]" |
| 1338 |
|
| 1339 |
@overload |
| 1340 |
def f2(x: T, y: Literal[3]) -> T: ... |
| 1341 |
@overload |
| 1342 |
def f2(x: T, y: str) -> Union[T]: ... |
| 1343 |
def f2(x, y): pass |
| 1344 |
|
| 1345 |
reveal_type(f2(1, 3)) # N: Revealed type is "builtins.int" |
| 1346 |
reveal_type(f2(a, 3)) # N: Revealed type is "Literal[1]" |
| 1347 |
|
| 1348 |
@overload |
| 1349 |
def f3(x: Literal[3]) -> Literal[3]: ... |
| 1350 |
@overload |
| 1351 |
def f3(x: T) -> T: ... |
| 1352 |
def f3(x): pass |
| 1353 |
|
| 1354 |
reveal_type(f3(1)) # N: Revealed type is "builtins.int" |
| 1355 |
reveal_type(f3(a)) # N: Revealed type is "Literal[1]" |
| 1356 |
|
| 1357 |
@overload |
| 1358 |
def f4(x: str) -> str: ... |
| 1359 |
@overload |
| 1360 |
def f4(x: T) -> T: ... |
| 1361 |
def f4(x): pass |
| 1362 |
|
| 1363 |
b: Literal['foo'] |
| 1364 |
reveal_type(f4(1)) # N: Revealed type is "builtins.int" |
| 1365 |
reveal_type(f4(a)) # N: Revealed type is "Literal[1]" |
| 1366 |
reveal_type(f4("foo")) # N: Revealed type is "builtins.str" |
| 1367 |
|
| 1368 |
# Note: first overload is selected and prevents the typevar from |
| 1369 |
# ever inferring a Literal["something"]. |
| 1370 |
reveal_type(f4(b)) # N: Revealed type is "builtins.str" |
| 1371 |
[builtins fixtures/tuple.pyi] |
| 1372 |
[out] |
| 1373 |
|
| 1374 |
[case testLiteralInferredInOverloadContextUnionMathTrickyOverload] |
| 1375 |
# This test is a transliteration of check-overloading::testUnionMathTrickyOverload1 |
| 1376 |
from typing import Literal, overload |
| 1377 |
|
| 1378 |
@overload |
| 1379 |
def f(x: Literal['a'], y: Literal['a']) -> int: ... |
| 1380 |
@overload |
| 1381 |
def f(x: str, y: Literal['b']) -> str: ... |
| 1382 |
def f(x): |
| 1383 |
pass |
| 1384 |
|
| 1385 |
x: Literal['a', 'b'] |
| 1386 |
y: Literal['a', 'b'] |
| 1387 |
f(x, y) # E: Argument 1 to "f" has incompatible type "Literal['a', 'b']"; expected "Literal['a']" \ |
| 1388 |
# E: Argument 2 to "f" has incompatible type "Literal['a', 'b']"; expected "Literal['a']" \ |
| 1389 |
[builtins fixtures/tuple.pyi] |
| 1390 |
[out] |
| 1391 |
|
| 1392 |
|
| 1393 |
--- |
| 1394 |
--- Tests that make sure we're correctly using the fallback |
| 1395 |
--- |
| 1396 |
|
| 1397 |
[case testLiteralFallbackOperatorsWorkCorrectly] |
| 1398 |
from typing import Literal |
| 1399 |
|
| 1400 |
a: Literal[3] |
| 1401 |
b: int |
| 1402 |
c: Literal[4] |
| 1403 |
d: Literal['foo'] |
| 1404 |
e: str |
| 1405 |
|
| 1406 |
reveal_type(a + a) # N: Revealed type is "builtins.int" |
| 1407 |
reveal_type(a + b) # N: Revealed type is "builtins.int" |
| 1408 |
reveal_type(b + a) # N: Revealed type is "builtins.int" |
| 1409 |
reveal_type(a + 1) # N: Revealed type is "builtins.int" |
| 1410 |
reveal_type(1 + a) # N: Revealed type is "builtins.int" |
| 1411 |
reveal_type(a + c) # N: Revealed type is "builtins.int" |
| 1412 |
reveal_type(c + a) # N: Revealed type is "builtins.int" |
| 1413 |
|
| 1414 |
reveal_type(d + d) # N: Revealed type is "builtins.str" |
| 1415 |
reveal_type(d + e) # N: Revealed type is "builtins.str" |
| 1416 |
reveal_type(e + d) # N: Revealed type is "builtins.str" |
| 1417 |
reveal_type(d + 'foo') # N: Revealed type is "builtins.str" |
| 1418 |
reveal_type('foo' + d) # N: Revealed type is "builtins.str" |
| 1419 |
|
| 1420 |
reveal_type(a.__add__(b)) # N: Revealed type is "builtins.int" |
| 1421 |
reveal_type(b.__add__(a)) # N: Revealed type is "builtins.int" |
| 1422 |
|
| 1423 |
a *= b # E: Incompatible types in assignment (expression has type "int", variable has type "Literal[3]") |
| 1424 |
b *= a |
| 1425 |
|
| 1426 |
reveal_type(b) # N: Revealed type is "builtins.int" |
| 1427 |
[builtins fixtures/primitives.pyi] |
| 1428 |
|
| 1429 |
[case testLiteralFallbackInheritedMethodsWorkCorrectly] |
| 1430 |
from typing import Literal |
| 1431 |
a: Literal['foo'] |
| 1432 |
b: str |
| 1433 |
|
| 1434 |
reveal_type(a.startswith(a)) # N: Revealed type is "builtins.bool" |
| 1435 |
reveal_type(b.startswith(a)) # N: Revealed type is "builtins.bool" |
| 1436 |
reveal_type(a.startswith(b)) # N: Revealed type is "builtins.bool" |
| 1437 |
reveal_type(a.strip()) # N: Revealed type is "builtins.str" |
| 1438 |
[builtins fixtures/ops.pyi] |
| 1439 |
[out] |
| 1440 |
|
| 1441 |
[case testLiteralFallbackMethodsDoNotCoerceToLiteral] |
| 1442 |
from typing import Literal |
| 1443 |
|
| 1444 |
a: Literal[3] |
| 1445 |
b: int |
| 1446 |
c: Literal["foo"] |
| 1447 |
|
| 1448 |
if int(): |
| 1449 |
a = a * a # E: Incompatible types in assignment (expression has type "int", variable has type "Literal[3]") |
| 1450 |
a = a * b # E: Incompatible types in assignment (expression has type "int", variable has type "Literal[3]") |
| 1451 |
a = b * a # E: Incompatible types in assignment (expression has type "int", variable has type "Literal[3]") |
| 1452 |
|
| 1453 |
b = a * a |
| 1454 |
b = a * b |
| 1455 |
b = b * a |
| 1456 |
|
| 1457 |
c = c.strip() # E: Incompatible types in assignment (expression has type "str", variable has type "Literal['foo']") |
| 1458 |
[builtins fixtures/ops.pyi] |
| 1459 |
[out] |
| 1460 |
|
| 1461 |
|
| 1462 |
-- |
| 1463 |
-- Tests that check we report errors when we try using Literal[...] |
| 1464 |
-- in invalid places. |
| 1465 |
-- |
| 1466 |
|
| 1467 |
[case testLiteralErrorsWithIsInstanceAndIsSubclass] |
| 1468 |
from typing_extensions import Literal |
| 1469 |
from typing_extensions import Literal as Renamed |
| 1470 |
import typing_extensions as indirect |
| 1471 |
|
| 1472 |
Alias = Literal[3] |
| 1473 |
|
| 1474 |
isinstance(3, Literal[3]) # E: Cannot use isinstance() with Literal type |
| 1475 |
isinstance(3, Alias) # E: Cannot use isinstance() with Literal type \ |
| 1476 |
# E: Argument 2 to "isinstance" has incompatible type "<typing special form>"; expected "type | tuple[Any, ...]" |
| 1477 |
isinstance(3, Renamed[3]) # E: Cannot use isinstance() with Literal type |
| 1478 |
isinstance(3, indirect.Literal[3]) # E: Cannot use isinstance() with Literal type |
| 1479 |
|
| 1480 |
issubclass(int, Literal[3]) # E: Cannot use issubclass() with Literal type |
| 1481 |
issubclass(int, Alias) # E: Cannot use issubclass() with Literal type \ |
| 1482 |
# E: Argument 2 to "issubclass" has incompatible type "<typing special form>"; expected "type | tuple[Any, ...]" |
| 1483 |
issubclass(int, Renamed[3]) # E: Cannot use issubclass() with Literal type |
| 1484 |
issubclass(int, indirect.Literal[3]) # E: Cannot use issubclass() with Literal type |
| 1485 |
[builtins fixtures/isinstancelist.pyi] |
| 1486 |
[typing fixtures/typing-medium.pyi] |
| 1487 |
[out] |
| 1488 |
|
| 1489 |
[case testLiteralErrorsWhenSubclassed] |
| 1490 |
from typing import Literal |
| 1491 |
from typing import Literal as Renamed |
| 1492 |
import typing as indirect |
| 1493 |
|
| 1494 |
Alias = Literal[3] |
| 1495 |
|
| 1496 |
class Bad1(Literal[3]): pass # E: Invalid base class "Literal" |
| 1497 |
class Bad2(Renamed[3]): pass # E: Invalid base class "Renamed" |
| 1498 |
class Bad3(indirect.Literal[3]): pass # E: Invalid base class "indirect.Literal" |
| 1499 |
class Bad4(Alias): pass # E: Invalid base class "Alias" |
| 1500 |
[builtins fixtures/tuple.pyi] |
| 1501 |
[out] |
| 1502 |
|
| 1503 |
[case testLiteralErrorsWhenInvoked-skip] |
| 1504 |
# TODO: We don't seem to correctly handle invoking types like |
| 1505 |
# 'Final' and 'Protocol' as well. When fixing this, also fix |
| 1506 |
# those types? |
| 1507 |
from typing import Literal |
| 1508 |
from typing import Literal as Renamed |
| 1509 |
import typing as indirect |
| 1510 |
|
| 1511 |
Alias = Literal[3] |
| 1512 |
|
| 1513 |
Literal[3]() # E: The type "Type[Literal]" is not generic and not indexable |
| 1514 |
Renamed[3]() # E: The type "Type[Literal]" is not generic and not indexable |
| 1515 |
indirect.Literal[3]() # E: The type "Type[Literal]" is not generic and not indexable |
| 1516 |
Alias() # E: "object" not callable |
| 1517 |
|
| 1518 |
# TODO: Add appropriate error messages to the following lines |
| 1519 |
Literal() |
| 1520 |
Renamed() |
| 1521 |
indirect.Literal() |
| 1522 |
[builtins fixtures/isinstancelist.pyi] |
| 1523 |
[out] |
| 1524 |
|
| 1525 |
|
| 1526 |
-- |
| 1527 |
-- Test to make sure literals interact with generics as expected |
| 1528 |
-- |
| 1529 |
|
| 1530 |
[case testLiteralAndGenericsWithSimpleFunctions] |
| 1531 |
from typing import Literal, TypeVar |
| 1532 |
|
| 1533 |
T = TypeVar('T') |
| 1534 |
def foo(x: T) -> T: pass |
| 1535 |
def expects_literal(x: Literal[3]) -> None: pass |
| 1536 |
def expects_int(x: int) -> None: pass |
| 1537 |
|
| 1538 |
a: Literal[3] |
| 1539 |
reveal_type(foo(3)) # N: Revealed type is "builtins.int" |
| 1540 |
reveal_type(foo(a)) # N: Revealed type is "Literal[3]" |
| 1541 |
|
| 1542 |
expects_literal(3) |
| 1543 |
expects_literal(foo(3)) |
| 1544 |
expects_literal(foo(foo(3))) |
| 1545 |
|
| 1546 |
expects_literal(a) |
| 1547 |
expects_literal(foo(a)) |
| 1548 |
expects_literal(foo(foo(a))) |
| 1549 |
|
| 1550 |
expects_literal(5) # E: Argument 1 to "expects_literal" has incompatible type "Literal[5]"; expected "Literal[3]" |
| 1551 |
expects_literal(foo(5)) # E: Argument 1 to "foo" has incompatible type "Literal[5]"; expected "Literal[3]" |
| 1552 |
expects_literal(foo(foo(5))) # E: Argument 1 to "foo" has incompatible type "Literal[5]"; expected "Literal[3]" |
| 1553 |
|
| 1554 |
expects_int(a) |
| 1555 |
expects_int(foo(a)) |
| 1556 |
expects_int(foo(foo(a))) |
| 1557 |
[builtins fixtures/tuple.pyi] |
| 1558 |
[out] |
| 1559 |
|
| 1560 |
[case testLiteralAndGenericWithUnion] |
| 1561 |
from typing import Literal, TypeVar, Union |
| 1562 |
|
| 1563 |
T = TypeVar('T') |
| 1564 |
def identity(x: T) -> T: return x |
| 1565 |
|
| 1566 |
a: Union[int, Literal['foo']] = identity('foo') |
| 1567 |
b: Union[int, Literal['foo']] = identity('bar') # E: Argument 1 to "identity" has incompatible type "Literal['bar']"; expected "int | Literal['foo']" |
| 1568 |
[builtins fixtures/tuple.pyi] |
| 1569 |
[out] |
| 1570 |
|
| 1571 |
[case testLiteralAndGenericsNoMatch] |
| 1572 |
from typing import Literal, TypeVar, Union, List |
| 1573 |
|
| 1574 |
def identity(x: T) -> T: |
| 1575 |
return x |
| 1576 |
|
| 1577 |
Ok1 = Union[List[int], Literal['bad']] |
| 1578 |
Ok2 = Union[List[Literal[42]], Literal['bad']] |
| 1579 |
Bad = Union[List[Literal[43]], Literal['bad']] |
| 1580 |
|
| 1581 |
x: Ok1 = identity([42]) |
| 1582 |
y: Ok2 = identity([42]) |
| 1583 |
z: Bad = identity([42]) # E: List item 0 has incompatible type "Literal[42]"; expected "Literal[43]" |
| 1584 |
[builtins fixtures/list.pyi] |
| 1585 |
[out] |
| 1586 |
|
| 1587 |
[case testLiteralAndGenericsWithSimpleClasses] |
| 1588 |
from typing import Literal, TypeVar, Generic |
| 1589 |
|
| 1590 |
T = TypeVar('T') |
| 1591 |
class Wrapper(Generic[T]): |
| 1592 |
def __init__(self, val: T) -> None: |
| 1593 |
self.val = val |
| 1594 |
def inner(self) -> T: |
| 1595 |
return self.val |
| 1596 |
|
| 1597 |
def expects_literal(a: Literal[3]) -> None: pass |
| 1598 |
def expects_literal_wrapper(x: Wrapper[Literal[3]]) -> None: pass |
| 1599 |
|
| 1600 |
a: Literal[3] |
| 1601 |
reveal_type(Wrapper(3)) # N: Revealed type is "__main__.Wrapper[builtins.int]" |
| 1602 |
reveal_type(Wrapper[Literal[3]](3)) # N: Revealed type is "__main__.Wrapper[Literal[3]]" |
| 1603 |
reveal_type(Wrapper(a)) # N: Revealed type is "__main__.Wrapper[Literal[3]]" |
| 1604 |
|
| 1605 |
expects_literal(Wrapper(a).inner()) |
| 1606 |
|
| 1607 |
# Note: the following probably ought to type-check: it's reasonable to infer |
| 1608 |
# Wrapper[Literal[3]] here. |
| 1609 |
# TODO: Consider finding a way to handle this edge case better |
| 1610 |
expects_literal(Wrapper(3).inner()) # E: Argument 1 to "expects_literal" has incompatible type "int"; expected "Literal[3]" |
| 1611 |
|
| 1612 |
# Note: if we handle the edge case above, we should make sure this error |
| 1613 |
# message switches to warning about an incompatible type 'Literal[5]' rather |
| 1614 |
# then an incompatible type 'int' |
| 1615 |
expects_literal(Wrapper(5).inner()) # E: Argument 1 to "expects_literal" has incompatible type "int"; expected "Literal[3]" |
| 1616 |
|
| 1617 |
expects_literal_wrapper(Wrapper(a)) |
| 1618 |
expects_literal_wrapper(Wrapper(3)) |
| 1619 |
expects_literal_wrapper(Wrapper(5)) # E: Argument 1 to "Wrapper" has incompatible type "Literal[5]"; expected "Literal[3]" |
| 1620 |
[builtins fixtures/tuple.pyi] |
| 1621 |
[out] |
| 1622 |
|
| 1623 |
[case testLiteralAndGenericsRespectsUpperBound] |
| 1624 |
from typing import Literal, TypeVar |
| 1625 |
|
| 1626 |
TLiteral = TypeVar('TLiteral', bound=Literal[3]) |
| 1627 |
TInt = TypeVar('TInt', bound=int) |
| 1628 |
|
| 1629 |
def func1(x: TLiteral) -> TLiteral: pass |
| 1630 |
def func2(x: TInt) -> TInt: pass |
| 1631 |
|
| 1632 |
def func3(x: TLiteral) -> TLiteral: |
| 1633 |
y = func2(x) |
| 1634 |
return y |
| 1635 |
def func4(x: TInt) -> TInt: |
| 1636 |
y = func1(x) # E: Value of type variable "TLiteral" of "func1" cannot be "TInt" |
| 1637 |
return y |
| 1638 |
|
| 1639 |
a: Literal[3] |
| 1640 |
b: Literal[4] |
| 1641 |
c: int |
| 1642 |
|
| 1643 |
reveal_type(func1) # N: Revealed type is "def [TLiteral <: Literal[3]] (x: TLiteral`-1) -> TLiteral`-1" |
| 1644 |
|
| 1645 |
reveal_type(func1(3)) # N: Revealed type is "Literal[3]" |
| 1646 |
reveal_type(func1(a)) # N: Revealed type is "Literal[3]" |
| 1647 |
reveal_type(func1(4)) # E: Value of type variable "TLiteral" of "func1" cannot be "Literal[4]" \ |
| 1648 |
# N: Revealed type is "Literal[4]" |
| 1649 |
reveal_type(func1(b)) # E: Value of type variable "TLiteral" of "func1" cannot be "Literal[4]" \ |
| 1650 |
# N: Revealed type is "Literal[4]" |
| 1651 |
reveal_type(func1(c)) # E: Value of type variable "TLiteral" of "func1" cannot be "int" \ |
| 1652 |
# N: Revealed type is "builtins.int" |
| 1653 |
|
| 1654 |
reveal_type(func2(3)) # N: Revealed type is "builtins.int" |
| 1655 |
reveal_type(func2(a)) # N: Revealed type is "Literal[3]" |
| 1656 |
reveal_type(func2(4)) # N: Revealed type is "builtins.int" |
| 1657 |
reveal_type(func2(b)) # N: Revealed type is "Literal[4]" |
| 1658 |
reveal_type(func2(c)) # N: Revealed type is "builtins.int" |
| 1659 |
[builtins fixtures/tuple.pyi] |
| 1660 |
[out] |
| 1661 |
|
| 1662 |
[case testLiteralAndGenericsRespectsValueRestriction] |
| 1663 |
from typing import Literal, TypeVar |
| 1664 |
|
| 1665 |
TLiteral = TypeVar('TLiteral', Literal[3], Literal['foo']) |
| 1666 |
TNormal = TypeVar('TNormal', int, str) |
| 1667 |
|
| 1668 |
def func1(x: TLiteral) -> TLiteral: pass |
| 1669 |
def func2(x: TNormal) -> TNormal: pass |
| 1670 |
|
| 1671 |
def func3(x: TLiteral) -> TLiteral: |
| 1672 |
y = func2(x) |
| 1673 |
return y # E: Incompatible return value type (got "int", expected "Literal[3]") \ |
| 1674 |
# E: Incompatible return value type (got "str", expected "Literal['foo']") |
| 1675 |
def func4(x: TNormal) -> TNormal: |
| 1676 |
y = func1(x) # E: Value of type variable "TLiteral" of "func1" cannot be "int" \ |
| 1677 |
# E: Value of type variable "TLiteral" of "func1" cannot be "str" |
| 1678 |
return y |
| 1679 |
|
| 1680 |
i1: Literal[3] |
| 1681 |
i2: Literal[4] |
| 1682 |
i: int |
| 1683 |
|
| 1684 |
s1: Literal['foo'] |
| 1685 |
s2: Literal['bar'] |
| 1686 |
s: str |
| 1687 |
|
| 1688 |
reveal_type(func1) # N: Revealed type is "def [TLiteral in (Literal[3], Literal['foo'])] (x: TLiteral`-1) -> TLiteral`-1" |
| 1689 |
|
| 1690 |
reveal_type(func1(3)) # N: Revealed type is "Literal[3]" |
| 1691 |
reveal_type(func1(i1)) # N: Revealed type is "Literal[3]" |
| 1692 |
reveal_type(func1(4)) # E: Value of type variable "TLiteral" of "func1" cannot be "Literal[4]" \ |
| 1693 |
# N: Revealed type is "Literal[4]" |
| 1694 |
reveal_type(func1(i2)) # E: Value of type variable "TLiteral" of "func1" cannot be "Literal[4]" \ |
| 1695 |
# N: Revealed type is "Literal[4]" |
| 1696 |
reveal_type(func1(i)) # E: Value of type variable "TLiteral" of "func1" cannot be "int" \ |
| 1697 |
# N: Revealed type is "builtins.int" |
| 1698 |
reveal_type(func1("foo")) # N: Revealed type is "Literal['foo']" |
| 1699 |
reveal_type(func1(s1)) # N: Revealed type is "Literal['foo']" |
| 1700 |
reveal_type(func1("bar")) # E: Value of type variable "TLiteral" of "func1" cannot be "Literal['bar']" \ |
| 1701 |
# N: Revealed type is "Literal['bar']" |
| 1702 |
reveal_type(func1(s2)) # E: Value of type variable "TLiteral" of "func1" cannot be "Literal['bar']" \ |
| 1703 |
# N: Revealed type is "Literal['bar']" |
| 1704 |
reveal_type(func1(s)) # E: Value of type variable "TLiteral" of "func1" cannot be "str" \ |
| 1705 |
# N: Revealed type is "builtins.str" |
| 1706 |
|
| 1707 |
reveal_type(func2(3)) # N: Revealed type is "builtins.int" |
| 1708 |
reveal_type(func2(i1)) # N: Revealed type is "builtins.int" |
| 1709 |
reveal_type(func2(4)) # N: Revealed type is "builtins.int" |
| 1710 |
reveal_type(func2(i2)) # N: Revealed type is "builtins.int" |
| 1711 |
reveal_type(func2("foo")) # N: Revealed type is "builtins.str" |
| 1712 |
reveal_type(func2(s1)) # N: Revealed type is "builtins.str" |
| 1713 |
reveal_type(func2("bar")) # N: Revealed type is "builtins.str" |
| 1714 |
reveal_type(func2(s2)) # N: Revealed type is "builtins.str" |
| 1715 |
[builtins fixtures/tuple.pyi] |
| 1716 |
[out] |
| 1717 |
|
| 1718 |
[case testLiteralAndGenericsWithOverloads] |
| 1719 |
from typing import Literal, TypeVar, overload, Union |
| 1720 |
|
| 1721 |
@overload |
| 1722 |
def func1(x: Literal[4]) -> Literal[19]: ... |
| 1723 |
@overload |
| 1724 |
def func1(x: int) -> int: ... |
| 1725 |
def func1(x: int) -> int: pass |
| 1726 |
|
| 1727 |
T = TypeVar('T') |
| 1728 |
def identity(x: T) -> T: pass |
| 1729 |
|
| 1730 |
a: Literal[4] |
| 1731 |
b: Literal[5] |
| 1732 |
|
| 1733 |
reveal_type(func1(identity(4))) # N: Revealed type is "Literal[19]" |
| 1734 |
reveal_type(func1(identity(5))) # N: Revealed type is "builtins.int" |
| 1735 |
reveal_type(func1(identity(a))) # N: Revealed type is "Literal[19]" |
| 1736 |
reveal_type(func1(identity(b))) # N: Revealed type is "builtins.int" |
| 1737 |
[builtins fixtures/tuple.pyi] |
| 1738 |
|
| 1739 |
-- |
| 1740 |
-- Interactions with meets |
| 1741 |
-- |
| 1742 |
|
| 1743 |
[case testLiteralMeets] |
| 1744 |
from typing import TypeVar, List, Literal, Callable, Union, Optional |
| 1745 |
|
| 1746 |
a: Callable[[Literal[1]], int] |
| 1747 |
b: Callable[[Literal[2]], str] |
| 1748 |
c: Callable[[int], str] |
| 1749 |
d: Callable[[object], str] |
| 1750 |
e: Callable[[Union[Literal[1], Literal[2]]], str] |
| 1751 |
|
| 1752 |
arr1 = [a, a] |
| 1753 |
arr2 = [a, b] |
| 1754 |
arr3 = [a, c] |
| 1755 |
arr4 = [a, d] |
| 1756 |
arr5 = [a, e] |
| 1757 |
|
| 1758 |
reveal_type(arr1) # N: Revealed type is "builtins.list[def (Literal[1]) -> builtins.int]" |
| 1759 |
reveal_type(arr2) # N: Revealed type is "builtins.list[builtins.function]" |
| 1760 |
reveal_type(arr3) # N: Revealed type is "builtins.list[def (Literal[1]) -> builtins.object]" |
| 1761 |
reveal_type(arr4) # N: Revealed type is "builtins.list[def (Literal[1]) -> builtins.object]" |
| 1762 |
reveal_type(arr5) # N: Revealed type is "builtins.list[def (Literal[1]) -> builtins.object]" |
| 1763 |
|
| 1764 |
# Inspect just only one interesting one |
| 1765 |
lit: Literal[1] |
| 1766 |
reveal_type(arr2[0](lit)) # E: Cannot call function of unknown type \ |
| 1767 |
# N: Revealed type is "Any" |
| 1768 |
|
| 1769 |
T = TypeVar('T') |
| 1770 |
def unify(func: Callable[[T, T], None]) -> T: pass |
| 1771 |
|
| 1772 |
def f1(x: Literal[1], y: Literal[1]) -> None: pass |
| 1773 |
def f2(x: Literal[1], y: Literal[2]) -> None: pass |
| 1774 |
def f3(x: Literal[1], y: int) -> None: pass |
| 1775 |
def f4(x: Literal[1], y: object) -> None: pass |
| 1776 |
def f5(x: Literal[1], y: Union[Literal[1], Literal[2]]) -> None: pass |
| 1777 |
def f6(x: Optional[Literal[1]], y: Optional[Literal[2]]) -> None: pass |
| 1778 |
|
| 1779 |
reveal_type(unify(f1)) # N: Revealed type is "Literal[1]" |
| 1780 |
if object(): |
| 1781 |
reveal_type(unify(f2)) # N: Revealed type is "Never" |
| 1782 |
reveal_type(unify(f3)) # N: Revealed type is "Literal[1]" |
| 1783 |
reveal_type(unify(f4)) # N: Revealed type is "Literal[1]" |
| 1784 |
reveal_type(unify(f5)) # N: Revealed type is "Literal[1]" |
| 1785 |
reveal_type(unify(f6)) # N: Revealed type is "None" |
| 1786 |
[builtins fixtures/list.pyi] |
| 1787 |
[out] |
| 1788 |
|
| 1789 |
[case testLiteralMeetsWithStrictOptional] |
| 1790 |
from typing import TypeVar, Callable, Literal, Union |
| 1791 |
|
| 1792 |
a: Callable[[Literal[1]], int] |
| 1793 |
b: Callable[[Literal[2]], str] |
| 1794 |
lit: Literal[1] |
| 1795 |
|
| 1796 |
arr = [a, b] |
| 1797 |
reveal_type(arr) # N: Revealed type is "builtins.list[builtins.function]" |
| 1798 |
reveal_type(arr[0](lit)) # E: Cannot call function of unknown type \ |
| 1799 |
# N: Revealed type is "Any" |
| 1800 |
|
| 1801 |
T = TypeVar('T') |
| 1802 |
def unify(func: Callable[[T, T], None]) -> T: pass |
| 1803 |
def func(x: Literal[1], y: Literal[2]) -> None: pass |
| 1804 |
|
| 1805 |
reveal_type(unify(func)) # N: Revealed type is "Never" |
| 1806 |
[builtins fixtures/list.pyi] |
| 1807 |
[out] |
| 1808 |
|
| 1809 |
|
| 1810 |
-- |
| 1811 |
-- Checks for intelligent indexing |
| 1812 |
-- |
| 1813 |
|
| 1814 |
[case testLiteralIntelligentIndexingTuples] |
| 1815 |
from typing import Literal, Tuple, NamedTuple, Optional, Final |
| 1816 |
|
| 1817 |
class A: pass |
| 1818 |
class B: pass |
| 1819 |
class C: pass |
| 1820 |
class D: pass |
| 1821 |
class E: pass |
| 1822 |
|
| 1823 |
idx0: Literal[0] |
| 1824 |
idx1: Literal[1] |
| 1825 |
idx2: Literal[2] |
| 1826 |
idx3: Literal[3] |
| 1827 |
idx4: Literal[4] |
| 1828 |
idx5: Literal[5] |
| 1829 |
idx_neg1: Literal[-1] |
| 1830 |
idx_final: Final = 2 |
| 1831 |
|
| 1832 |
tup1: Tuple[A, B, Optional[C], D, E] |
| 1833 |
reveal_type(tup1[idx0]) # N: Revealed type is "__main__.A" |
| 1834 |
reveal_type(tup1[idx1]) # N: Revealed type is "__main__.B" |
| 1835 |
reveal_type(tup1[idx2]) # N: Revealed type is "__main__.C | None" |
| 1836 |
reveal_type(tup1[idx_final]) # N: Revealed type is "__main__.C | None" |
| 1837 |
reveal_type(tup1[idx3]) # N: Revealed type is "__main__.D" |
| 1838 |
reveal_type(tup1[idx4]) # N: Revealed type is "__main__.E" |
| 1839 |
reveal_type(tup1[idx_neg1]) # N: Revealed type is "__main__.E" |
| 1840 |
tup1[idx5] # E: Tuple index out of range |
| 1841 |
reveal_type(tup1[idx2:idx4]) # N: Revealed type is "tuple[__main__.C | None, __main__.D]" |
| 1842 |
reveal_type(tup1[::idx2]) # N: Revealed type is "tuple[__main__.A, __main__.C | None, __main__.E]" |
| 1843 |
if tup1[idx2] is not None: |
| 1844 |
reveal_type(tup1[idx2]) # N: Revealed type is "__main__.C | None" |
| 1845 |
if tup1[idx_final] is not None: |
| 1846 |
reveal_type(tup1[idx_final]) # N: Revealed type is "__main__.C" |
| 1847 |
|
| 1848 |
Tup2Class = NamedTuple('Tup2Class', [('a', A), ('b', B), ('c', C), ('d', D), ('e', E)]) |
| 1849 |
tup2: Tup2Class |
| 1850 |
reveal_type(tup2[idx0]) # N: Revealed type is "__main__.A" |
| 1851 |
reveal_type(tup2[idx1]) # N: Revealed type is "__main__.B" |
| 1852 |
reveal_type(tup2[idx2]) # N: Revealed type is "__main__.C" |
| 1853 |
reveal_type(tup2[idx3]) # N: Revealed type is "__main__.D" |
| 1854 |
reveal_type(tup2[idx4]) # N: Revealed type is "__main__.E" |
| 1855 |
reveal_type(tup2[idx_neg1]) # N: Revealed type is "__main__.E" |
| 1856 |
tup2[idx5] # E: Tuple index out of range |
| 1857 |
reveal_type(tup2[idx2:idx4]) # N: Revealed type is "tuple[__main__.C, __main__.D]" |
| 1858 |
reveal_type(tup2[::idx2]) # N: Revealed type is "tuple[__main__.A, __main__.C, __main__.E]" |
| 1859 |
tup3: Tup2Class = tup2[:] # E: Incompatible types in assignment (expression has type "tuple[A, B, C, D, E]", variable has type "Tup2Class") |
| 1860 |
[builtins fixtures/slice.pyi] |
| 1861 |
|
| 1862 |
[case testLiteralIntelligentIndexingTypedDict] |
| 1863 |
from typing import Literal, TypedDict |
| 1864 |
|
| 1865 |
class Unrelated: pass |
| 1866 |
u: Unrelated |
| 1867 |
|
| 1868 |
class Inner(TypedDict): |
| 1869 |
a: int |
| 1870 |
class Outer(Inner, total=False): |
| 1871 |
b: str |
| 1872 |
|
| 1873 |
a_key: Literal["a"] |
| 1874 |
b_key: Literal["b"] |
| 1875 |
c_key: Literal["c"] |
| 1876 |
|
| 1877 |
d: Outer |
| 1878 |
|
| 1879 |
reveal_type(d[a_key]) # N: Revealed type is "builtins.int" |
| 1880 |
reveal_type(d[b_key]) # N: Revealed type is "builtins.str" |
| 1881 |
d[c_key] # E: TypedDict "Outer" has no key "c" |
| 1882 |
|
| 1883 |
reveal_type(d.get(a_key, u)) # N: Revealed type is "builtins.int" |
| 1884 |
reveal_type(d.get(b_key, u)) # N: Revealed type is "builtins.str | __main__.Unrelated" |
| 1885 |
reveal_type(d.get(c_key, u)) # N: Revealed type is "builtins.object" |
| 1886 |
|
| 1887 |
reveal_type(d.pop(a_key)) # N: Revealed type is "builtins.int" \ |
| 1888 |
# E: Key "a" of TypedDict "Outer" cannot be deleted |
| 1889 |
|
| 1890 |
reveal_type(d.pop(b_key)) # N: Revealed type is "builtins.str" |
| 1891 |
d.pop(c_key) # E: TypedDict "Outer" has no key "c" |
| 1892 |
|
| 1893 |
del d[a_key] # E: Key "a" of TypedDict "Outer" cannot be deleted |
| 1894 |
del d[b_key] |
| 1895 |
del d[c_key] # E: TypedDict "Outer" has no key "c" |
| 1896 |
[builtins fixtures/dict.pyi] |
| 1897 |
[typing fixtures/typing-typeddict.pyi] |
| 1898 |
[out] |
| 1899 |
|
| 1900 |
[case testLiteralIntelligentIndexingUsingFinal] |
| 1901 |
from typing import Final, Literal, Tuple, NamedTuple, TypedDict |
| 1902 |
|
| 1903 |
int_key_good: Final = 0 |
| 1904 |
int_key_bad: Final = 3 |
| 1905 |
str_key_good: Final = "foo" |
| 1906 |
str_key_bad: Final = "missing" |
| 1907 |
|
| 1908 |
class Unrelated: pass |
| 1909 |
|
| 1910 |
MyTuple = NamedTuple('MyTuple', [ |
| 1911 |
('foo', int), |
| 1912 |
('bar', str), |
| 1913 |
]) |
| 1914 |
|
| 1915 |
class MyDict(TypedDict): |
| 1916 |
foo: int |
| 1917 |
bar: str |
| 1918 |
|
| 1919 |
a: Tuple[int, str] |
| 1920 |
b: MyTuple |
| 1921 |
c: MyDict |
| 1922 |
u: Unrelated |
| 1923 |
|
| 1924 |
reveal_type(a[int_key_good]) # N: Revealed type is "builtins.int" |
| 1925 |
reveal_type(b[int_key_good]) # N: Revealed type is "builtins.int" |
| 1926 |
reveal_type(c[str_key_good]) # N: Revealed type is "builtins.int" |
| 1927 |
reveal_type(c.get(str_key_good, u)) # N: Revealed type is "builtins.int" |
| 1928 |
reveal_type(c.get(str_key_bad, u)) # N: Revealed type is "builtins.object" |
| 1929 |
|
| 1930 |
a[int_key_bad] # E: Tuple index out of range |
| 1931 |
b[int_key_bad] # E: Tuple index out of range |
| 1932 |
c[str_key_bad] # E: TypedDict "MyDict" has no key "missing" |
| 1933 |
[builtins fixtures/dict.pyi] |
| 1934 |
[typing fixtures/typing-typeddict.pyi] |
| 1935 |
[out] |
| 1936 |
|
| 1937 |
[case testLiteralIntelligentIndexingTupleUnions] |
| 1938 |
from typing import Literal, Tuple, NamedTuple |
| 1939 |
|
| 1940 |
class A: pass |
| 1941 |
class B: pass |
| 1942 |
class C: pass |
| 1943 |
class D: pass |
| 1944 |
class E: pass |
| 1945 |
|
| 1946 |
idx1: Literal[1, 2] |
| 1947 |
idx2: Literal[3, 4] |
| 1948 |
idx_bad: Literal[1, 20] |
| 1949 |
|
| 1950 |
tup1: Tuple[A, B, C, D, E] |
| 1951 |
Tup2Class = NamedTuple('Tup2Class', [('a', A), ('b', B), ('c', C), ('d', D), ('e', E)]) |
| 1952 |
tup2: Tup2Class |
| 1953 |
|
| 1954 |
reveal_type(tup1[idx1]) # N: Revealed type is "__main__.B | __main__.C" |
| 1955 |
reveal_type(tup1[idx1:idx2]) # N: Revealed type is "tuple[__main__.B, __main__.C] | tuple[__main__.B, __main__.C, __main__.D] | tuple[__main__.C] | tuple[__main__.C, __main__.D]" |
| 1956 |
reveal_type(tup1[0::idx1]) # N: Revealed type is "tuple[__main__.A, __main__.B, __main__.C, __main__.D, __main__.E] | tuple[__main__.A, __main__.C, __main__.E]" |
| 1957 |
tup1[idx_bad] # E: Tuple index out of range |
| 1958 |
|
| 1959 |
reveal_type(tup2[idx1]) # N: Revealed type is "__main__.B | __main__.C" |
| 1960 |
reveal_type(tup2[idx1:idx2]) # N: Revealed type is "tuple[__main__.B, __main__.C] | tuple[__main__.B, __main__.C, __main__.D] | tuple[__main__.C] | tuple[__main__.C, __main__.D]" |
| 1961 |
reveal_type(tup2[0::idx1]) # N: Revealed type is "tuple[__main__.A, __main__.B, __main__.C, __main__.D, __main__.E] | tuple[__main__.A, __main__.C, __main__.E]" |
| 1962 |
tup2[idx_bad] # E: Tuple index out of range |
| 1963 |
[builtins fixtures/slice.pyi] |
| 1964 |
[out] |
| 1965 |
|
| 1966 |
[case testLiteralIntelligentIndexingTypedDictUnions] |
| 1967 |
from typing import Final, Literal, TypedDict |
| 1968 |
|
| 1969 |
class A: pass |
| 1970 |
class B: pass |
| 1971 |
class C: pass |
| 1972 |
class D: pass |
| 1973 |
class E: pass |
| 1974 |
|
| 1975 |
class Base(TypedDict): |
| 1976 |
a: A |
| 1977 |
b: B |
| 1978 |
c: C |
| 1979 |
|
| 1980 |
class Test(Base, total=False): |
| 1981 |
d: D |
| 1982 |
e: E |
| 1983 |
|
| 1984 |
class AAndB(A, B): pass |
| 1985 |
|
| 1986 |
test: Test |
| 1987 |
good_keys: Literal["a", "b"] |
| 1988 |
optional_keys: Literal["d", "e"] |
| 1989 |
bad_keys: Literal["a", "bad"] |
| 1990 |
|
| 1991 |
reveal_type(test[good_keys]) # N: Revealed type is "__main__.A | __main__.B" |
| 1992 |
reveal_type(test.get(good_keys)) # N: Revealed type is "__main__.A | __main__.B" |
| 1993 |
reveal_type(test.get(good_keys, 3)) # N: Revealed type is "__main__.A | __main__.B" |
| 1994 |
reveal_type(test.pop(optional_keys)) # N: Revealed type is "__main__.D | __main__.E" |
| 1995 |
reveal_type(test.pop(optional_keys, 3)) # N: Revealed type is "__main__.D | __main__.E | Literal[3]?" |
| 1996 |
reveal_type(test.setdefault(good_keys, AAndB())) # N: Revealed type is "__main__.A | __main__.B" |
| 1997 |
reveal_type(test.get(bad_keys)) # N: Revealed type is "builtins.object" |
| 1998 |
reveal_type(test.get(bad_keys, 3)) # N: Revealed type is "builtins.object" |
| 1999 |
del test[optional_keys] |
| 2000 |
|
| 2001 |
|
| 2002 |
test[bad_keys] # E: TypedDict "Test" has no key "bad" |
| 2003 |
test.pop(good_keys) # E: Key "a" of TypedDict "Test" cannot be deleted \ |
| 2004 |
# E: Key "b" of TypedDict "Test" cannot be deleted |
| 2005 |
test.pop(bad_keys) # E: Key "a" of TypedDict "Test" cannot be deleted \ |
| 2006 |
# E: TypedDict "Test" has no key "bad" |
| 2007 |
test.setdefault(good_keys, 3) # E: Argument 2 to "setdefault" of "TypedDict" has incompatible type "int"; expected "A" |
| 2008 |
test.setdefault(bad_keys, 3 ) # E: Argument 2 to "setdefault" of "TypedDict" has incompatible type "int"; expected "A" |
| 2009 |
del test[good_keys] # E: Key "a" of TypedDict "Test" cannot be deleted \ |
| 2010 |
# E: Key "b" of TypedDict "Test" cannot be deleted |
| 2011 |
del test[bad_keys] # E: Key "a" of TypedDict "Test" cannot be deleted \ |
| 2012 |
# E: TypedDict "Test" has no key "bad" |
| 2013 |
[builtins fixtures/dict.pyi] |
| 2014 |
[typing fixtures/typing-typeddict.pyi] |
| 2015 |
[out] |
| 2016 |
|
| 2017 |
[case testLiteralIntelligentIndexingMultiTypedDict] |
| 2018 |
from typing import Literal, TypedDict, Union |
| 2019 |
|
| 2020 |
class A: pass |
| 2021 |
class B: pass |
| 2022 |
class C: pass |
| 2023 |
class D: pass |
| 2024 |
|
| 2025 |
class D1(TypedDict): |
| 2026 |
a: A |
| 2027 |
b: B |
| 2028 |
c: C |
| 2029 |
|
| 2030 |
class D2(TypedDict): |
| 2031 |
b: B |
| 2032 |
c: C |
| 2033 |
d: D |
| 2034 |
|
| 2035 |
x: Union[D1, D2] |
| 2036 |
good_keys: Literal['b', 'c'] |
| 2037 |
mixed_keys: Literal['a', 'b', 'c', 'd'] |
| 2038 |
bad_keys: Literal['e', 'f'] |
| 2039 |
|
| 2040 |
x[mixed_keys] # E: TypedDict "D1" has no key "d" \ |
| 2041 |
# E: TypedDict "D2" has no key "a" |
| 2042 |
|
| 2043 |
reveal_type(x[good_keys]) # N: Revealed type is "__main__.B | __main__.C" |
| 2044 |
reveal_type(x.get(good_keys)) # N: Revealed type is "__main__.B | __main__.C" |
| 2045 |
reveal_type(x.get(good_keys, 3)) # N: Revealed type is "__main__.B | __main__.C" |
| 2046 |
reveal_type(x.get(mixed_keys)) # N: Revealed type is "builtins.object" |
| 2047 |
reveal_type(x.get(mixed_keys, 3)) # N: Revealed type is "builtins.object" |
| 2048 |
reveal_type(x.get(bad_keys)) # N: Revealed type is "builtins.object" |
| 2049 |
reveal_type(x.get(bad_keys, 3)) # N: Revealed type is "builtins.object" |
| 2050 |
|
| 2051 |
[builtins fixtures/dict.pyi] |
| 2052 |
[typing fixtures/typing-typeddict.pyi] |
| 2053 |
|
| 2054 |
-- |
| 2055 |
-- Interactions with 'Final' |
| 2056 |
-- |
| 2057 |
|
| 2058 |
[case testLiteralFinalInferredAsLiteral] |
| 2059 |
from typing import Final, Literal |
| 2060 |
|
| 2061 |
var1: Final = 1 |
| 2062 |
var2: Final = "foo" |
| 2063 |
var3: Final = True |
| 2064 |
var4: Final = None |
| 2065 |
|
| 2066 |
class Foo: |
| 2067 |
classvar1: Final = 1 |
| 2068 |
classvar2: Final = "foo" |
| 2069 |
classvar3: Final = True |
| 2070 |
classvar4: Final = None |
| 2071 |
|
| 2072 |
def __init__(self) -> None: |
| 2073 |
self.instancevar1: Final = 1 |
| 2074 |
self.instancevar2: Final = "foo" |
| 2075 |
self.instancevar3: Final = True |
| 2076 |
self.instancevar4: Final = None |
| 2077 |
|
| 2078 |
def force1(x: Literal[1]) -> None: pass |
| 2079 |
def force2(x: Literal["foo"]) -> None: pass |
| 2080 |
def force3(x: Literal[True]) -> None: pass |
| 2081 |
def force4(x: Literal[None]) -> None: pass |
| 2082 |
|
| 2083 |
reveal_type(var1) # N: Revealed type is "Literal[1]?" |
| 2084 |
reveal_type(var2) # N: Revealed type is "Literal['foo']?" |
| 2085 |
reveal_type(var3) # N: Revealed type is "Literal[True]?" |
| 2086 |
reveal_type(var4) # N: Revealed type is "None" |
| 2087 |
force1(reveal_type(var1)) # N: Revealed type is "Literal[1]" |
| 2088 |
force2(reveal_type(var2)) # N: Revealed type is "Literal['foo']" |
| 2089 |
force3(reveal_type(var3)) # N: Revealed type is "Literal[True]" |
| 2090 |
force4(reveal_type(var4)) # N: Revealed type is "None" |
| 2091 |
|
| 2092 |
reveal_type(Foo.classvar1) # N: Revealed type is "Literal[1]?" |
| 2093 |
reveal_type(Foo.classvar2) # N: Revealed type is "Literal['foo']?" |
| 2094 |
reveal_type(Foo.classvar3) # N: Revealed type is "Literal[True]?" |
| 2095 |
reveal_type(Foo.classvar4) # N: Revealed type is "None" |
| 2096 |
force1(reveal_type(Foo.classvar1)) # N: Revealed type is "Literal[1]" |
| 2097 |
force2(reveal_type(Foo.classvar2)) # N: Revealed type is "Literal['foo']" |
| 2098 |
force3(reveal_type(Foo.classvar3)) # N: Revealed type is "Literal[True]" |
| 2099 |
force4(reveal_type(Foo.classvar4)) # N: Revealed type is "None" |
| 2100 |
|
| 2101 |
f = Foo() |
| 2102 |
reveal_type(f.instancevar1) # N: Revealed type is "Literal[1]?" |
| 2103 |
reveal_type(f.instancevar2) # N: Revealed type is "Literal['foo']?" |
| 2104 |
reveal_type(f.instancevar3) # N: Revealed type is "Literal[True]?" |
| 2105 |
reveal_type(f.instancevar4) # N: Revealed type is "None" |
| 2106 |
force1(reveal_type(f.instancevar1)) # N: Revealed type is "Literal[1]" |
| 2107 |
force2(reveal_type(f.instancevar2)) # N: Revealed type is "Literal['foo']" |
| 2108 |
force3(reveal_type(f.instancevar3)) # N: Revealed type is "Literal[True]" |
| 2109 |
force4(reveal_type(f.instancevar4)) # N: Revealed type is "None" |
| 2110 |
[builtins fixtures/primitives.pyi] |
| 2111 |
[out] |
| 2112 |
|
| 2113 |
[case testLiteralFinalDirectInstanceTypesSupersedeInferredLiteral] |
| 2114 |
from typing import Final, Literal |
| 2115 |
|
| 2116 |
var1: Final[int] = 1 |
| 2117 |
var2: Final[str] = "foo" |
| 2118 |
var3: Final[bool] = True |
| 2119 |
var4: Final[None] = None |
| 2120 |
|
| 2121 |
class Foo: |
| 2122 |
classvar1: Final[int] = 1 |
| 2123 |
classvar2: Final[str] = "foo" |
| 2124 |
classvar3: Final[bool] = True |
| 2125 |
classvar4: Final[None] = None |
| 2126 |
|
| 2127 |
def __init__(self) -> None: |
| 2128 |
self.instancevar1: Final[int] = 1 |
| 2129 |
self.instancevar2: Final[str] = "foo" |
| 2130 |
self.instancevar3: Final[bool] = True |
| 2131 |
self.instancevar4: Final[None] = None |
| 2132 |
|
| 2133 |
def force1(x: Literal[1]) -> None: pass |
| 2134 |
def force2(x: Literal["foo"]) -> None: pass |
| 2135 |
def force3(x: Literal[True]) -> None: pass |
| 2136 |
def force4(x: Literal[None]) -> None: pass |
| 2137 |
|
| 2138 |
reveal_type(var1) # N: Revealed type is "builtins.int" |
| 2139 |
reveal_type(var2) # N: Revealed type is "builtins.str" |
| 2140 |
reveal_type(var3) # N: Revealed type is "builtins.bool" |
| 2141 |
reveal_type(var4) # N: Revealed type is "None" |
| 2142 |
force1(var1) # E: Argument 1 to "force1" has incompatible type "int"; expected "Literal[1]" |
| 2143 |
force2(var2) # E: Argument 1 to "force2" has incompatible type "str"; expected "Literal['foo']" |
| 2144 |
force3(var3) # E: Argument 1 to "force3" has incompatible type "bool"; expected "Literal[True]" |
| 2145 |
force4(var4) |
| 2146 |
|
| 2147 |
reveal_type(Foo.classvar1) # N: Revealed type is "builtins.int" |
| 2148 |
reveal_type(Foo.classvar2) # N: Revealed type is "builtins.str" |
| 2149 |
reveal_type(Foo.classvar3) # N: Revealed type is "builtins.bool" |
| 2150 |
reveal_type(Foo.classvar4) # N: Revealed type is "None" |
| 2151 |
force1(Foo.classvar1) # E: Argument 1 to "force1" has incompatible type "int"; expected "Literal[1]" |
| 2152 |
force2(Foo.classvar2) # E: Argument 1 to "force2" has incompatible type "str"; expected "Literal['foo']" |
| 2153 |
force3(Foo.classvar3) # E: Argument 1 to "force3" has incompatible type "bool"; expected "Literal[True]" |
| 2154 |
force4(Foo.classvar4) |
| 2155 |
|
| 2156 |
f = Foo() |
| 2157 |
reveal_type(f.instancevar1) # N: Revealed type is "builtins.int" |
| 2158 |
reveal_type(f.instancevar2) # N: Revealed type is "builtins.str" |
| 2159 |
reveal_type(f.instancevar3) # N: Revealed type is "builtins.bool" |
| 2160 |
reveal_type(f.instancevar4) # N: Revealed type is "None" |
| 2161 |
force1(f.instancevar1) # E: Argument 1 to "force1" has incompatible type "int"; expected "Literal[1]" |
| 2162 |
force2(f.instancevar2) # E: Argument 1 to "force2" has incompatible type "str"; expected "Literal['foo']" |
| 2163 |
force3(f.instancevar3) # E: Argument 1 to "force3" has incompatible type "bool"; expected "Literal[True]" |
| 2164 |
force4(f.instancevar4) |
| 2165 |
[builtins fixtures/primitives.pyi] |
| 2166 |
[out] |
| 2167 |
|
| 2168 |
[case testLiteralFinalDirectLiteralTypesForceLiteral] |
| 2169 |
from typing import Final, Literal |
| 2170 |
|
| 2171 |
var1: Final[Literal[1]] = 1 |
| 2172 |
var2: Final[Literal["foo"]] = "foo" |
| 2173 |
var3: Final[Literal[True]] = True |
| 2174 |
var4: Final[Literal[None]] = None |
| 2175 |
|
| 2176 |
class Foo: |
| 2177 |
classvar1: Final[Literal[1]] = 1 |
| 2178 |
classvar2: Final[Literal["foo"]] = "foo" |
| 2179 |
classvar3: Final[Literal[True]] = True |
| 2180 |
classvar4: Final[Literal[None]] = None |
| 2181 |
|
| 2182 |
def __init__(self) -> None: |
| 2183 |
self.instancevar1: Final[Literal[1]] = 1 |
| 2184 |
self.instancevar2: Final[Literal["foo"]] = "foo" |
| 2185 |
self.instancevar3: Final[Literal[True]] = True |
| 2186 |
self.instancevar4: Final[Literal[None]] = None |
| 2187 |
|
| 2188 |
def force1(x: Literal[1]) -> None: pass |
| 2189 |
def force2(x: Literal["foo"]) -> None: pass |
| 2190 |
def force3(x: Literal[True]) -> None: pass |
| 2191 |
def force4(x: Literal[None]) -> None: pass |
| 2192 |
|
| 2193 |
reveal_type(var1) # N: Revealed type is "Literal[1]" |
| 2194 |
reveal_type(var2) # N: Revealed type is "Literal['foo']" |
| 2195 |
reveal_type(var3) # N: Revealed type is "Literal[True]" |
| 2196 |
reveal_type(var4) # N: Revealed type is "None" |
| 2197 |
force1(reveal_type(var1)) # N: Revealed type is "Literal[1]" |
| 2198 |
force2(reveal_type(var2)) # N: Revealed type is "Literal['foo']" |
| 2199 |
force3(reveal_type(var3)) # N: Revealed type is "Literal[True]" |
| 2200 |
force4(reveal_type(var4)) # N: Revealed type is "None" |
| 2201 |
|
| 2202 |
reveal_type(Foo.classvar1) # N: Revealed type is "Literal[1]" |
| 2203 |
reveal_type(Foo.classvar2) # N: Revealed type is "Literal['foo']" |
| 2204 |
reveal_type(Foo.classvar3) # N: Revealed type is "Literal[True]" |
| 2205 |
reveal_type(Foo.classvar4) # N: Revealed type is "None" |
| 2206 |
force1(reveal_type(Foo.classvar1)) # N: Revealed type is "Literal[1]" |
| 2207 |
force2(reveal_type(Foo.classvar2)) # N: Revealed type is "Literal['foo']" |
| 2208 |
force3(reveal_type(Foo.classvar3)) # N: Revealed type is "Literal[True]" |
| 2209 |
force4(reveal_type(Foo.classvar4)) # N: Revealed type is "None" |
| 2210 |
|
| 2211 |
f = Foo() |
| 2212 |
reveal_type(f.instancevar1) # N: Revealed type is "Literal[1]" |
| 2213 |
reveal_type(f.instancevar2) # N: Revealed type is "Literal['foo']" |
| 2214 |
reveal_type(f.instancevar3) # N: Revealed type is "Literal[True]" |
| 2215 |
reveal_type(f.instancevar4) # N: Revealed type is "None" |
| 2216 |
force1(reveal_type(f.instancevar1)) # N: Revealed type is "Literal[1]" |
| 2217 |
force2(reveal_type(f.instancevar2)) # N: Revealed type is "Literal['foo']" |
| 2218 |
force3(reveal_type(f.instancevar3)) # N: Revealed type is "Literal[True]" |
| 2219 |
force4(reveal_type(f.instancevar4)) # N: Revealed type is "None" |
| 2220 |
[builtins fixtures/primitives.pyi] |
| 2221 |
[out] |
| 2222 |
|
| 2223 |
[case testLiteralFinalErasureInMutableDatastructures1] |
| 2224 |
from typing import Final |
| 2225 |
|
| 2226 |
var1: Final = [0, None] |
| 2227 |
var2: Final = (0, None) |
| 2228 |
|
| 2229 |
reveal_type(var1) # N: Revealed type is "builtins.list[builtins.int | None]" |
| 2230 |
reveal_type(var2) # N: Revealed type is "tuple[Literal[0]?, None]" |
| 2231 |
[builtins fixtures/tuple.pyi] |
| 2232 |
|
| 2233 |
[case testLiteralFinalErasureInMutableDatastructures2] |
| 2234 |
from typing import Final, Literal |
| 2235 |
|
| 2236 |
var1: Final = [] |
| 2237 |
var1.append(0) |
| 2238 |
reveal_type(var1) # N: Revealed type is "builtins.list[builtins.int]" |
| 2239 |
|
| 2240 |
var2 = [] |
| 2241 |
var2.append(0) |
| 2242 |
reveal_type(var2) # N: Revealed type is "builtins.list[builtins.int]" |
| 2243 |
|
| 2244 |
x: Literal[0] = 0 |
| 2245 |
var3 = [] |
| 2246 |
var3.append(x) |
| 2247 |
reveal_type(var3) # N: Revealed type is "builtins.list[Literal[0]]" |
| 2248 |
|
| 2249 |
[builtins fixtures/list.pyi] |
| 2250 |
|
| 2251 |
[case testLiteralFinalMismatchCausesError] |
| 2252 |
from typing import Final, Literal |
| 2253 |
|
| 2254 |
var1: Final[Literal[4]] = 1 # E: Incompatible types in assignment (expression has type "Literal[1]", variable has type "Literal[4]") |
| 2255 |
var2: Final[Literal['bad']] = "foo" # E: Incompatible types in assignment (expression has type "Literal['foo']", variable has type "Literal['bad']") |
| 2256 |
var3: Final[Literal[False]] = True # E: Incompatible types in assignment (expression has type "Literal[True]", variable has type "Literal[False]") |
| 2257 |
|
| 2258 |
class Foo: |
| 2259 |
classvar1: Final[Literal[4]] = 1 # E: Incompatible types in assignment (expression has type "Literal[1]", variable has type "Literal[4]") |
| 2260 |
classvar2: Final[Literal['bad']] = "foo" # E: Incompatible types in assignment (expression has type "Literal['foo']", variable has type "Literal['bad']") |
| 2261 |
classvar3: Final[Literal[False]] = True # E: Incompatible types in assignment (expression has type "Literal[True]", variable has type "Literal[False]") |
| 2262 |
|
| 2263 |
def __init__(self) -> None: |
| 2264 |
self.instancevar1: Final[Literal[4]] = 1 # E: Incompatible types in assignment (expression has type "Literal[1]", variable has type "Literal[4]") |
| 2265 |
self.instancevar2: Final[Literal['bad']] = "foo" # E: Incompatible types in assignment (expression has type "Literal['foo']", variable has type "Literal['bad']") |
| 2266 |
self.instancevar3: Final[Literal[False]] = True # E: Incompatible types in assignment (expression has type "Literal[True]", variable has type "Literal[False]") |
| 2267 |
|
| 2268 |
# TODO: Fix the order in which these error messages are shown to be more consistent. |
| 2269 |
var1 = 10 # E: Cannot assign to final name "var1" \ |
| 2270 |
# E: Incompatible types in assignment (expression has type "Literal[10]", variable has type "Literal[4]") |
| 2271 |
|
| 2272 |
|
| 2273 |
Foo.classvar1 = 10 # E: Cannot assign to final attribute "classvar1" \ |
| 2274 |
# E: Incompatible types in assignment (expression has type "Literal[10]", variable has type "Literal[4]") |
| 2275 |
|
| 2276 |
Foo().instancevar1 = 10 # E: Cannot assign to final attribute "instancevar1" \ |
| 2277 |
# E: Incompatible types in assignment (expression has type "Literal[10]", variable has type "Literal[4]") |
| 2278 |
[builtins fixtures/primitives.pyi] |
| 2279 |
[out] |
| 2280 |
|
| 2281 |
[case testLiteralFinalGoesOnlyOneLevelDown] |
| 2282 |
from typing import Final, Literal, Tuple |
| 2283 |
|
| 2284 |
a: Final = 1 |
| 2285 |
b: Final = (1, 2) |
| 2286 |
|
| 2287 |
def force1(x: Literal[1]) -> None: pass |
| 2288 |
def force2(x: Tuple[Literal[1], Literal[2]]) -> None: pass |
| 2289 |
|
| 2290 |
reveal_type(a) # N: Revealed type is "Literal[1]?" |
| 2291 |
reveal_type(b) # N: Revealed type is "tuple[Literal[1]?, Literal[2]?]" |
| 2292 |
|
| 2293 |
force1(a) # ok |
| 2294 |
force2(b) # ok |
| 2295 |
[builtins fixtures/tuple.pyi] |
| 2296 |
[out] |
| 2297 |
|
| 2298 |
[case testLiteralFinalCollectionPropagation] |
| 2299 |
from typing import Final, List, Literal |
| 2300 |
|
| 2301 |
a: Final = 1 |
| 2302 |
implicit = [a] |
| 2303 |
explicit: List[Literal[1]] = [a] |
| 2304 |
direct = [1] |
| 2305 |
|
| 2306 |
def force1(x: List[Literal[1]]) -> None: pass |
| 2307 |
def force2(x: Literal[1]) -> None: pass |
| 2308 |
|
| 2309 |
reveal_type(implicit) # N: Revealed type is "builtins.list[builtins.int]" |
| 2310 |
force1(reveal_type(implicit)) # E: Argument 1 to "force1" has incompatible type "list[int]"; expected "list[Literal[1]]" \ |
| 2311 |
# N: Revealed type is "builtins.list[builtins.int]" |
| 2312 |
force2(reveal_type(implicit[0])) # E: Argument 1 to "force2" has incompatible type "int"; expected "Literal[1]" \ |
| 2313 |
# N: Revealed type is "builtins.int" |
| 2314 |
|
| 2315 |
reveal_type(explicit) # N: Revealed type is "builtins.list[Literal[1]]" |
| 2316 |
force1(reveal_type(explicit)) # N: Revealed type is "builtins.list[Literal[1]]" |
| 2317 |
force2(reveal_type(explicit[0])) # N: Revealed type is "Literal[1]" |
| 2318 |
|
| 2319 |
reveal_type(direct) # N: Revealed type is "builtins.list[builtins.int]" |
| 2320 |
force1(reveal_type(direct)) # E: Argument 1 to "force1" has incompatible type "list[int]"; expected "list[Literal[1]]" \ |
| 2321 |
# N: Revealed type is "builtins.list[builtins.int]" |
| 2322 |
force2(reveal_type(direct[0])) # E: Argument 1 to "force2" has incompatible type "int"; expected "Literal[1]" \ |
| 2323 |
# N: Revealed type is "builtins.int" |
| 2324 |
[builtins fixtures/list.pyi] |
| 2325 |
[out] |
| 2326 |
|
| 2327 |
[case testLiteralFinalStringTypesPython3] |
| 2328 |
from typing import Final, Literal |
| 2329 |
|
| 2330 |
a: Final = u"foo" |
| 2331 |
b: Final = "foo" |
| 2332 |
c: Final = b"foo" |
| 2333 |
|
| 2334 |
def force_unicode(x: Literal[u"foo"]) -> None: pass |
| 2335 |
def force_bytes(x: Literal[b"foo"]) -> None: pass |
| 2336 |
|
| 2337 |
force_unicode(reveal_type(a)) # N: Revealed type is "Literal['foo']" |
| 2338 |
force_unicode(reveal_type(b)) # N: Revealed type is "Literal['foo']" |
| 2339 |
force_unicode(reveal_type(c)) # E: Argument 1 to "force_unicode" has incompatible type "Literal[b'foo']"; expected "Literal['foo']" \ |
| 2340 |
# N: Revealed type is "Literal[b'foo']" |
| 2341 |
|
| 2342 |
force_bytes(reveal_type(a)) # E: Argument 1 to "force_bytes" has incompatible type "Literal['foo']"; expected "Literal[b'foo']" \ |
| 2343 |
# N: Revealed type is "Literal['foo']" |
| 2344 |
force_bytes(reveal_type(b)) # E: Argument 1 to "force_bytes" has incompatible type "Literal['foo']"; expected "Literal[b'foo']" \ |
| 2345 |
# N: Revealed type is "Literal['foo']" |
| 2346 |
force_bytes(reveal_type(c)) # N: Revealed type is "Literal[b'foo']" |
| 2347 |
[builtins fixtures/tuple.pyi] |
| 2348 |
[out] |
| 2349 |
|
| 2350 |
[case testLiteralFinalPropagatesThroughGenerics] |
| 2351 |
from typing import TypeVar, Generic, Final, Literal |
| 2352 |
|
| 2353 |
|
| 2354 |
T = TypeVar('T') |
| 2355 |
|
| 2356 |
class WrapperClass(Generic[T]): |
| 2357 |
def __init__(self, data: T) -> None: |
| 2358 |
self.data = data |
| 2359 |
|
| 2360 |
def wrapper_func(x: T) -> T: |
| 2361 |
return x |
| 2362 |
|
| 2363 |
def force(x: Literal[99]) -> None: pass |
| 2364 |
def over_int(x: WrapperClass[int]) -> None: pass |
| 2365 |
def over_literal(x: WrapperClass[Literal[99]]) -> None: pass |
| 2366 |
|
| 2367 |
var1: Final = 99 |
| 2368 |
w1 = WrapperClass(var1) |
| 2369 |
force(reveal_type(w1.data)) # E: Argument 1 to "force" has incompatible type "int"; expected "Literal[99]" \ |
| 2370 |
# N: Revealed type is "builtins.int" |
| 2371 |
force(reveal_type(WrapperClass(var1).data)) # E: Argument 1 to "force" has incompatible type "int"; expected "Literal[99]" \ |
| 2372 |
# N: Revealed type is "builtins.int" |
| 2373 |
force(reveal_type(wrapper_func(var1))) # N: Revealed type is "Literal[99]" |
| 2374 |
over_int(reveal_type(w1)) # N: Revealed type is "__main__.WrapperClass[builtins.int]" |
| 2375 |
over_literal(reveal_type(w1)) # E: Argument 1 to "over_literal" has incompatible type "WrapperClass[int]"; expected "WrapperClass[Literal[99]]" \ |
| 2376 |
# N: Revealed type is "__main__.WrapperClass[builtins.int]" |
| 2377 |
over_int(reveal_type(WrapperClass(var1))) # N: Revealed type is "__main__.WrapperClass[builtins.int]" |
| 2378 |
over_literal(reveal_type(WrapperClass(var1))) # N: Revealed type is "__main__.WrapperClass[Literal[99]]" |
| 2379 |
|
| 2380 |
w2 = WrapperClass(99) |
| 2381 |
force(reveal_type(w2.data)) # E: Argument 1 to "force" has incompatible type "int"; expected "Literal[99]" \ |
| 2382 |
# N: Revealed type is "builtins.int" |
| 2383 |
force(reveal_type(WrapperClass(99).data)) # E: Argument 1 to "force" has incompatible type "int"; expected "Literal[99]" \ |
| 2384 |
# N: Revealed type is "builtins.int" |
| 2385 |
force(reveal_type(wrapper_func(99))) # N: Revealed type is "Literal[99]" |
| 2386 |
over_int(reveal_type(w2)) # N: Revealed type is "__main__.WrapperClass[builtins.int]" |
| 2387 |
over_literal(reveal_type(w2)) # E: Argument 1 to "over_literal" has incompatible type "WrapperClass[int]"; expected "WrapperClass[Literal[99]]" \ |
| 2388 |
# N: Revealed type is "__main__.WrapperClass[builtins.int]" |
| 2389 |
over_int(reveal_type(WrapperClass(99))) # N: Revealed type is "__main__.WrapperClass[builtins.int]" |
| 2390 |
over_literal(reveal_type(WrapperClass(99))) # N: Revealed type is "__main__.WrapperClass[Literal[99]]" |
| 2391 |
|
| 2392 |
var3: Literal[99] = 99 |
| 2393 |
w3 = WrapperClass(var3) |
| 2394 |
force(reveal_type(w3.data)) # N: Revealed type is "Literal[99]" |
| 2395 |
force(reveal_type(WrapperClass(var3).data)) # N: Revealed type is "Literal[99]" |
| 2396 |
force(reveal_type(wrapper_func(var3))) # N: Revealed type is "Literal[99]" |
| 2397 |
over_int(reveal_type(w3)) # E: Argument 1 to "over_int" has incompatible type "WrapperClass[Literal[99]]"; expected "WrapperClass[int]" \ |
| 2398 |
# N: Revealed type is "__main__.WrapperClass[Literal[99]]" |
| 2399 |
over_literal(reveal_type(w3)) # N: Revealed type is "__main__.WrapperClass[Literal[99]]" |
| 2400 |
over_int(reveal_type(WrapperClass(var3))) # N: Revealed type is "__main__.WrapperClass[builtins.int]" |
| 2401 |
over_literal(reveal_type(WrapperClass(var3))) # N: Revealed type is "__main__.WrapperClass[Literal[99]]" |
| 2402 |
[builtins fixtures/tuple.pyi] |
| 2403 |
[out] |
| 2404 |
|
| 2405 |
[case testLiteralFinalUsedInLiteralType] |
| 2406 |
from typing import Final, Literal |
| 2407 |
a: Final[int] = 3 |
| 2408 |
b: Final = 3 |
| 2409 |
c: Final[Literal[3]] = 3 |
| 2410 |
d: Literal[3] |
| 2411 |
|
| 2412 |
a_wrap: Literal[4, a] # E: Parameter 2 of Literal[...] is invalid |
| 2413 |
b_wrap: Literal[4, b] # E: Parameter 2 of Literal[...] is invalid |
| 2414 |
c_wrap: Literal[4, c] # E: Parameter 2 of Literal[...] is invalid |
| 2415 |
d_wrap: Literal[4, d] # E: Parameter 2 of Literal[...] is invalid |
| 2416 |
[builtins fixtures/tuple.pyi] |
| 2417 |
[out] |
| 2418 |
|
| 2419 |
[case testLiteralWithFinalPropagation] |
| 2420 |
from typing import Final, Literal |
| 2421 |
|
| 2422 |
a: Final = 3 |
| 2423 |
b: Final = a |
| 2424 |
c = a |
| 2425 |
|
| 2426 |
def expect_3(x: Literal[3]) -> None: pass |
| 2427 |
expect_3(a) |
| 2428 |
expect_3(b) |
| 2429 |
expect_3(c) # E: Argument 1 to "expect_3" has incompatible type "int"; expected "Literal[3]" |
| 2430 |
[builtins fixtures/tuple.pyi] |
| 2431 |
[out] |
| 2432 |
|
| 2433 |
[case testLiteralWithFinalPropagationIsNotLeaking] |
| 2434 |
from typing import Final, Literal |
| 2435 |
|
| 2436 |
final_tuple_direct: Final = (2, 3) |
| 2437 |
final_tuple_indirect: Final = final_tuple_direct |
| 2438 |
mutable_tuple = final_tuple_direct |
| 2439 |
final_list_1: Final = [2] |
| 2440 |
final_list_2: Final = [2, 2] |
| 2441 |
final_dict: Final = {"foo": 2} |
| 2442 |
final_set_1: Final = {2} |
| 2443 |
final_set_2: Final = {2, 2} |
| 2444 |
|
| 2445 |
def expect_2(x: Literal[2]) -> None: pass |
| 2446 |
|
| 2447 |
expect_2(final_tuple_direct[0]) |
| 2448 |
expect_2(final_tuple_indirect[0]) |
| 2449 |
|
| 2450 |
expect_2(mutable_tuple[0]) # E: Argument 1 to "expect_2" has incompatible type "int"; expected "Literal[2]" |
| 2451 |
expect_2(final_list_1[0]) # E: Argument 1 to "expect_2" has incompatible type "int"; expected "Literal[2]" |
| 2452 |
expect_2(final_list_2[0]) # E: Argument 1 to "expect_2" has incompatible type "int"; expected "Literal[2]" |
| 2453 |
expect_2(final_dict["foo"]) # E: Argument 1 to "expect_2" has incompatible type "int"; expected "Literal[2]" |
| 2454 |
expect_2(final_set_1.pop()) # E: Argument 1 to "expect_2" has incompatible type "int"; expected "Literal[2]" |
| 2455 |
expect_2(final_set_2.pop()) # E: Argument 1 to "expect_2" has incompatible type "int"; expected "Literal[2]" |
| 2456 |
[builtins fixtures/isinstancelist.pyi] |
| 2457 |
[typing fixtures/typing-medium.pyi] |
| 2458 |
|
| 2459 |
-- |
| 2460 |
-- Tests for Literals and enums |
| 2461 |
-- |
| 2462 |
|
| 2463 |
[case testLiteralWithEnumsBasic_no_native_parse] |
| 2464 |
from typing import Literal |
| 2465 |
from enum import Enum |
| 2466 |
|
| 2467 |
class Color(Enum): |
| 2468 |
RED = 1 |
| 2469 |
GREEN = 2 |
| 2470 |
BLUE = 3 |
| 2471 |
__ROUGE = RED |
| 2472 |
def func(self) -> int: pass |
| 2473 |
|
| 2474 |
r: Literal[Color.RED] |
| 2475 |
g: Literal[Color.GREEN] |
| 2476 |
b: Literal[Color.BLUE] |
| 2477 |
bad1: Literal[Color] # E: Parameter 1 of Literal[...] is invalid |
| 2478 |
bad2: Literal[Color.func] # E: Parameter 1 of Literal[...] is invalid |
| 2479 |
bad3: Literal[Color.func()] # E: Invalid type: Literal[...] cannot contain arbitrary expressions |
| 2480 |
# TODO: change the next line to use Color._Color__ROUGE when mypy implements name mangling |
| 2481 |
bad4: Literal[Color.__ROUGE] # E: Parameter 1 of Literal[...] is invalid |
| 2482 |
|
| 2483 |
def expects_color(x: Color) -> None: pass |
| 2484 |
def expects_red(x: Literal[Color.RED]) -> None: pass |
| 2485 |
def bad_func(x: Color.RED) -> None: pass # E: Invalid type: try using Literal[Color.RED] instead? |
| 2486 |
|
| 2487 |
expects_color(r) |
| 2488 |
expects_color(g) |
| 2489 |
expects_color(b) |
| 2490 |
expects_red(r) |
| 2491 |
expects_red(g) # E: Argument 1 to "expects_red" has incompatible type "Literal[Color.GREEN]"; expected "Literal[Color.RED]" |
| 2492 |
expects_red(b) # E: Argument 1 to "expects_red" has incompatible type "Literal[Color.BLUE]"; expected "Literal[Color.RED]" |
| 2493 |
|
| 2494 |
reveal_type(expects_red) # N: Revealed type is "def (x: Literal[__main__.Color.RED])" |
| 2495 |
reveal_type(r) # N: Revealed type is "Literal[__main__.Color.RED]" |
| 2496 |
reveal_type(r.func()) # N: Revealed type is "builtins.int" |
| 2497 |
[builtins fixtures/tuple.pyi] |
| 2498 |
[out] |
| 2499 |
|
| 2500 |
[case testLiteralWithEnumsDefinedInClass] |
| 2501 |
from typing import Literal |
| 2502 |
from enum import Enum |
| 2503 |
|
| 2504 |
class Wrapper: |
| 2505 |
class Color(Enum): |
| 2506 |
RED = 1 |
| 2507 |
GREEN = 2 |
| 2508 |
BLUE = 3 |
| 2509 |
|
| 2510 |
def foo(x: Literal[Wrapper.Color.RED]) -> None: pass |
| 2511 |
|
| 2512 |
r: Literal[Wrapper.Color.RED] |
| 2513 |
g: Literal[Wrapper.Color.GREEN] |
| 2514 |
|
| 2515 |
foo(r) |
| 2516 |
foo(g) # E: Argument 1 to "foo" has incompatible type "Literal[Color.GREEN]"; expected "Literal[Color.RED]" |
| 2517 |
|
| 2518 |
reveal_type(foo) # N: Revealed type is "def (x: Literal[__main__.Wrapper.Color.RED])" |
| 2519 |
reveal_type(r) # N: Revealed type is "Literal[__main__.Wrapper.Color.RED]" |
| 2520 |
[builtins fixtures/tuple.pyi] |
| 2521 |
[out] |
| 2522 |
|
| 2523 |
[case testLiteralWithEnumsSimilarDefinitions] |
| 2524 |
from typing import Literal |
| 2525 |
import mod_a |
| 2526 |
import mod_b |
| 2527 |
|
| 2528 |
def f(x: Literal[mod_a.Test.FOO]) -> None: pass |
| 2529 |
|
| 2530 |
a: Literal[mod_a.Test.FOO] |
| 2531 |
b: Literal[mod_a.Test2.FOO] |
| 2532 |
c: Literal[mod_b.Test.FOO] |
| 2533 |
|
| 2534 |
f(a) |
| 2535 |
f(b) # E: Argument 1 to "f" has incompatible type "Literal[Test2.FOO]"; expected "Literal[Test.FOO]" |
| 2536 |
f(c) # E: Argument 1 to "f" has incompatible type "Literal[mod_b.Test.FOO]"; expected "Literal[mod_a.Test.FOO]" |
| 2537 |
|
| 2538 |
[file mod_a.py] |
| 2539 |
from enum import Enum |
| 2540 |
|
| 2541 |
class Test(Enum): |
| 2542 |
FOO = 1 |
| 2543 |
BAR = 2 |
| 2544 |
|
| 2545 |
class Test2(Enum): |
| 2546 |
FOO = 1 |
| 2547 |
BAR = 2 |
| 2548 |
|
| 2549 |
[file mod_b.py] |
| 2550 |
from enum import Enum |
| 2551 |
|
| 2552 |
class Test(Enum): |
| 2553 |
FOO = 1 |
| 2554 |
BAR = 2 |
| 2555 |
[builtins fixtures/tuple.pyi] |
| 2556 |
[out] |
| 2557 |
|
| 2558 |
[case testLiteralWithEnumsDeclaredUsingCallSyntax] |
| 2559 |
from typing import Literal |
| 2560 |
from enum import Enum |
| 2561 |
|
| 2562 |
A = Enum('A', 'FOO BAR') |
| 2563 |
B = Enum('B', ['FOO', 'BAR']) |
| 2564 |
C = Enum('C', [('FOO', 1), ('BAR', 2)]) |
| 2565 |
D = Enum('D', {'FOO': 1, 'BAR': 2}) |
| 2566 |
|
| 2567 |
a: Literal[A.FOO] |
| 2568 |
b: Literal[B.FOO] |
| 2569 |
c: Literal[C.FOO] |
| 2570 |
d: Literal[D.FOO] |
| 2571 |
|
| 2572 |
reveal_type(a) # N: Revealed type is "Literal[__main__.A.FOO]" |
| 2573 |
reveal_type(b) # N: Revealed type is "Literal[__main__.B.FOO]" |
| 2574 |
reveal_type(c) # N: Revealed type is "Literal[__main__.C.FOO]" |
| 2575 |
reveal_type(d) # N: Revealed type is "Literal[__main__.D.FOO]" |
| 2576 |
[builtins fixtures/dict.pyi] |
| 2577 |
[out] |
| 2578 |
|
| 2579 |
[case testLiteralWithEnumsDerivedEnums] |
| 2580 |
from typing import Literal |
| 2581 |
from enum import Enum, IntEnum, IntFlag, Flag |
| 2582 |
|
| 2583 |
def expects_int(x: int) -> None: pass |
| 2584 |
|
| 2585 |
class A(Enum): |
| 2586 |
FOO = 1 |
| 2587 |
|
| 2588 |
class B(IntEnum): |
| 2589 |
FOO = 1 |
| 2590 |
|
| 2591 |
class C(IntFlag): |
| 2592 |
FOO = 1 |
| 2593 |
|
| 2594 |
class D(Flag): |
| 2595 |
FOO = 1 |
| 2596 |
|
| 2597 |
a: Literal[A.FOO] |
| 2598 |
b: Literal[B.FOO] |
| 2599 |
c: Literal[C.FOO] |
| 2600 |
d: Literal[D.FOO] |
| 2601 |
|
| 2602 |
expects_int(a) # E: Argument 1 to "expects_int" has incompatible type "Literal[A.FOO]"; expected "int" |
| 2603 |
expects_int(b) |
| 2604 |
expects_int(c) |
| 2605 |
expects_int(d) # E: Argument 1 to "expects_int" has incompatible type "Literal[D.FOO]"; expected "int" |
| 2606 |
[builtins fixtures/tuple.pyi] |
| 2607 |
[out] |
| 2608 |
|
| 2609 |
[case testLiteralWithEnumsAliases] |
| 2610 |
from typing import Literal |
| 2611 |
from enum import Enum |
| 2612 |
|
| 2613 |
class Test(Enum): |
| 2614 |
FOO = 1 |
| 2615 |
BAR = 2 |
| 2616 |
|
| 2617 |
Alias = Test |
| 2618 |
|
| 2619 |
x: Literal[Alias.FOO] |
| 2620 |
reveal_type(x) # N: Revealed type is "Literal[__main__.Test.FOO]" |
| 2621 |
[builtins fixtures/tuple.pyi] |
| 2622 |
[out] |
| 2623 |
|
| 2624 |
[case testLiteralUsingEnumAttributesInLiteralContexts] |
| 2625 |
from typing import Final, Literal |
| 2626 |
from enum import Enum |
| 2627 |
|
| 2628 |
class Test1(Enum): |
| 2629 |
FOO = 1 |
| 2630 |
BAR = 2 |
| 2631 |
Test2 = Enum('Test2', [('FOO', 1), ('BAR', 2)]) |
| 2632 |
|
| 2633 |
def expects_test1_foo(x: Literal[Test1.FOO]) -> None: ... |
| 2634 |
def expects_test2_foo(x: Literal[Test2.FOO]) -> None: ... |
| 2635 |
|
| 2636 |
expects_test1_foo(Test1.FOO) |
| 2637 |
expects_test1_foo(Test1.BAR) # E: Argument 1 to "expects_test1_foo" has incompatible type "Literal[Test1.BAR]"; expected "Literal[Test1.FOO]" |
| 2638 |
expects_test2_foo(Test2.FOO) |
| 2639 |
expects_test2_foo(Test2.BAR) # E: Argument 1 to "expects_test2_foo" has incompatible type "Literal[Test2.BAR]"; expected "Literal[Test2.FOO]" |
| 2640 |
|
| 2641 |
# Make sure the two 'FOO's are not interchangeable |
| 2642 |
expects_test1_foo(Test2.FOO) # E: Argument 1 to "expects_test1_foo" has incompatible type "Literal[Test2.FOO]"; expected "Literal[Test1.FOO]" |
| 2643 |
expects_test2_foo(Test1.FOO) # E: Argument 1 to "expects_test2_foo" has incompatible type "Literal[Test1.FOO]"; expected "Literal[Test2.FOO]" |
| 2644 |
|
| 2645 |
# Make sure enums follow the same semantics as 'x = 1' vs 'x: Final = 1' |
| 2646 |
var1 = Test1.FOO |
| 2647 |
final1: Final = Test1.FOO |
| 2648 |
expects_test1_foo(var1) # E: Argument 1 to "expects_test1_foo" has incompatible type "Test1"; expected "Literal[Test1.FOO]" |
| 2649 |
expects_test1_foo(final1) |
| 2650 |
|
| 2651 |
var2 = Test2.FOO |
| 2652 |
final2: Final = Test2.FOO |
| 2653 |
expects_test2_foo(var2) # E: Argument 1 to "expects_test2_foo" has incompatible type "Test2"; expected "Literal[Test2.FOO]" |
| 2654 |
expects_test2_foo(final2) |
| 2655 |
[builtins fixtures/tuple.pyi] |
| 2656 |
[out] |
| 2657 |
|
| 2658 |
[case testLiteralUsingEnumAttributeNamesInLiteralContexts] |
| 2659 |
from typing import Final, Literal |
| 2660 |
from enum import Enum |
| 2661 |
|
| 2662 |
class Test1(Enum): |
| 2663 |
FOO = 1 |
| 2664 |
BAR = 2 |
| 2665 |
Test2 = Enum('Test2', [('FOO', 1), ('BAR', 2)]) |
| 2666 |
Test3 = Enum('Test3', 'FOO BAR') |
| 2667 |
Test4 = Enum('Test4', ['FOO', 'BAR']) |
| 2668 |
Test5 = Enum('Test5', {'FOO': 1, 'BAR': 2}) |
| 2669 |
|
| 2670 |
def expects_foo(x: Literal['FOO']) -> None: ... |
| 2671 |
|
| 2672 |
expects_foo(Test1.FOO.name) |
| 2673 |
expects_foo(Test2.FOO.name) |
| 2674 |
expects_foo(Test3.FOO.name) |
| 2675 |
expects_foo(Test4.FOO.name) |
| 2676 |
expects_foo(Test5.FOO.name) |
| 2677 |
|
| 2678 |
expects_foo(Test1.BAR.name) # E: Argument 1 to "expects_foo" has incompatible type "Literal['BAR']"; expected "Literal['FOO']" |
| 2679 |
expects_foo(Test2.BAR.name) # E: Argument 1 to "expects_foo" has incompatible type "Literal['BAR']"; expected "Literal['FOO']" |
| 2680 |
expects_foo(Test3.BAR.name) # E: Argument 1 to "expects_foo" has incompatible type "Literal['BAR']"; expected "Literal['FOO']" |
| 2681 |
expects_foo(Test4.BAR.name) # E: Argument 1 to "expects_foo" has incompatible type "Literal['BAR']"; expected "Literal['FOO']" |
| 2682 |
expects_foo(Test5.BAR.name) # E: Argument 1 to "expects_foo" has incompatible type "Literal['BAR']"; expected "Literal['FOO']" |
| 2683 |
|
| 2684 |
reveal_type(Test1.FOO.name) # N: Revealed type is "Literal['FOO']?" |
| 2685 |
reveal_type(Test2.FOO.name) # N: Revealed type is "Literal['FOO']?" |
| 2686 |
reveal_type(Test3.FOO.name) # N: Revealed type is "Literal['FOO']?" |
| 2687 |
reveal_type(Test4.FOO.name) # N: Revealed type is "Literal['FOO']?" |
| 2688 |
reveal_type(Test5.FOO.name) # N: Revealed type is "Literal['FOO']?" |
| 2689 |
[builtins fixtures/tuple.pyi] |
| 2690 |
[out] |
| 2691 |
|
| 2692 |
[case testLiteralBinderLastValueErased] |
| 2693 |
# mypy: strict-equality |
| 2694 |
from typing import Literal |
| 2695 |
|
| 2696 |
def takes_three(x: Literal[3]) -> None: ... |
| 2697 |
x: object |
| 2698 |
x = 3 |
| 2699 |
|
| 2700 |
takes_three(x) # E: Argument 1 to "takes_three" has incompatible type "int"; expected "Literal[3]" |
| 2701 |
if x == 2: # OK |
| 2702 |
... |
| 2703 |
[builtins fixtures/bool.pyi] |
| 2704 |
|
| 2705 |
[case testLiteralBinderLastValueErasedPartialTypes] |
| 2706 |
# mypy: strict-equality |
| 2707 |
|
| 2708 |
def test() -> None: |
| 2709 |
x = None |
| 2710 |
if bool(): |
| 2711 |
x = 1 |
| 2712 |
|
| 2713 |
if x == 2: # OK |
| 2714 |
... |
| 2715 |
[builtins fixtures/bool.pyi] |
| 2716 |
|
| 2717 |
[case testUnaryOpLiteral] |
| 2718 |
from typing import Literal |
| 2719 |
|
| 2720 |
a: Literal[-2] = -2 |
| 2721 |
b: Literal[-1] = -1 |
| 2722 |
c: Literal[0] = 0 |
| 2723 |
d: Literal[1] = 1 |
| 2724 |
e: Literal[2] = 2 |
| 2725 |
f: Literal[+1] = 1 |
| 2726 |
g: Literal[+2] = 2 |
| 2727 |
h: Literal[1] = +1 |
| 2728 |
i: Literal[+2] = 2 |
| 2729 |
j: Literal[+3] = +3 |
| 2730 |
|
| 2731 |
x: Literal[+True] = True # E: Invalid type: Literal[...] cannot contain arbitrary expressions |
| 2732 |
y: Literal[-True] = -1 # E: Invalid type: Literal[...] cannot contain arbitrary expressions |
| 2733 |
z: Literal[~0] = 0 # E: Invalid type: Literal[...] cannot contain arbitrary expressions |
| 2734 |
[out] |
| 2735 |
[builtins fixtures/ops.pyi] |
| 2736 |
|
| 2737 |
[case testNegativeIntLiteralWithFinal] |
| 2738 |
from typing import Final, Literal |
| 2739 |
|
| 2740 |
ONE: Final = 1 |
| 2741 |
x: Literal[-1] = -ONE |
| 2742 |
y: Literal[+1] = +ONE |
| 2743 |
|
| 2744 |
TWO: Final = 2 |
| 2745 |
THREE: Final = 3 |
| 2746 |
|
| 2747 |
err_code = -TWO |
| 2748 |
if bool(): |
| 2749 |
err_code = -THREE |
| 2750 |
[builtins fixtures/ops.pyi] |
| 2751 |
|
| 2752 |
[case testAliasForEnumTypeAsLiteral] |
| 2753 |
from typing import Literal |
| 2754 |
from enum import Enum |
| 2755 |
|
| 2756 |
class Foo(Enum): |
| 2757 |
A = 1 |
| 2758 |
|
| 2759 |
F = Foo |
| 2760 |
|
| 2761 |
x: Literal[Foo.A] |
| 2762 |
y: Literal[F.A] |
| 2763 |
reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.A]" |
| 2764 |
reveal_type(y) # N: Revealed type is "Literal[__main__.Foo.A]" |
| 2765 |
[builtins fixtures/tuple.pyi] |
| 2766 |
|
| 2767 |
[case testLiteralUnionEnumAliasAssignable] |
| 2768 |
from enum import Enum |
| 2769 |
from typing import Literal, Union |
| 2770 |
|
| 2771 |
class E(Enum): |
| 2772 |
A = 'a' |
| 2773 |
B = 'b' |
| 2774 |
C = 'c' |
| 2775 |
|
| 2776 |
A = Literal[E.A] |
| 2777 |
B = Literal[E.B, E.C] |
| 2778 |
|
| 2779 |
def f(x: Union[A, B]) -> None: ... |
| 2780 |
def f2(x: Union[A, Literal[E.B, E.C]]) -> None: ... |
| 2781 |
def f3(x: Union[Literal[E.A], B]) -> None: ... |
| 2782 |
|
| 2783 |
def main(x: E) -> None: |
| 2784 |
f(x) |
| 2785 |
f2(x) |
| 2786 |
f3(x) |
| 2787 |
[builtins fixtures/tuple.pyi] |
| 2788 |
|
| 2789 |
[case testStrictEqualityLiteralTrueVsFalse] |
| 2790 |
# mypy: strict-equality |
| 2791 |
|
| 2792 |
class C: |
| 2793 |
a = True |
| 2794 |
|
| 2795 |
def update(self) -> None: |
| 2796 |
self.a = False |
| 2797 |
|
| 2798 |
c = C() |
| 2799 |
assert c.a is True |
| 2800 |
c.update() |
| 2801 |
assert c.a is False |
| 2802 |
[builtins fixtures/bool.pyi] |
| 2803 |
|
| 2804 |
[case testConditionalBoolLiteralUnionNarrowing] |
| 2805 |
# flags: --warn-unreachable |
| 2806 |
from typing import Literal, Union |
| 2807 |
|
| 2808 |
class Truth: |
| 2809 |
def __bool__(self) -> Literal[True]: ... |
| 2810 |
|
| 2811 |
class AlsoTruth: |
| 2812 |
def __bool__(self) -> Literal[True]: ... |
| 2813 |
|
| 2814 |
class Lie: |
| 2815 |
def __bool__(self) -> Literal[False]: ... |
| 2816 |
|
| 2817 |
class AnyAnswer: |
| 2818 |
def __bool__(self) -> bool: ... |
| 2819 |
|
| 2820 |
class NoAnswerSpecified: |
| 2821 |
pass |
| 2822 |
|
| 2823 |
x: Union[Truth, Lie] |
| 2824 |
|
| 2825 |
if x: |
| 2826 |
reveal_type(x) # N: Revealed type is "__main__.Truth" |
| 2827 |
else: |
| 2828 |
reveal_type(x) # N: Revealed type is "__main__.Lie" |
| 2829 |
|
| 2830 |
if not x: |
| 2831 |
reveal_type(x) # N: Revealed type is "__main__.Lie" |
| 2832 |
else: |
| 2833 |
reveal_type(x) # N: Revealed type is "__main__.Truth" |
| 2834 |
|
| 2835 |
y: Union[Truth, AlsoTruth, Lie] |
| 2836 |
|
| 2837 |
if y: |
| 2838 |
reveal_type(y) # N: Revealed type is "__main__.Truth | __main__.AlsoTruth" |
| 2839 |
else: |
| 2840 |
reveal_type(y) # N: Revealed type is "__main__.Lie" |
| 2841 |
|
| 2842 |
z: Union[Truth, AnyAnswer] |
| 2843 |
|
| 2844 |
if z: |
| 2845 |
reveal_type(z) # N: Revealed type is "__main__.Truth | __main__.AnyAnswer" |
| 2846 |
else: |
| 2847 |
reveal_type(z) # N: Revealed type is "__main__.AnyAnswer" |
| 2848 |
|
| 2849 |
q: Union[Truth, NoAnswerSpecified] |
| 2850 |
|
| 2851 |
if q: |
| 2852 |
reveal_type(q) # N: Revealed type is "__main__.Truth | __main__.NoAnswerSpecified" |
| 2853 |
else: |
| 2854 |
reveal_type(q) # N: Revealed type is "__main__.NoAnswerSpecified" |
| 2855 |
|
| 2856 |
w: Union[Truth, AlsoTruth] |
| 2857 |
|
| 2858 |
if w: |
| 2859 |
reveal_type(w) # N: Revealed type is "__main__.Truth | __main__.AlsoTruth" |
| 2860 |
else: |
| 2861 |
reveal_type(w) # E: Statement is unreachable |
| 2862 |
|
| 2863 |
[builtins fixtures/bool.pyi] |
| 2864 |
|
| 2865 |
[case testLiteralAndInstanceSubtyping] |
| 2866 |
# https://github.com/python/mypy/issues/7399 |
| 2867 |
# https://github.com/python/mypy/issues/11232 |
| 2868 |
from typing import Final, Literal, Tuple, Union |
| 2869 |
|
| 2870 |
x: bool |
| 2871 |
|
| 2872 |
def f() -> Union[Tuple[Literal[True], int], Tuple[Literal[False], str]]: |
| 2873 |
if x: |
| 2874 |
return (True, 5) |
| 2875 |
else: |
| 2876 |
return (False, 'oops') |
| 2877 |
|
| 2878 |
reveal_type(f()) # N: Revealed type is "tuple[Literal[True], builtins.int] | tuple[Literal[False], builtins.str]" |
| 2879 |
|
| 2880 |
def does_work() -> Tuple[Literal[1]]: |
| 2881 |
x: Final = (1,) |
| 2882 |
return x |
| 2883 |
|
| 2884 |
def also_works() -> Tuple[Literal[1]]: |
| 2885 |
x: Tuple[Literal[1]] = (1,) |
| 2886 |
return x |
| 2887 |
|
| 2888 |
def invalid_literal_value() -> Tuple[Literal[1]]: |
| 2889 |
x: Final = (2,) |
| 2890 |
return x # E: Incompatible return value type (got "tuple[int]", expected "tuple[Literal[1]]") |
| 2891 |
|
| 2892 |
def invalid_literal_type() -> Tuple[Literal[1]]: |
| 2893 |
x: Final = (True,) |
| 2894 |
return x # E: Incompatible return value type (got "tuple[bool]", expected "tuple[Literal[1]]") |
| 2895 |
|
| 2896 |
def incorrect_return1() -> Union[Tuple[Literal[True], int], Tuple[Literal[False], str]]: |
| 2897 |
if x: |
| 2898 |
return (False, 5) # E: Incompatible return value type (got "tuple[bool, int]", expected "tuple[Literal[True], int] | tuple[Literal[False], str]") |
| 2899 |
else: |
| 2900 |
return (True, 'oops') # E: Incompatible return value type (got "tuple[bool, str]", expected "tuple[Literal[True], int] | tuple[Literal[False], str]") |
| 2901 |
|
| 2902 |
def incorrect_return2() -> Union[Tuple[Literal[True], int], Tuple[Literal[False], str]]: |
| 2903 |
if x: |
| 2904 |
return (bool(), 5) # E: Incompatible return value type (got "tuple[bool, int]", expected "tuple[Literal[True], int] | tuple[Literal[False], str]") |
| 2905 |
else: |
| 2906 |
return (bool(), 'oops') # E: Incompatible return value type (got "tuple[bool, str]", expected "tuple[Literal[True], int] | tuple[Literal[False], str]") |
| 2907 |
[builtins fixtures/bool.pyi] |
| 2908 |
|
| 2909 |
[case testLiteralSubtypeContext] |
| 2910 |
from typing import Literal |
| 2911 |
|
| 2912 |
class A: |
| 2913 |
foo: Literal['bar', 'spam'] |
| 2914 |
class B(A): |
| 2915 |
foo = 'spam' |
| 2916 |
|
| 2917 |
reveal_type(B().foo) # N: Revealed type is "Literal['spam']" |
| 2918 |
[builtins fixtures/tuple.pyi] |
| 2919 |
|
| 2920 |
[case testLiteralSubtypeContextNested] |
| 2921 |
from typing import List, Literal |
| 2922 |
|
| 2923 |
class A: |
| 2924 |
foo: List[Literal['bar', 'spam']] |
| 2925 |
class B(A): |
| 2926 |
foo = ['spam'] |
| 2927 |
|
| 2928 |
reveal_type(B().foo) # N: Revealed type is "builtins.list[Literal['bar'] | Literal['spam']]" |
| 2929 |
[builtins fixtures/tuple.pyi] |
| 2930 |
|
| 2931 |
[case testLiteralSubtypeContextGeneric] |
| 2932 |
from typing import Generic, List, Literal, TypeVar |
| 2933 |
|
| 2934 |
T = TypeVar("T", bound=str) |
| 2935 |
|
| 2936 |
class B(Generic[T]): |
| 2937 |
collection: List[T] |
| 2938 |
word: T |
| 2939 |
|
| 2940 |
class C(B[Literal["word"]]): |
| 2941 |
collection = ["word"] |
| 2942 |
word = "word" |
| 2943 |
|
| 2944 |
reveal_type(C().collection) # N: Revealed type is "builtins.list[Literal['word']]" |
| 2945 |
reveal_type(C().word) # N: Revealed type is "Literal['word']" |
| 2946 |
[builtins fixtures/tuple.pyi] |
| 2947 |
|
| 2948 |
[case testStringIntUnionTernary] |
| 2949 |
# https://github.com/python/mypy/issues/19534 |
| 2950 |
def test(b: bool) -> None: |
| 2951 |
l = 1 if b else "a" |
| 2952 |
reveal_type(l) # N: Revealed type is "builtins.int | builtins.str" |
| 2953 |
[builtins fixtures/tuple.pyi] |
| 2954 |
|
| 2955 |
[case testListComprehensionTernary] |
| 2956 |
# https://github.com/python/mypy/issues/19534 |
| 2957 |
def test(b: bool) -> None: |
| 2958 |
l = [1] if b else ["a"] |
| 2959 |
reveal_type(l) # N: Revealed type is "builtins.list[builtins.int] | builtins.list[builtins.str]" |
| 2960 |
[builtins fixtures/list.pyi] |
| 2961 |
|
| 2962 |
[case testLambdaTernary] |
| 2963 |
from typing import TypeVar, Union, Callable, reveal_type |
| 2964 |
|
| 2965 |
NOOP = lambda: None |
| 2966 |
class A: pass |
| 2967 |
class B: |
| 2968 |
attr: Union[A, None] |
| 2969 |
|
| 2970 |
def test_static(x: Union[A, None]) -> None: |
| 2971 |
def foo(t: A) -> None: ... |
| 2972 |
|
| 2973 |
l1: Callable[[], object] = (lambda: foo(x)) if x is not None else NOOP |
| 2974 |
r1: Callable[[], object] = NOOP if x is None else (lambda: foo(x)) |
| 2975 |
l2 = (lambda: foo(x)) if x is not None else NOOP |
| 2976 |
r2 = NOOP if x is None else (lambda: foo(x)) |
| 2977 |
reveal_type(l2) # N: Revealed type is "def ()" |
| 2978 |
reveal_type(r2) # N: Revealed type is "def ()" |
| 2979 |
|
| 2980 |
def test_generic(x: Union[A, None]) -> None: |
| 2981 |
T = TypeVar("T") |
| 2982 |
def bar(t: T) -> T: return t |
| 2983 |
|
| 2984 |
l1: Callable[[], None] = (lambda: bar(x)) if x is None else NOOP |
| 2985 |
r1: Callable[[], None] = NOOP if x is not None else (lambda: bar(x)) |
| 2986 |
l2 = (lambda: bar(x)) if x is None else NOOP |
| 2987 |
r2 = NOOP if x is not None else (lambda: bar(x)) |
| 2988 |
reveal_type(l2) # N: Revealed type is "def ()" |
| 2989 |
reveal_type(r2) # N: Revealed type is "def ()" |
| 2990 |
|
| 2991 |
|
| 2992 |
[case testLambdaTernaryIndirectAttribute] |
| 2993 |
# fails due to binder issue inside `check_func_def` |
| 2994 |
# https://github.com/python/mypy/issues/19561 |
| 2995 |
from typing import TypeVar, Union, Callable, reveal_type |
| 2996 |
|
| 2997 |
NOOP = lambda: None |
| 2998 |
class A: pass |
| 2999 |
class B: |
| 3000 |
attr: Union[A, None] |
| 3001 |
|
| 3002 |
def test_static_with_attr(x: B) -> None: |
| 3003 |
def foo(t: A) -> None: ... |
| 3004 |
|
| 3005 |
l1: Callable[[], None] = (lambda: foo(x.attr)) if x.attr is not None else NOOP # E: Argument 1 to "foo" has incompatible type "A | None"; expected "A" |
| 3006 |
r1: Callable[[], None] = NOOP if x.attr is None else (lambda: foo(x.attr)) # E: Argument 1 to "foo" has incompatible type "A | None"; expected "A" |
| 3007 |
l2 = (lambda: foo(x.attr)) if x.attr is not None else NOOP # E: Argument 1 to "foo" has incompatible type "A | None"; expected "A" |
| 3008 |
r2 = NOOP if x.attr is None else (lambda: foo(x.attr)) # E: Argument 1 to "foo" has incompatible type "A | None"; expected "A" |
| 3009 |
reveal_type(l2) # N: Revealed type is "def ()" |
| 3010 |
reveal_type(r2) # N: Revealed type is "def ()" |
| 3011 |
|
| 3012 |
def test_generic_with_attr(x: B) -> None: |
| 3013 |
T = TypeVar("T") |
| 3014 |
def bar(t: T) -> T: return t |
| 3015 |
|
| 3016 |
l1: Callable[[], None] = (lambda: bar(x.attr)) if x.attr is None else NOOP # E: Incompatible types in assignment (expression has type "Callable[[], A | None]", variable has type "Callable[[], None]") |
| 3017 |
r1: Callable[[], None] = NOOP if x.attr is not None else (lambda: bar(x.attr)) # E: Incompatible types in assignment (expression has type "Callable[[], A | None]", variable has type "Callable[[], None]") |
| 3018 |
l2 = (lambda: bar(x.attr)) if x.attr is None else NOOP |
| 3019 |
r2 = NOOP if x.attr is not None else (lambda: bar(x.attr)) |
| 3020 |
reveal_type(l2) # N: Revealed type is "def () -> __main__.A | None" |
| 3021 |
reveal_type(r2) # N: Revealed type is "def () -> __main__.A | None" |
| 3022 |
|
| 3023 |
[case testLambdaTernaryDoubleIndirectAttribute] |
| 3024 |
# fails due to binder issue inside `check_func_def` |
| 3025 |
# https://github.com/python/mypy/issues/19561 |
| 3026 |
from typing import TypeVar, Union, Callable, reveal_type |
| 3027 |
|
| 3028 |
NOOP = lambda: None |
| 3029 |
class A: pass |
| 3030 |
class B: |
| 3031 |
attr: Union[A, None] |
| 3032 |
class C: |
| 3033 |
attr: B |
| 3034 |
|
| 3035 |
def test_static_with_attr(x: C) -> None: |
| 3036 |
def foo(t: A) -> None: ... |
| 3037 |
|
| 3038 |
l1: Callable[[], None] = (lambda: foo(x.attr.attr)) if x.attr.attr is not None else NOOP # E: Argument 1 to "foo" has incompatible type "A | None"; expected "A" |
| 3039 |
r1: Callable[[], None] = NOOP if x.attr.attr is None else (lambda: foo(x.attr.attr)) # E: Argument 1 to "foo" has incompatible type "A | None"; expected "A" |
| 3040 |
l2 = (lambda: foo(x.attr.attr)) if x.attr.attr is not None else NOOP # E: Argument 1 to "foo" has incompatible type "A | None"; expected "A" |
| 3041 |
r2 = NOOP if x.attr.attr is None else (lambda: foo(x.attr.attr)) # E: Argument 1 to "foo" has incompatible type "A | None"; expected "A" |
| 3042 |
reveal_type(l2) # N: Revealed type is "def ()" |
| 3043 |
reveal_type(r2) # N: Revealed type is "def ()" |
| 3044 |
|
| 3045 |
def test_generic_with_attr(x: C) -> None: |
| 3046 |
T = TypeVar("T") |
| 3047 |
def bar(t: T) -> T: return t |
| 3048 |
|
| 3049 |
l1: Callable[[], None] = (lambda: bar(x.attr.attr)) if x.attr.attr is None else NOOP # E: Incompatible types in assignment (expression has type "Callable[[], A | None]", variable has type "Callable[[], None]") |
| 3050 |
r1: Callable[[], None] = NOOP if x.attr.attr is not None else (lambda: bar(x.attr.attr)) # E: Incompatible types in assignment (expression has type "Callable[[], A | None]", variable has type "Callable[[], None]") |
| 3051 |
l2 = (lambda: bar(x.attr.attr)) if x.attr.attr is None else NOOP |
| 3052 |
r2 = NOOP if x.attr.attr is not None else (lambda: bar(x.attr.attr)) |
| 3053 |
reveal_type(l2) # N: Revealed type is "def () -> __main__.A | None" |
| 3054 |
reveal_type(r2) # N: Revealed type is "def () -> __main__.A | None" |
| 3055 |
|
| 3056 |
|
| 3057 |
[case testLiteralTernaryUnionNarrowing] |
| 3058 |
from typing import Literal, Optional |
| 3059 |
|
| 3060 |
SEP = Literal["a", "b"] |
| 3061 |
|
| 3062 |
class Base: |
| 3063 |
def feed_data( |
| 3064 |
self, |
| 3065 |
sep: SEP, |
| 3066 |
) -> int: |
| 3067 |
return 0 |
| 3068 |
|
| 3069 |
class C(Base): |
| 3070 |
def feed_data( |
| 3071 |
self, |
| 3072 |
sep: Optional[SEP] = None, |
| 3073 |
) -> int: |
| 3074 |
if sep is None: |
| 3075 |
sep = "a" if int() else "b" |
| 3076 |
reveal_type(sep) # N: Revealed type is "Literal['a'] | Literal['b']" |
| 3077 |
return super().feed_data(sep) |
| 3078 |
[builtins fixtures/primitives.pyi] |
| 3079 |
|
| 3080 |
[case testLiteralInsideAType] |
| 3081 |
from typing import Literal, Type, Union |
| 3082 |
|
| 3083 |
x: Type[Literal[1]] # E: Type[...] can't contain "Literal[...]" |
| 3084 |
y: Type[Union[Literal[1], Literal[2]]] # E: Type[...] can't contain "Union[Literal[...], Literal[...]]" |
| 3085 |
z: Type[Literal[1, 2]] # E: Type[...] can't contain "Union[Literal[...], Literal[...]]" |
| 3086 |
[builtins fixtures/tuple.pyi] |
| 3087 |
|
| 3088 |
[case testJoinLiteralAndInstance] |
| 3089 |
from typing import Generic, TypeVar, Literal |
| 3090 |
|
| 3091 |
T = TypeVar("T") |
| 3092 |
|
| 3093 |
class A(Generic[T]): ... |
| 3094 |
|
| 3095 |
def f(a: A[T], t: T) -> T: ... |
| 3096 |
def g(a: T, t: A[T]) -> T: ... |
| 3097 |
|
| 3098 |
def check(obj: A[Literal[1]]) -> None: |
| 3099 |
reveal_type(f(obj, 1)) # N: Revealed type is "Literal[1]" |
| 3100 |
reveal_type(f(obj, '')) # E: Cannot infer value of type parameter "T" of "f" \ |
| 3101 |
# N: Revealed type is "Any" |
| 3102 |
reveal_type(g(1, obj)) # N: Revealed type is "Literal[1]" |
| 3103 |
reveal_type(g('', obj)) # E: Cannot infer value of type parameter "T" of "g" \ |
| 3104 |
# N: Revealed type is "Any" |
| 3105 |
[builtins fixtures/tuple.pyi] |
| 3106 |
|
| 3107 |
[case testEnumLiteralIsNotIntLiteral] |
| 3108 |
# See https://discuss.python.org/t/amend-pep-586-to-make-enum-values-subtypes-of-literal/59456/4 |
| 3109 |
from enum import IntEnum |
| 3110 |
from typing import Literal |
| 3111 |
|
| 3112 |
class X(IntEnum): |
| 3113 |
a = 1 |
| 3114 |
|
| 3115 |
def __add__(self, value: int, /) -> int: |
| 3116 |
return self.value + value + 42 |
| 3117 |
|
| 3118 |
def __eq__(self, other: object): |
| 3119 |
return False |
| 3120 |
|
| 3121 |
def __bool__(self) -> bool: |
| 3122 |
return False |
| 3123 |
|
| 3124 |
def f(x: Literal[1]): |
| 3125 |
# 44 at runtime if you were to allow passing X.a |
| 3126 |
reveal_type(x + 1) # N: Revealed type is "builtins.int" |
| 3127 |
|
| 3128 |
def g(x: Literal[1, 2]): |
| 3129 |
# false if you were to allow passing X.a |
| 3130 |
if x == 1: |
| 3131 |
reveal_type(x) # N: Revealed type is "Literal[1]" |
| 3132 |
else: |
| 3133 |
reveal_type(x) # N: Revealed type is "Literal[2]" |
| 3134 |
|
| 3135 |
def h(x: Literal[0, 1]): |
| 3136 |
# false if you were to allow passing X.a |
| 3137 |
if x: |
| 3138 |
reveal_type(x) # N: Revealed type is "Literal[1]" |
| 3139 |
else: |
| 3140 |
reveal_type(x) # N: Revealed type is "Literal[0]" |
| 3141 |
|
| 3142 |
f(X.a) # E: Argument 1 to "f" has incompatible type "Literal[X.a]"; expected "Literal[1]" |
| 3143 |
g(X.a) # E: Argument 1 to "g" has incompatible type "Literal[X.a]"; expected "Literal[1, 2]" |
| 3144 |
h(X.a) # E: Argument 1 to "h" has incompatible type "Literal[X.a]"; expected "Literal[0, 1]" |
| 3145 |
|
| 3146 |
f(X.a.value) |
| 3147 |
g(X.a.value) |
| 3148 |
h(X.a.value) |
| 3149 |
[builtins fixtures/primitives.pyi] |