Skip to content

Commit df9f695

Browse files
committed
Use narrow_declared_type instead of meet_types for type narrowing
1 parent 7a5f33e commit df9f695

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

mypy/checker.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@
4848
from mypy.expandtype import expand_type
4949
from mypy.literals import Key, extract_var_from_literal_hash, literal, literal_hash
5050
from mypy.maptype import map_instance_to_supertype
51-
from mypy.meet import is_overlapping_erased_types, is_overlapping_types, meet_types
51+
from mypy.meet import (
52+
is_overlapping_erased_types,
53+
is_overlapping_types,
54+
meet_types,
55+
narrow_declared_type,
56+
)
5257
from mypy.message_registry import ErrorMessage
5358
from mypy.messages import (
5459
SUGGESTED_TEST_FIXTURES,
@@ -6292,26 +6297,28 @@ def update_fixed_type(new_fixed_type: Type, new_is_final: bool) -> bool:
62926297
if_maps = []
62936298
else_maps = []
62946299
for expr in exprs_in_type_calls:
6295-
expr_type = get_proper_type(self.lookup_type(expr))
6300+
expr_type: Type = get_proper_type(self.lookup_type(expr))
62966301
for type_range in target_types:
6297-
new_expr_type, _ = self.conditional_types_with_intersection(
6302+
restriction, _ = self.conditional_types_with_intersection(
62986303
expr_type, type_range, expr
62996304
)
6300-
if new_expr_type is not None:
6301-
new_expr_type = get_proper_type(new_expr_type)
6302-
if isinstance(expr_type, AnyType):
6303-
expr_type = new_expr_type
6304-
elif not isinstance(new_expr_type, AnyType):
6305-
expr_type = meet_types(expr_type, new_expr_type)
6305+
if restriction is not None:
6306+
narrowed_type = get_proper_type(narrow_declared_type(expr_type, restriction))
6307+
# Cannot be guaranteed that this is unreachable, so use fallback type.
6308+
if isinstance(narrowed_type, UninhabitedType):
6309+
expr_type = restriction
6310+
else:
6311+
expr_type = narrowed_type
63066312
_, else_map = conditional_types_to_typemaps(
63076313
expr,
63086314
*self.conditional_types_with_intersection(
63096315
(self.lookup_type(expr)), (type_range), expr
63106316
),
63116317
)
63126318
else_maps.append(else_map)
6319+
63136320
if fixed_type and expr_type is not None:
6314-
expr_type = meet_types(expr_type, fixed_type)
6321+
expr_type = narrow_declared_type(expr_type, fixed_type)
63156322

63166323
if_map, _ = conditional_types_to_typemaps(expr, expr_type, None)
63176324
if_maps.append(if_map)

test-data/unit/check-narrowing.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1262,7 +1262,7 @@ def f(t: Type[C]) -> None:
12621262
if type(t) is M:
12631263
reveal_type(t) # N: Revealed type is "type[__main__.C]"
12641264
else:
1265-
reveal_type(t) # N: Revealed type is "type[__main__.C]"
1265+
reveal_type(t) # N: Revealed type is "type[__main__.C]"
12661266
if type(t) is not M:
12671267
reveal_type(t) # N: Revealed type is "type[__main__.C]"
12681268
else:

0 commit comments

Comments
 (0)