Skip to content

Commit ac3e5ce

Browse files
authored
Fix type inference regression with partial types (python#8091)
Don't infer types such as List[<nothing>]; infer List[Any] instead. Fixes python#8090.
1 parent 5680f5c commit ac3e5ce

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

mypy/checker.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,7 +2173,7 @@ def try_infer_partial_generic_type_from_assignment(self,
21732173
rvalue_type = self.expr_checker.accept(rvalue)
21742174
rvalue_type = get_proper_type(rvalue_type)
21752175
if isinstance(rvalue_type, Instance):
2176-
if rvalue_type.type == typ.type:
2176+
if rvalue_type.type == typ.type and is_valid_inferred_type(rvalue_type):
21772177
var.type = rvalue_type
21782178
del partial_types[var]
21792179
elif isinstance(rvalue_type, AnyType):
@@ -2855,10 +2855,14 @@ def set_inference_error_fallback_type(self, var: Var, lvalue: Lvalue, type: Type
28552855
28562856
We implement this here by giving x a valid type (replacing inferred <nothing> with Any).
28572857
"""
2858+
fallback = self.inference_error_fallback_type(type)
2859+
self.set_inferred_type(var, lvalue, fallback)
2860+
2861+
def inference_error_fallback_type(self, type: Type) -> Type:
28582862
fallback = type.accept(SetNothingToAny())
28592863
# Type variables may leak from inference, see https://github.com/python/mypy/issues/5738,
28602864
# we therefore need to erase them.
2861-
self.set_inferred_type(var, lvalue, erase_typevars(fallback))
2865+
return erase_typevars(fallback)
28622866

28632867
def check_simple_assignment(self, lvalue_type: Optional[Type], rvalue: Expression,
28642868
context: Context,

test-data/unit/check-inference.test

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,29 @@ oo.update(d)
15851585
reveal_type(oo) # N: Revealed type is 'collections.OrderedDict[builtins.int*, builtins.str*]'
15861586
[builtins fixtures/dict.pyi]
15871587

1588+
[case testEmptyCollectionAssignedToVariableTwiceIncremental]
1589+
x = [] # E: Need type annotation for 'x' (hint: "x: List[<type>] = ...")
1590+
y = x
1591+
x = []
1592+
reveal_type(x) # N: Revealed type is 'builtins.list[Any]'
1593+
d = {} # E: Need type annotation for 'd' (hint: "d: Dict[<type>, <type>] = ...")
1594+
z = d
1595+
d = {}
1596+
reveal_type(d) # N: Revealed type is 'builtins.dict[Any, Any]'
1597+
[builtins fixtures/dict.pyi]
1598+
[out2]
1599+
main:1: error: Need type annotation for 'x' (hint: "x: List[<type>] = ...")
1600+
main:4: note: Revealed type is 'builtins.list[Any]'
1601+
main:5: error: Need type annotation for 'd' (hint: "d: Dict[<type>, <type>] = ...")
1602+
main:8: note: Revealed type is 'builtins.dict[Any, Any]'
1603+
1604+
[case testEmptyCollectionAssignedToVariableTwiceNoReadIncremental]
1605+
x = [] # E: Need type annotation for 'x' (hint: "x: List[<type>] = ...")
1606+
x = []
1607+
[builtins fixtures/list.pyi]
1608+
[out2]
1609+
main:1: error: Need type annotation for 'x' (hint: "x: List[<type>] = ...")
1610+
15881611
[case testInferAttributeInitializedToEmptyAndAssigned]
15891612
class C:
15901613
def __init__(self) -> None:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy