Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type variables leaking from inference #7113

Merged
merged 1 commit into from
Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <nothing> 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,
Expand Down
9 changes: 9 additions & 0 deletions test-data/unit/python2eval.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]]'