From 2f98510e0bb5194b3eed2e89a970c8aad4cb35ee Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Mon, 1 Jul 2019 12:30:24 +0100 Subject: [PATCH] Fix type variables leaking from inference --- mypy/checker.py | 5 ++++- test-data/unit/python2eval.test | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/mypy/checker.py b/mypy/checker.py index c3f0d28cc3a8..4ecf36376fbc 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -2622,7 +2622,10 @@ def set_inference_error_fallback_type(self, var: Var, lvalue: Lvalue, type: Type We implement this here by giving x a valid type (replacing inferred with Any). """ - self.set_inferred_type(var, lvalue, type.accept(SetNothingToAny())) + fallback = type.accept(SetNothingToAny()) + # Type variables may leak from inference, see https://github.com/python/mypy/issues/5738, + # we therefore need to erase them. + self.set_inferred_type(var, lvalue, erase_typevars(fallback)) def check_simple_assignment(self, lvalue_type: Optional[Type], rvalue: Expression, context: Context, diff --git a/test-data/unit/python2eval.test b/test-data/unit/python2eval.test index a9a3cffaf3c8..8e808122f09f 100644 --- a/test-data/unit/python2eval.test +++ b/test-data/unit/python2eval.test @@ -419,3 +419,12 @@ if MYPY: [file lib.pyi] x = b'abc' [out] + +[case testNestedGenericFailedInference] +from collections import defaultdict +def foo() -> None: + x = defaultdict(list) # type: ignore + x['lol'].append(10) + reveal_type(x) +[out] +_testNestedGenericFailedInference.py:5: note: Revealed type is 'collections.defaultdict[Any, builtins.list[Any]]'