Skip to content

Commit 61eb912

Browse files
ilevkivskyiJukkaL
authored andcommitted
Fix type variables leaking from inference (#7113)
Fixes #7101. This fixes another manifestation of an old issue where type variables can leak where a generic function is passed as an argument to another generic function.
1 parent 9e91dc0 commit 61eb912

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

mypy/checker.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2622,7 +2622,10 @@ def set_inference_error_fallback_type(self, var: Var, lvalue: Lvalue, type: Type
26222622
26232623
We implement this here by giving x a valid type (replacing inferred <nothing> with Any).
26242624
"""
2625-
self.set_inferred_type(var, lvalue, type.accept(SetNothingToAny()))
2625+
fallback = type.accept(SetNothingToAny())
2626+
# Type variables may leak from inference, see https://github.com/python/mypy/issues/5738,
2627+
# we therefore need to erase them.
2628+
self.set_inferred_type(var, lvalue, erase_typevars(fallback))
26262629

26272630
def check_simple_assignment(self, lvalue_type: Optional[Type], rvalue: Expression,
26282631
context: Context,

test-data/unit/python2eval.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,3 +419,12 @@ if MYPY:
419419
[file lib.pyi]
420420
x = b'abc'
421421
[out]
422+
423+
[case testNestedGenericFailedInference]
424+
from collections import defaultdict
425+
def foo() -> None:
426+
x = defaultdict(list) # type: ignore
427+
x['lol'].append(10)
428+
reveal_type(x)
429+
[out]
430+
_testNestedGenericFailedInference.py:5: note: Revealed type is 'collections.defaultdict[Any, builtins.list[Any]]'

0 commit comments

Comments
 (0)