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
Merged
Show file tree
Hide file tree
Changes from all 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
10000
2 changes: 2 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

else:
return [t]

Expand Down
26 changes: 26 additions & 0 deletions test-data/unit/check-isinstance.test
Original file line number Diff line number Diff line change
Expand Up @@ -2207,3 +2207,29 @@ def bar(x: Union[List[str], List[int], None]) -> None:
assert isinstance(x, list)
reveal_type(x) # E: Revealed type is 'Union[builtins.list[builtins.str], builtins.list[builtins.int]]'
[builtins fixtures/isinstancelist.pyi]

[case testIsInstanceWithStarExpression]
from typing import Union, List, Tuple

def f(var: Union[List[str], Tuple[str, str], str]) -> None:
reveal_type(var) # E: Revealed type is 'Union[builtins.list[builtins.str], Tuple[builtins.str, builtins.str], builtins.str]'
if isinstance(var, (list, *(str, int))):
reveal_type(var) # E: Revealed type is 'Union[builtins.list[builtins.str], builtins.str]'
[builtins fixtures/isinstancelist.pyi]

[case testIsInstanceWithStarExpressionAndVariable]
from typing import Union

def f(var: Union[int, str]) -> None:
reveal_type(var) # E: Revealed type is 'Union[builtins.int, builtins.str]'
some_types = (str, tuple)
another_type = list
if isinstance(var, (*some_types, another_type)):
reveal_type(var) # E: Revealed type is 'builtins.str'
[builtins fixtures/isinstancelist.pyi]

[case testIsInstanceWithWrongStarExpression]
var = 'some string'
if isinstance(var, *(str, int)): # E: Too many arguments for "isinstance"
pass
[builtins fixtures/isinstancelist.pyi]
0