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

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Dec 5, 2019

Don't infer types such as List[<nothing>]; infer List[Any] instead.

Fixes #8090.

@JukkaL JukkaL requested a review from ilevkivskyi December 5, 2019 14:30
@JukkaL
Copy link
Collaborator Author

JukkaL commented Dec 5, 2019

Not ready to merge -- there seems to a problem with this PR.

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

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

This fix looks better, I have couple optional suggestions.

mypy/checker.py Outdated
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.

mypy/checker.py Outdated
@@ -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.

@JukkaL JukkaL merged commit ac3e5ce into master Dec 6, 2019
@gvanrossum gvanrossum deleted the partial-regression branch August 26, 2020 21:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression: type inference can fall back to List[<nothing>]
2 participants