@@ -1367,34 +1367,29 @@ a = []
1367
1367
a.append(1)
1368
1368
a.append('') # E: Argument 1 to "append" of "list" has incompatible type "str"; expected "int"
1369
1369
[builtins fixtures/list.pyi]
1370
- [out]
1371
1370
1372
1371
[case testInferListInitializedToEmptyUsingUpdate]
1373
1372
a = []
1374
1373
a.extend([''])
1375
1374
a.append(0) # E: Argument 1 to "append" of "list" has incompatible type "int"; expected "str"
1376
1375
[builtins fixtures/list.pyi]
1377
- [out]
1378
1376
1379
1377
[case testInferListInitializedToEmptyAndNotAnnotated]
1380
1378
a = [] # E: Need type annotation for 'a' (hint: "a: List[<type>] = ...")
1381
1379
[builtins fixtures/list.pyi]
1382
- [out]
1383
1380
1384
1381
[case testInferListInitializedToEmptyAndReadBeforeAppend]
1385
1382
a = [] # E: Need type annotation for 'a' (hint: "a: List[<type>] = ...")
1386
1383
if a: pass
1387
1384
a.xyz # E: "List[Any]" has no attribute "xyz"
1388
1385
a.append('')
1389
1386
[builtins fixtures/list.pyi]
1390
- [out]
1391
1387
1392
1388
[case testInferListInitializedToEmptyAndIncompleteTypeInAppend]
1393
1389
a = [] # E: Need type annotation for 'a' (hint: "a: List[<type>] = ...")
1394
1390
a.append([])
1395
1391
a() # E: "List[Any]" not callable
1396
1392
[builtins fixtures/list.pyi]
1397
- [out]
1398
1393
1399
1394
[case testInferListInitializedToEmptyAndMultipleAssignment]
1400
1395
a, b = [], []
@@ -1403,15 +1398,13 @@ b.append('')
1403
1398
a() # E: "List[int]" not callable
1404
1399
b() # E: "List[str]" not callable
1405
1400
[builtins fixtures/list.pyi]
1406
- [out]
1407
1401
1408
1402
[case testInferListInitializedToEmptyInFunction]
1409
1403
def f() -> None:
1410
1404
a = []
1411
1405
a.append(1)
1412
1406
a.append('') # E: Argument 1 to "append" of "list" has incompatible type "str"; expected "int"
1413
1407
[builtins fixtures/list.pyi]
1414
- [out]
1415
1408
1416
1409
[case testInferListInitializedToEmptyAndNotAnnotatedInFunction]
1417
1410
def f() -> None:
@@ -1422,7 +1415,6 @@ def g() -> None: pass
1422
1415
a = []
1423
1416
a.append(1)
1424
1417
[builtins fixtures/list.pyi]
1425
- [out]
1426
1418
1427
1419
[case testInferListInitializedToEmptyAndReadBeforeAppendInFunction]
1428
1420
def f() -> None:
@@ -1431,15 +1423,13 @@ def f() -> None:
1431
1423
a.xyz # E: "List[Any]" has no attribute "xyz"
1432
1424
a.append('')
1433
1425
[builtins fixtures/list.pyi]
1434
- [out]
1435
1426
1436
1427
[case testInferListInitializedToEmptyInClassBody]
1437
1428
class A:
1438
1429
a = []
1439
1430
a.append(1)
1440
1431
a.append('') # E: Argument 1 to "append" of "list" has incompatible type "str"; expected "int"
1441
1432
[builtins fixtures/list.pyi]
1442
- [out]
1443
1433
1444
1434
[case testInferListInitializedToEmptyAndNotAnnotatedInClassBody]
1445
1435
class A:
@@ -1449,7 +1439,6 @@ class B:
1449
1439
a = []
1450
1440
a.append(1)
1451
1441
[builtins fixtures/list.pyi]
1452
- [out]
1453
1442
1454
1443
[case testInferListInitializedToEmptyInMethod]
1455
1444
class A:
@@ -1458,14 +1447,12 @@ class A:
1458
1447
a.append(1)
1459
1448
a.append('') # E: Argument 1 to "append" of "list" has incompatible type "str"; expected "int"
1460
1449
[builtins fixtures/list.pyi]
1461
- [out]
1462
1450
1463
1451
[case testInferListInitializedToEmptyAndNotAnnotatedInMethod]
1464
1452
class A:
1465
1453
def f(self) -> None:
1466
1454
a = [] # E: Need type annotation for 'a' (hint: "a: List[<type>] = ...")
1467
1455
[builtins fixtures/list.pyi]
1468
- [out]
1469
1456
1470
1457
[case testInferListInitializedToEmptyInMethodViaAttribute]
1471
1458
class A:
@@ -1475,7 +1462,6 @@ class A:
1475
1462
self.a.append(1)
1476
1463
self.a.append('')
1477
1464
[builtins fixtures/list.pyi]
1478
- [out]
1479
1465
1480
1466
[case testInferListInitializedToEmptyInClassBodyAndOverriden]
1481
1467
from typing import List
@@ -1490,57 +1476,49 @@ class B(A):
1490
1476
def x(self) -> List[int]: # E: Signature of "x" incompatible with supertype "A"
1491
1477
return [123]
1492
1478
[builtins fixtures/list.pyi]
1493
- [out]
1494
1479
1495
1480
[case testInferSetInitializedToEmpty]
1496
1481
a = set()
1497
1482
a.add(1)
1498
1483
a.add('') # E: Argument 1 to "add" of "set" has incompatible type "str"; expected "int"
1499
1484
[builtins fixtures/set.pyi]
1500
- [out]
1501
1485
1502
1486
[case testInferSetInitializedToEmptyUsingDiscard]
1503
1487
a = set()
1504
1488
a.discard('')
1505
1489
a.add(0) # E: Argument 1 to "add" of "set" has incompatible type "int"; expected "str"
1506
1490
[builtins fixtures/set.pyi]
1507
- [out]
1508
1491
1509
1492
[case testInferSetInitializedToEmptyUsingUpdate]
1510
1493
a = set()
1511
1494
a.update({0})
1512
1495
a.add('') # E: Argument 1 to "add" of "set" has incompatible type "str"; expected "int"
1513
1496
[builtins fixtures/set.pyi]
1514
- [out]
1515
1497
1516
1498
[case testInferDictInitializedToEmpty]
1517
1499
a = {}
1518
1500
a[1] = ''
1519
1501
a() # E: "Dict[int, str]" not callable
1520
1502
[builtins fixtures/dict.pyi]
1521
- [out]
1522
1503
1523
1504
[case testInferDictInitializedToEmptyUsingUpdate]
1524
1505
a = {}
1525
1506
a.update({'': 42})
1526
1507
a() # E: "Dict[str, int]" not callable
1527
1508
[builtins fixtures/dict.pyi]
1528
- [out]
1529
1509
1530
1510
[case testInferDictInitializedToEmptyUsingUpdateError]
1531
1511
a = {} # E: Need type annotation for 'a' (hint: "a: Dict[<type>, <type>] = ...")
1532
1512
a.update([1, 2]) # E: Argument 1 to "update" of "dict" has incompatible type "List[int]"; expected "Mapping[Any, Any]"
1533
1513
a() # E: "Dict[Any, Any]" not callable
1534
1514
[builtins fixtures/dict.pyi]
1535
- [out]
1536
1515
1537
1516
[case testInferDictInitializedToEmptyAndIncompleteTypeInUpdate]
1538
1517
a = {} # E: Need type annotation for 'a' (hint: "a: Dict[<type>, <type>] = ...")
1539
1518
a[1] = {}
1540
1519
b = {} # E: Need type annotation for 'b' (hint: "b: Dict[<type>, <type>] = ...")
1541
1520
b[{}] = 1
1542
1521
[builtins fixtures/dict.pyi]
1543
- [out]
1544
1522
1545
1523
[case testInferDictInitializedToEmptyAndUpdatedFromMethod]
1546
1524
map = {}
@@ -1557,20 +1535,42 @@ def add():
1557
1535
[case testSpecialCaseEmptyListInitialization]
1558
1536
def f(blocks: Any): # E: Name 'Any' is not defined \
1559
1537
# N: Did you forget to import it from "typing"? (Suggestion: "from typing import Any")
1560
- to_process = [] # E: Need type annotation for 'to_process' (hint: "to_process: List[<type>] = ...")
1538
+ to_process = []
1561
1539
to_process = list(blocks)
1562
1540
[builtins fixtures/list.pyi]
1563
- [out]
1564
1541
1565
1542
[case testSpecialCaseEmptyListInitialization2]
1566
1543
def f(blocks: object):
1567
- to_process = [] # E: Need type annotation for 'to_process' (hint: "to_process: List[<type>] = ...")
1544
+ to_process = []
1568
1545
to_process = list(blocks) # E: No overload variant of "list" matches argument type "object" \
1569
1546
# N: Possible overload variant: \
1570
1547
# N: def [T] __init__(self, x: Iterable[T]) -> List[T] \
1571
1548
# N: <1 more non-matching overload not shown>
1572
1549
[builtins fixtures/list.pyi]
1573
- [out]
1550
+
1551
+ [case testInferListInitializedToEmptyAndAssigned]
1552
+ a = []
1553
+ if bool():
1554
+ a = [1]
1555
+ reveal_type(a) # N: Revealed type is 'builtins.list[builtins.int*]'
1556
+
1557
+ def f():
1558
+ return [1]
1559
+ b = []
1560
+ if bool():
1561
+ b = f()
1562
+ reveal_type(b) # N: Revealed type is 'builtins.list[Any]'
1563
+
1564
+ d = {}
1565
+ if bool():
1566
+ d = {1: 'x'}
1567
+ reveal_type(d) # N: Revealed type is 'builtins.dict[builtins.int*, builtins.str*]'
1568
+
1569
+ dd = {} # E: Need type annotation for 'dd' (hint: "dd: Dict[<type>, <type>] = ...")
1570
+ if bool():
1571
+ dd = [1] # E: Incompatible types in assignment (expression has type "List[int]", variable has type "Dict[Any, Any]")
1572
+ reveal_type(dd) # N: Revealed type is 'builtins.dict[Any, Any]'
1573
+ [builtins fixtures/dict.pyi]
1574
1574
1575
1575
1576
1576
-- Inferring types of variables first initialized to None (partial types)
0 commit comments