-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Fix crash when a star expression is used in isinstance #6659
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 crash when a star expression is used in isinstance #6659
Conversation
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.
Thanks! The fix looks good, I just have a suggestion for few extra tests.
@@ -3949,6 +3949,8 @@ def flatten(t: Expression) -> List[Expression]: | |||
"""Flatten a nested sequence of tuples/lists into one list of nodes.""" | |||
if isinstance(t, TupleExpr) or isinstance(t, ListExpr): | |||
return [b for a in t.items for b in flatten(a)] | |||
elif isinstance(t, StarExpr): | |||
return flatten(t.expr) |
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.
Technically this adds some non-trivial functionality. Could you please add tests that check the behavior (i.e. whether types are narrowed or nor) for situations like:
isinstance(x, *(str, int)) # This should fail IIUC
isinstance(x, (int, *(str, list))) # This should behave the same as (int, str, list)
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.
I did not understand the difference between the second one and my proposed test.
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.
Basically your test only tests that mypy doesn't crash, while I want:
- check that type narrowing happens when it should, and doesn't happen when it shouldn't
- check both situations where tuple appears in place and where it is referenced by a variable
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.
I got it, thanks.
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.
Done.
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.
Thanks, there is one more comment about the added tests.
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.
Thanks, LGTM now.
Fixes #4986.