Skip to content

Commit

Permalink
Fix self-check (and therefore mypyc) on older Python versions (#7176)
Browse files Browse the repository at this point in the history
This also implements one of my old wishes: `UnionType.make_simplified_union()` should not mutate its argument.
  • Loading branch information
ilevkivskyi committed Jul 8, 2019
1 parent 37fdc36 commit c04091b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
5 changes: 2 additions & 3 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4545,9 +4545,8 @@ class Status(Enum):
this function will return Literal[Color.RED, Color.BLUE, Color.YELLOW, Status].
"""
if isinstance(typ, UnionType):
new_items = [try_expanding_enum_to_union(item, target_fullname)
for item in typ.items]
return UnionType.make_simplified_union(new_items)
items = [try_expanding_enum_to_union(item, target_fullname) for item in typ.items]
return UnionType.make_simplified_union(items)
elif isinstance(typ, Instance) and typ.type.is_enum and typ.type.fullname() == target_fullname:
new_items = []
for name, symbol in typ.type.names.items():
Expand Down
5 changes: 3 additions & 2 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,7 @@ def make_union(items: List[Type], line: int = -1, column: int = -1) -> Type:
return UninhabitedType()

@staticmethod
def make_simplified_union(items: List[Type], line: int = -1, column: int = -1) -> Type:
def make_simplified_union(items: Sequence[Type], line: int = -1, column: int = -1) -> Type:
"""Build union type with redundant union items removed.
If only a single item remains, this may return a non-union type.
Expand All @@ -1623,6 +1623,7 @@ def make_simplified_union(items: List[Type], line: int = -1, column: int = -1) -
"""
# TODO: Make this a function living somewhere outside mypy.types. Most other non-trivial
# type operations are not static methods, so this is inconsistent.
items = list(items)
while any(isinstance(typ, UnionType) for typ in items):
all_items = [] # type: List[Type]
for typ in items:
Expand All @@ -1640,7 +1641,7 @@ def make_simplified_union(items: List[Type], line: int = -1, column: int = -1) -
# Keep track of the truishness info for deleted subtypes which can be relevant
cbt = cbf = False
for j, tj in enumerate(items):
if (i != j and is_proper_subtype(tj, ti)):
if i != j and is_proper_subtype(tj, ti):
# We found a redundant item in the union.
removed.add(j)
cbt = cbt or tj.can_be_true
Expand Down

0 comments on commit c04091b

Please sign in to comment.