10000 Allow assignments to multiple targets from union types by ilevkivskyi · Pull Request #4067 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Allow assignments to multiple targets from union types #4067

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 11 commits into from
Oct 11, 2017
Prev Previous commit
Next Next commit
3.5.1, again
  • Loading branch information
Ivan Levkivskyi committed Oct 6, 2017
commit 9a68ae6cafdb3aa143c91c1ac8ca7bb70a72350d
11 changes: 8 additions & 3 deletions mypy/binder.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from typing import Dict, List, Set, Iterator, Union, Optional, Tuple, DefaultDict, cast
from typing import Dict, List, Set, Iterator, Union, Optional, Tuple, cast
from contextlib import contextmanager
from collections import defaultdict

MYPY = False
if MYPY:
from typing import DefaultDict

from mypy.types import Type, AnyType, PartialType, UnionType, TypeOfAny
from mypy.subtypes import is_subtype
from mypy.join import join_simple
Expand Down Expand Up @@ -38,7 +42,8 @@ def __init__(self) -> None:
self.unreachable = False


Assigns = DefaultDict[Expression, List[Tuple[Type, Optional[Type]]]]
if MYPY:
Assigns = DefaultDict[Expression, List[Tuple[Type, Optional[Type]]]]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add comment that explains what this is used for.



class ConditionalTypeBinder:
Expand Down Expand Up @@ -216,7 +221,7 @@ def pop_frame(self, can_skip: bool, fall_through: int) -> Frame:
return result

@contextmanager
def accumulate_type_assignments(self) -> Iterator[Assigns]:
def accumulate_type_assignments(self) -> 'Iterator[Assigns]':
self.type_assignments = defaultdict(list)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add docstring. Also mention that this is used for multi-assignment from union (and why this is needed there).

yield self.type_assignments
self.type_assignments = None
Copy link
Collaborator

Choose a reason for hiding this comment

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

What about things like (x, (y, z)) = ... where there are two levels of union type multiple assignment? Would we need to restore old mapping perhaps? This would be worth testing as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, you are totally right. Will push a commit in a second. Also testing this uncovered another flaw in my implementation: I need to iterate over nested lvalues in for loops (added a test for this as well).

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

Expand Down
0