Skip to content

Commit 2aa1337

Browse files
committed
address review comments and misc cleanups
1 parent 062cb81 commit 2aa1337

File tree

4 files changed

+16
-19
lines changed

4 files changed

+16
-19
lines changed

mypy/meet.py

+9-12
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,15 @@ def is_enum_overlapping_union(x: ProperType, y: ProperType) -> bool:
143143
return (
144144
isinstance(x, Instance) and x.type.is_enum and
145145
isinstance(y, UnionType) and
146-
all(isinstance(z, LiteralType) and z.fallback.type == x.type # type: ignore[misc]
147-
for z in y.items)
146+
all(x.type == p.fallback.type
147+
for p in (get_proper_type(z) for z in y.relevant_items())
148+
if isinstance(p, LiteralType))
148149
)
149150

150151

151152
def is_literal_in_union(x: ProperType, y: ProperType) -> bool:
152-
return (
153-
isinstance(x, LiteralType) and isinstance(y, UnionType) and any(
154-
isinstance(z, LiteralType) and z == x # type: ignore[misc]
155-
for z in y.items
156-
)
157-
)
153+
return (isinstance(x, LiteralType) and isinstance(y, UnionType) and
154+
any(x == get_proper_type(z) for z in y.items))
158155

159156

160157
def is_overlapping_types(left: Type,
@@ -223,10 +220,10 @@ def _is_overlapping_types(left: Type, right: Type) -> bool:
223220
# and crucially, we want to do that *fast* in case the enum is large
224221
# so we do it before expanding variants below to avoid O(n**2) behavior
225222
if (
226-
is_enum_overlapping_union(left, right) or
227-
is_enum_overlapping_union(right, left) or
228-
is_literal_in_union(left, right) or
229-
is_literal_in_union(right, left)
223+
is_enum_overlapping_union(left, right)
224+
or is_enum_overlapping_union(right, left)
225+
or is_literal_in_union(left, right)
226+
or is_literal_in_union(right, left)
230227
):
231228
return True
232229

mypy/sametypes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ def visit_union_type(self, left: UnionType) -> bool:
147147
def _extract_literals(u: UnionType) -> Tuple[Set[LiteralType], List[Type]]:
148148
lit: Set[LiteralType] = set()
149149
rem: List[Type] = []
150-
for i in u.items:
150+
for i in u.relevant_items():
151+
i = get_proper_type(i)
151152
if is_simple_literal(i):
152-
assert isinstance(i, LiteralType) # type: ignore[misc]
153153
lit.add(i)
154154
else:
155155
rem.append(i)

mypy/subtypes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -501,9 +501,9 @@ def visit_union_type(self, left: UnionType) -> bool:
501501
if isinstance(self.right, Instance):
502502
literal_types: Set[Instance] = set()
503503
# avoid redundant check for union of literals
504-
for item in left.items:
504+
for item in left.relevant_items():
505+
item = get_proper_type(item)
505506
if mypy.typeops.is_simple_literal(item):
506-
assert isinstance(item, LiteralType) # type: ignore[misc]
507507
if item.fallback in literal_types:
508508
continue
509509
literal_types.add(item.fallback)

mypy/typeops.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"""
77

88
from typing import cast, Optional, List, Sequence, Set, Iterable, TypeVar, Dict, Tuple, Any
9-
from typing_extensions import Type as TypingType
9+
from typing_extensions import Type as TypingType, TypeGuard
1010
import itertools
1111
import sys
1212

@@ -294,13 +294,13 @@ def callable_corresponding_argument(typ: CallableType,
294294
return by_name if by_name is not None else by_pos
295295

296296

297-
def is_simple_literal(t: Type) -> bool:
297+
def is_simple_literal(t: ProperType) -> TypeGuard[LiteralType]:
298298
"""
299299
Whether a type is a simple enough literal to allow for fast Union simplification
300300
301301
For now this means enum or string
302302
"""
303-
return isinstance(t, LiteralType) and ( # type: ignore[misc]
303+
return isinstance(t, LiteralType) and (
304304
t.fallback.type.is_enum or t.fallback.type.fullname == 'builtins.str'
305305
)
306306

0 commit comments

Comments
 (0)