8000 Fix crash when a star expression is used in isinstance by onlined · Pull Request #6659 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 6 commits into from
Apr 12, 2019

Conversation

onlined
Copy link
Contributor
@onlined onlined commented Apr 11, 2019

Fixes #4986.

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.

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)
Copy link
Member

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)

Copy link
Contributor Author
@onlined onlined Apr 11, 2019

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.

Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I got it, thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

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.

Thanks, there is one more comment about the added tests.

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.

Thanks, LGTM now.

@ilevkivskyi ilevkivskyi merged commit 9a4cfae into python:master Apr 12, 2019
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.

isinstance() tuple unpacking raises mypy internal error
2 participants
0