Skip to content

Fix type inference regression with partial types #8091

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

Merged
merged 4 commits into from
Dec 6, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 10 additions & 2 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2174,7 +2174,10 @@ def try_infer_partial_generic_type_from_assignment(self,
rvalue_type = get_proper_type(rvalue_type)
if isinstance(rvalue_type, Instance):
if rvalue_type.type == typ.type:
var.type = rvalue_type
if is_valid_inferred_type(rvalue_type):
var.type = rvalue_type
else:
var.type = self.inference_error_fallback_type(rvalue_type)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if one just assigns the variable twice (without reading it), would it silence the error on first assignment? Not that it is too bad, but still may be surprising.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, a good catch! This behavior is inconsistent. I'll try to fix it.

del partial_types[var]
elif isinstance(rvalue_type, AnyType):
var.type = fill_typevars_with_any(typ.type)
Expand Down Expand Up @@ -2785,6 +2788,7 @@ def infer_variable_type(self, name: Var, lvalue: Lvalue,
init_type = get_proper_type(init_type)
if isinstance(init_type, DeletedType):
self.msg.deleted_as_rvalue(init_type, context)
name.type = AnyType(TypeOfAny.from_error)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a test for this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, and this is kind of an unrelated thing that I encountered when trying a different fix. I might take this out of this PR.

elif not is_valid_inferred_type(init_type) and not self.no_partial_types:
# We cannot use the type of the initialization expression for full type
# inference (it's not specific enough), but we might be able to give
Expand Down Expand Up @@ -2855,10 +2859,14 @@ 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).
"""
fallback = self.inference_error_fallback_type(type)
self.set_inferred_type(var, lvalue, fallback)

def inference_error_fallback_type(self, type: Type) -> Type:
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))
return erase_typevars(fallback)

def check_simple_assignment(self, lvalue_type: Optional[Type], rvalue: Expression,
context: Context,
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -1585,6 +1585,22 @@ oo.update(d)
reveal_type(oo) # N: Revealed type is 'collections.OrderedDict[builtins.int*, builtins.str*]'
[builtins fixtures/dict.pyi]

[case testEmptyCollectionAssignedToVariableTwiceIncremental]
x = [] # E: Need type annotation for 'x' (hint: "x: List[<type>] = ...")
y = x
x = []
reveal_type(x) # N: Revealed type is 'builtins.list[Any]'
d = {} # E: Need type annotation for 'd' (hint: "d: Dict[<type>, <type>] = ...")
z = d
d = {}
reveal_type(d) # N: Revealed type is 'builtins.dict[Any, Any]'
[builtins fixtures/dict.pyi]
[out2]
main:1: error: Need type annotation for 'x' (hint: "x: List[<type>] = ...")
main:4: note: Revealed type is 'builtins.list[Any]'
main:5: error: Need type annotation for 'd' (hint: "d: Dict[<type>, <type>] = ...")
main:8: note: Revealed type is 'builtins.dict[Any, Any]'

[case testInferAttributeInitializedToEmptyAndAssigned]
class C:
def __init__(self) -> None:
Expand Down