| 1 |
-- Test cases for the redefinition of variable with a different type. |
| 2 |
|
| 3 |
|
| 4 |
-- Redefine local variable |
| 5 |
-- ----------------------- |
| 6 |
|
| 7 |
|
| 8 |
[case testRedefineLocalWithDifferentType] |
| 9 |
# flags: --allow-redefinition-old |
| 10 |
def f() -> None: |
| 11 |
x = 0 |
| 12 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 13 |
x = '' |
| 14 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 15 |
|
| 16 |
[case testCannotConditionallyRedefineLocalWithDifferentType] |
| 17 |
# flags: --allow-redefinition-old |
| 18 |
def f() -> None: |
| 19 |
y = 0 |
| 20 |
reveal_type(y) # N: Revealed type is "builtins.int" |
| 21 |
if int(): |
| 22 |
y = '' \ |
| 23 |
# E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 24 |
reveal_type(y) # N: Revealed type is "builtins.int" |
| 25 |
reveal_type(y) # N: Revealed type is "builtins.int" |
| 26 |
|
| 27 |
[case testRedefineFunctionArg] |
| 28 |
# flags: --allow-redefinition-old |
| 29 |
def f(x: int) -> None: |
| 30 |
g(x) |
| 31 |
x = '' |
| 32 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 33 |
def g(x: int) -> None: |
| 34 |
if int(): |
| 35 |
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 36 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 37 |
|
| 38 |
[case testRedefineAnnotationOnly] |
| 39 |
# flags: --allow-redefinition-old |
| 40 |
def f() -> None: |
| 41 |
x: int |
| 42 |
x = '' \ |
| 43 |
# E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 44 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 45 |
def g() -> None: |
| 46 |
x: int |
| 47 |
x = 1 |
| 48 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 49 |
x = '' |
| 50 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 51 |
|
| 52 |
[case testRedefineLocalUsingOldValue] |
| 53 |
# flags: --allow-redefinition-old |
| 54 |
from typing import TypeVar, Union |
| 55 |
|
| 56 |
T = TypeVar('T') |
| 57 |
|
| 58 |
def f(x: int) -> None: |
| 59 |
x = g(x) |
| 60 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 61 |
y = 1 |
| 62 |
y = g(y) |
| 63 |
reveal_type(y) # N: Revealed type is "builtins.int | builtins.str" |
| 64 |
|
| 65 |
def g(x: T) -> Union[T, str]: pass |
| 66 |
|
| 67 |
[case testRedefineLocalForLoopIndexVariable] |
| 68 |
# flags: --allow-redefinition-old |
| 69 |
from typing import Iterable |
| 70 |
def f(a: Iterable[int], b: Iterable[str]) -> None: |
| 71 |
for x in a: |
| 72 |
x = '' \ |
| 73 |
# E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 74 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 75 |
for x in b: |
| 76 |
x = 1 \ |
| 77 |
# E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 78 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 79 |
|
| 80 |
def g(a: Iterable[int]) -> None: |
| 81 |
for x in a: pass |
| 82 |
x = '' |
| 83 |
|
| 84 |
def h(a: Iterable[int]) -> None: |
| 85 |
x = '' |
| 86 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 87 |
for x in a: pass |
| 88 |
|
| 89 |
[case testCannotRedefineLocalWithinTry] |
| 90 |
# flags: --allow-redefinition-old |
| 91 |
def g(): pass |
| 92 |
def f() -> None: |
| 93 |
try: |
| 94 |
x = 0 |
| 95 |
x |
| 96 |
g() # Might raise an exception |
| 97 |
x = '' \ |
| 98 |
# E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 99 |
except: |
| 100 |
pass |
| 101 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 102 |
y = 0 |
| 103 |
y |
| 104 |
y = '' |
| 105 |
|
| 106 |
[case testRedefineLocalWithinTryClauses] |
| 107 |
# flags: --allow-redefinition-old |
| 108 |
def fn_str(_: str) -> int: ... |
| 109 |
def fn_int(_: int) -> None: ... |
| 110 |
|
| 111 |
def in_block() -> None: |
| 112 |
try: |
| 113 |
a = "" |
| 114 |
a = fn_str(a) # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 115 |
fn_int(a) # E: Argument 1 to "fn_int" has incompatible type "str"; expected "int" |
| 116 |
except: |
| 117 |
b = "" |
| 118 |
b = fn_str(b) |
| 119 |
fn_int(b) |
| 120 |
else: |
| 121 |
c = "" |
| 122 |
c = fn_str(c) |
| 123 |
fn_int(c) |
| 124 |
finally: |
| 125 |
d = "" |
| 126 |
d = fn_str(d) |
| 127 |
fn_int(d) |
| 128 |
reveal_type(a) # N: Revealed type is "builtins.str" |
| 129 |
reveal_type(b) # N: Revealed type is "builtins.int" |
| 130 |
reveal_type(c) # N: Revealed type is "builtins.int" |
| 131 |
reveal_type(d) # N: Revealed type is "builtins.int" |
| 132 |
|
| 133 |
def across_blocks() -> None: |
| 134 |
try: |
| 135 |
a = "" |
| 136 |
except: |
| 137 |
pass |
| 138 |
else: |
| 139 |
a = fn_str(a) # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 140 |
reveal_type(a) # N: Revealed type is "builtins.str" |
| 141 |
|
| 142 |
[case testRedefineLocalExceptVar] |
| 143 |
# flags: --allow-redefinition-old |
| 144 |
def fn_exc(_: Exception) -> str: ... |
| 145 |
|
| 146 |
def exc_name() -> None: |
| 147 |
try: |
| 148 |
pass |
| 149 |
except RuntimeError as e: |
| 150 |
e = fn_exc(e) |
| 151 |
[builtins fixtures/exception.pyi] |
| 152 |
|
| 153 |
[case testRedefineNestedInTry] |
| 154 |
# flags: --allow-redefinition-old |
| 155 |
|
| 156 |
def fn_int(_: int) -> None: ... |
| 157 |
|
| 158 |
try: |
| 159 |
try: |
| 160 |
... |
| 161 |
finally: |
| 162 |
a = "" |
| 163 |
a = 5 # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 164 |
fn_int(a) # E: Argument 1 to "fn_int" has incompatible type "str"; expected "int" |
| 165 |
except: |
| 166 |
pass |
| 167 |
|
| 168 |
[case testRedefineLocalWithinWith] |
| 169 |
# flags: --allow-redefinition-old |
| 170 |
def f() -> None: |
| 171 |
with g(): |
| 172 |
x = 0 |
| 173 |
x |
| 174 |
g() # Might raise an exception, but we ignore this |
| 175 |
x = '' |
| 176 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 177 |
y = 0 |
| 178 |
y |
| 179 |
y = '' |
| 180 |
|
| 181 |
def g(): pass |
| 182 |
|
| 183 |
[case testCannotRedefineAcrossNestedFunction] |
| 184 |
# flags: --allow-redefinition-old |
| 185 |
def f() -> None: |
| 186 |
x = 0 |
| 187 |
x |
| 188 |
def g() -> None: |
| 189 |
x |
| 190 |
g() |
| 191 |
x = '' \ |
| 192 |
# E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 193 |
g() |
| 194 |
y = 0 |
| 195 |
y |
| 196 |
y = '' |
| 197 |
|
| 198 |
[case testCannotRedefineAcrossNestedDecoratedFunction] |
| 199 |
# flags: --allow-redefinition-old |
| 200 |
def dec(f): return f |
| 201 |
|
| 202 |
def f() -> None: |
| 203 |
x = 0 |
| 204 |
x |
| 205 |
@dec |
| 206 |
def g() -> None: |
| 207 |
x |
| 208 |
g() |
| 209 |
x = '' \ |
| 210 |
# E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 211 |
g() |
| 212 |
y = 0 |
| 213 |
y |
| 214 |
y = '' |
| 215 |
|
| 216 |
[case testCannotRedefineAcrossNestedOverloadedFunction] |
| 217 |
# flags: --allow-redefinition-old |
| 218 |
from typing import overload |
| 219 |
|
| 220 |
def f() -> None: |
| 221 |
x = 0 |
| 222 |
x |
| 223 |
@overload |
| 224 |
def g() -> None: pass |
| 225 |
@overload |
| 226 |
def g(x: int) -> None: pass |
| 227 |
def g(x=0): |
| 228 |
pass |
| 229 |
g() |
| 230 |
x = '' \ |
| 231 |
# E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 232 |
g() |
| 233 |
y = 0 |
| 234 |
y |
| 235 |
y = '' |
| 236 |
|
| 237 |
[case testRedefineLocalInMultipleAssignment] |
| 238 |
# flags: --allow-redefinition-old |
| 239 |
def f() -> None: |
| 240 |
x, x = 1, '' |
| 241 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 242 |
x = object() |
| 243 |
reveal_type(x) # N: Revealed type is "builtins.object" |
| 244 |
|
| 245 |
def g() -> None: |
| 246 |
x = 1 |
| 247 |
if 1: |
| 248 |
x, x = '', 1 \ |
| 249 |
# E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 250 |
|
| 251 |
[case testRedefineUnderscore] |
| 252 |
# flags: --allow-redefinition-old |
| 253 |
def f() -> None: |
| 254 |
_, _ = 1, '' |
| 255 |
if 1: |
| 256 |
_, _ = '', 1 |
| 257 |
# This is unintentional but probably fine. No one is going to read _ value. |
| 258 |
reveal_type(_) # N: Revealed type is "builtins.int" |
| 259 |
|
| 260 |
[case testRedefineWithBreakAndContinue] |
| 261 |
# flags: --allow-redefinition-old |
| 262 |
def f() -> None: |
| 263 |
y = 0 |
| 264 |
y |
| 265 |
while int(): |
| 266 |
z = 0 |
| 267 |
z |
| 268 |
z = '' |
| 269 |
x = 0 |
| 270 |
if int(): |
| 271 |
break |
| 272 |
x = '' \ |
| 273 |
# E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 274 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 275 |
y = '' |
| 276 |
|
| 277 |
def g() -> None: |
| 278 |
y = 0 |
| 279 |
y |
| 280 |
for a in h(): |
| 281 |
z = 0 |
| 282 |
z |
| 283 |
z = '' |
| 284 |
x = 0 |
| 285 |
if int(): |
| 286 |
continue |
| 287 |
x = '' \ |
| 288 |
# E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 289 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 290 |
y = '' |
| 291 |
|
| 292 |
def h(): pass |
| 293 |
|
| 294 |
[case testRedefineLocalAndNestedLoops] |
| 295 |
# flags: --allow-redefinition-old |
| 296 |
def f() -> None: |
| 297 |
z = 0 |
| 298 |
z |
| 299 |
while int(): |
| 300 |
x = 0 |
| 301 |
x |
| 302 |
while int(): |
| 303 |
if 1: |
| 304 |
y = 1 |
| 305 |
y |
| 306 |
if int(): |
| 307 |
break |
| 308 |
y = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 309 |
x = '' |
| 310 |
z = '' |
| 311 |
|
| 312 |
[case testCannotRedefineVarAsFunction] |
| 313 |
# flags: --allow-redefinition-old |
| 314 |
def f() -> None: |
| 315 |
def x(): pass |
| 316 |
x = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Callable[[], Any]") |
| 317 |
reveal_type(x) # N: Revealed type is "def () -> Any" |
| 318 |
y = 1 |
| 319 |
def y(): pass # E: Name "y" already defined on line 6 |
| 320 |
|
| 321 |
[case testCannotRedefineVarAsClass] |
| 322 |
# flags: --allow-redefinition-old |
| 323 |
def f() -> None: |
| 324 |
class x: pass |
| 325 |
x = 1 # E: Cannot assign to a type \ |
| 326 |
# E: Incompatible types in assignment (expression has type "int", variable has type "type[x]") |
| 327 |
y = 1 |
| 328 |
class y: pass # E: Name "y" already defined on line 5 |
| 329 |
|
| 330 |
[case testRedefineVarAsTypeVar] |
| 331 |
# flags: --allow-redefinition-old |
| 332 |
from typing import TypeVar |
| 333 |
def f() -> None: |
| 334 |
x = TypeVar('x') |
| 335 |
x = 1 # E: Invalid assignment target \ |
| 336 |
# E: Incompatible types in assignment (expression has type "int", variable has type "TypeVar") |
| 337 |
reveal_type(x) # N: Revealed type is "typing.TypeVar" |
| 338 |
y = 1 |
| 339 |
y = TypeVar('y') # E: Cannot redefine "y" as a type variable \ |
| 340 |
# E: Incompatible types in assignment (expression has type "TypeVar", variable has type "int") |
| 341 |
def h(a: y) -> y: return a # E: Variable "y" is not valid as a type \ |
| 342 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases |
| 343 |
[builtins fixtures/tuple.pyi] |
| 344 |
[typing fixtures/typing-full.pyi] |
| 345 |
|
| 346 |
[case testCannotRedefineVarAsModule] |
| 347 |
# flags: --allow-redefinition-old |
| 348 |
def f() -> None: |
| 349 |
import typing as m |
| 350 |
m = 1 # E: Incompatible types in assignment (expression has type "int", variable has type Module) |
| 351 |
n = 1 |
| 352 |
import typing as n # E: Incompatible import of "n" (imported name has type Module, local name has type "int") |
| 353 |
[builtins fixtures/module.pyi] |
| 354 |
[typing fixtures/typing-full.pyi] |
| 355 |
|
| 356 |
[case testRedefineLocalWithTypeAnnotation] |
| 357 |
# flags: --allow-redefinition-old |
| 358 |
def f() -> None: |
| 359 |
x = 1 |
| 360 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 361 |
x = '' # type: object |
| 362 |
reveal_type(x) # N: Revealed type is "builtins.object" |
| 363 |
def g() -> None: |
| 364 |
x = 1 |
| 365 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 366 |
x: object = '' |
| 367 |
reveal_type(x) # N: Revealed type is "builtins.object" |
| 368 |
def h() -> None: |
| 369 |
x: int |
| 370 |
x = 1 |
| 371 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 372 |
x: object |
| 373 |
x: object = '' # E: Name "x" already defined on line 16 |
| 374 |
def farg(x: int) -> None: |
| 375 |
x: str = '' # E: Name "x" already defined on line 18 |
| 376 |
def farg2(x: int) -> None: |
| 377 |
x: str = x # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 378 |
|
| 379 |
[case testRedefineLocalWithTypeAnnotationSpecialCases] |
| 380 |
# flags: --allow-redefinition-old |
| 381 |
def f() -> None: |
| 382 |
x: object |
| 383 |
x = 1 |
| 384 |
if int(): |
| 385 |
x = '' |
| 386 |
reveal_type(x) # N: Revealed type is "builtins.str | builtins.int" |
| 387 |
x = '' |
| 388 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 389 |
if int(): |
| 390 |
x = 2 \ |
| 391 |
# E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 392 |
|
| 393 |
|
| 394 |
[case testCannotRedefineSelf] |
| 395 |
# flags: --allow-redefinition-old |
| 396 |
class A: |
| 397 |
x = 0 |
| 398 |
|
| 399 |
def f(self) -> None: |
| 400 |
reveal_type(self.x) # N: Revealed type is "builtins.int" |
| 401 |
self = f() |
| 402 |
self.y: str = '' |
| 403 |
reveal_type(self.y) # N: Revealed type is "builtins.str" |
| 404 |
|
| 405 |
def f() -> A: return A() |
| 406 |
|
| 407 |
|
| 408 |
-- Redefine global variable |
| 409 |
-- ------------------------ |
| 410 |
|
| 411 |
|
| 412 |
[case testRedefineGlobalWithDifferentType] |
| 413 |
# flags: --allow-redefinition-old |
| 414 |
import m |
| 415 |
reveal_type(m.x) # N: Revealed type is "builtins.str" |
| 416 |
[file m.py] |
| 417 |
x = 0 |
| 418 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 419 |
x = object() |
| 420 |
reveal_type(x) # N: Revealed type is "builtins.object" |
| 421 |
x = '' |
| 422 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 423 |
|
| 424 |
[case testRedefineGlobalForIndex] |
| 425 |
# flags: --allow-redefinition-old |
| 426 |
import m |
| 427 |
reveal_type(m.x) # N: Revealed type is "builtins.str" |
| 428 |
[file m.py] |
| 429 |
from typing import Iterable |
| 430 |
def f(): pass |
| 431 |
it1: Iterable[int] = f() |
| 432 |
it2: Iterable[str] = f() |
| 433 |
for x in it1: |
| 434 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 435 |
for x in it2: |
| 436 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 437 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 438 |
|
| 439 |
[case testRedefineGlobalBasedOnPreviousValues] |
| 440 |
# flags: --allow-redefinition-old |
| 441 |
from typing import TypeVar, Iterable |
| 442 |
T = TypeVar('T') |
| 443 |
def f(x: T) -> Iterable[T]: pass |
| 444 |
a = 0 |
| 445 |
a = f(a) |
| 446 |
reveal_type(a) # N: Revealed type is "typing.Iterable[builtins.int]" |
| 447 |
|
| 448 |
[case testRedefineGlobalWithSeparateDeclaration] |
| 449 |
# flags: --allow-redefinition-old |
| 450 |
x = '' |
| 451 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 452 |
x: int |
| 453 |
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 454 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 455 |
x: object |
| 456 |
x = 1 |
| 457 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 458 |
if int(): |
| 459 |
x = object() |
| 460 |
|
| 461 |
[case testRedefineGlobalUsingForLoop] |
| 462 |
# flags: --allow-redefinition-old |
| 463 |
from typing import Iterable, TypeVar, Union |
| 464 |
T = TypeVar('T') |
| 465 |
def f(x: T) -> Iterable[Union[T, str]]: pass |
| 466 |
x = 0 |
| 467 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 468 |
for x in f(x): |
| 469 |
pass |
| 470 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 471 |
|
| 472 |
[case testNoRedefinitionIfOnlyInitialized] |
| 473 |
# flags: --allow-redefinition-old --no-strict-optional |
| 474 |
x = None # type: int |
| 475 |
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 476 |
x = object() # E: Incompatible types in assignment (expression has type "object", variable has type "int") |
| 477 |
x # Reference to variable |
| 478 |
x = '' |
| 479 |
|
| 480 |
y = 0 |
| 481 |
y = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 482 |
|
| 483 |
[case testNoRedefinitionIfNoValueAssigned] |
| 484 |
# flags: --allow-redefinition-old |
| 485 |
x: int |
| 486 |
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 487 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 488 |
x: object |
| 489 |
|
| 490 |
[case testNoRedefinitionIfExplicitlyDisallowed] |
| 491 |
# flags: --disallow-redefinition-old |
| 492 |
x = 0 |
| 493 |
x = 2 |
| 494 |
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 495 |
def f() -> None: |
| 496 |
y = 0 |
| 497 |
y = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 498 |
class C: |
| 499 |
y = 0 |
| 500 |
y = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 501 |
def g() -> None: |
| 502 |
# _ is a special case |
| 503 |
_ = 0 |
| 504 |
_ = '' |
| 505 |
x, _ = 0, C() |
| 506 |
[builtins fixtures/tuple.pyi] |
| 507 |
|
| 508 |
[case testRedefineAsException] |
| 509 |
# flags: --allow-redefinition-old |
| 510 |
e = 1 |
| 511 |
reveal_type(e) # N: Revealed type is "builtins.int" |
| 512 |
try: |
| 513 |
pass |
| 514 |
except Exception as e: |
| 515 |
reveal_type(e) # N: Revealed type is "builtins.Exception" |
| 516 |
e = '' |
| 517 |
reveal_type(e) # N: Revealed type is "builtins.str" |
| 518 |
[builtins fixtures/exception.pyi] |
| 519 |
|
| 520 |
[case testRedefineUsingWithStatement] |
| 521 |
# flags: --allow-redefinition-old |
| 522 |
class A: |
| 523 |
def __enter__(self) -> int: ... |
| 524 |
def __exit__(self, x, y, z) -> None: ... |
| 525 |
class B: |
| 526 |
def __enter__(self) -> str: ... |
| 527 |
def __exit__(self, x, y, z) -> None: ... |
| 528 |
with A() as x: |
| 529 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 530 |
with B() as x: |
| 531 |
x = 0 # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 532 |
|
| 533 |
|
| 534 |
[case testRedefineModuleAsException] |
| 535 |
import typing |
| 536 |
try: |
| 537 |
pass |
| 538 |
except Exception as typing: |
| 539 |
pass |
| 540 |
[builtins fixtures/exception.pyi] |
| 541 |
[typing fixtures/typing-full.pyi] |
| 542 |
|
| 543 |
[case testRedefiningUnderscoreFunctionIsntAnError] |
| 544 |
def _(arg): |
| 545 |
pass |
| 546 |
|
| 547 |
def _(arg): |
| 548 |
pass |
| 549 |
|
| 550 |
[case testTypeErrorsInUnderscoreFunctionsReported] |
| 551 |
def _(arg: str): |
| 552 |
x = arg + 1 # E: Unsupported left operand type for + ("str") |
| 553 |
|
| 554 |
def _(arg: int) -> int: |
| 555 |
return 'a' # E: Incompatible return value type (got "str", expected "int") |
| 556 |
|
| 557 |
[case testFunctionStillTypeCheckedWhenAliasedAsUnderscoreDuringImport] |
| 558 |
from a import f as _ |
| 559 |
|
| 560 |
_(1) # E: Argument 1 to "f" has incompatible type "int"; expected "str" |
| 561 |
reveal_type(_('a')) # N: Revealed type is "builtins.str" |
| 562 |
|
| 563 |
[file a.py] |
| 564 |
def f(arg: str) -> str: |
| 565 |
return arg |
| 566 |
|
| 567 |
[case testCallToFunctionStillTypeCheckedWhenAssignedToUnderscoreVariable] |
| 568 |
from a import g |
| 569 |
_ = g |
| 570 |
|
| 571 |
_('a') # E: Argument 1 has incompatible type "str"; expected "int" |
| 572 |
reveal_type(_(1)) # N: Revealed type is "builtins.int" |
| 573 |
|
| 574 |
[file a.py] |
| 575 |
def g(arg: int) -> int: |
| 576 |
return arg |
| 577 |
|
| 578 |
[case testRedefiningUnderscoreFunctionWithDecoratorWithUnderscoreFunctionsNextToEachOther] |
| 579 |
def dec(f): |
| 580 |
return f |
| 581 |
|
| 582 |
@dec |
| 583 |
def _(arg): |
| 584 |
pass |
| 585 |
|
| 586 |
@dec |
| 587 |
def _(arg): |
| 588 |
pass |
| 589 |
|
| 590 |
[case testRedefiningUnderscoreFunctionWithDecoratorInDifferentPlaces] |
| 591 |
def dec(f): |
| 592 |
return f |
| 593 |
|
| 594 |
def dec2(f): |
| 595 |
return f |
| 596 |
|
| 597 |
@dec |
| 598 |
def _(arg): |
| 599 |
pass |
| 600 |
|
| 601 |
def f(arg): |
| 602 |
pass |
| 603 |
|
| 604 |
@dec2 |
| 605 |
def _(arg): |
| 606 |
pass |
| 607 |
|
| 608 |
[case testOverwritingImportedFunctionThatWasAliasedAsUnderscore] |
| 609 |
from a import f as _ |
| 610 |
|
| 611 |
def _(arg: str) -> str: # E: Name "_" already defined (possibly by an import) |
| 612 |
return arg |
| 613 |
|
| 614 |
[file a.py] |
| 615 |
def f(s: str) -> str: |
| 616 |
return s |