-
Notifications
You must be signed in to change notification settings - Fork 0
Force List Expression to Infer Union Type of Member Types. #1
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
base: master
Are you sure you want to change the base?
Conversation
da03a38 to
f5cc357
Compare
lexik04
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks very well written
vsolskyyy
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well organized, I like your use of comments. Well done.
ErkeXia
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good and solve the problem. Just wonder when the isinstance(e, ListExpr) branch will be executed in infer_item_type
vsolskyyy
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well written.
KevnKey
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a good fix to the issue by inferring directly. I can see the union implementation.
Infer Union Types for List Expressions with Heterogeneous Elements
Description:
This PR forces the type inference for list expressions in mypy to correctly infer
Uniontypes when a list contains elements of different types that have no common supertype other thanobject. However, currently this forces this for all list expression type inferences and hence breaks everything else. A pretty quirky fix would be to track any list comprehension accesses since this only happens when a union type list is used in the generator portion of a list comprehension.Background:
Before, when a list literal was constructed with elements of disparate types without a meaningful common superclass, mypy would default to inferring the list's type as
List[object]. This behavior led to type-checking issues in scenarios where a more precise type was expected.For example:
However, while this fixes this issue, it also generalizes the entire list expression inference to be forced as well.