34
34
Type , AnyType , CallableType , FunctionLike , Overloaded , TupleType , TypedDictType ,
35
35
Instance , NoneType , strip_type , TypeType , TypeOfAny ,
36
36
UnionType , TypeVarId , TypeVarType , PartialType , DeletedType , UninhabitedType , TypeVarDef ,
37
- true_only , false_only , function_type , is_named_instance , union_items , TypeQuery , LiteralType ,
37
+ function_type , is_named_instance , union_items , TypeQuery , LiteralType ,
38
38
is_optional , remove_optional , TypeTranslator , StarType , get_proper_type , ProperType ,
39
39
get_proper_types , is_literal_type
40
40
)
45
45
)
46
46
import mypy .checkexpr
47
47
from mypy .checkmember import (
48
- map_type_from_supertype , bind_self , erase_to_bound , type_object_type ,
49
- analyze_descriptor_access ,
48
+ analyze_descriptor_access , type_object_type ,
49
+ )
50
+ from mypy .typeops import (
51
+ map_type_from_supertype , bind_self , erase_to_bound , make_simplified_union ,
52
+ erase_def_to_union_or_bound , erase_to_union_or_bound ,
53
+ true_only , false_only ,
50
54
)
51
55
from mypy import message_registry
52
56
from mypy .subtypes import (
@@ -1539,7 +1543,7 @@ def get_op_other_domain(self, tp: FunctionLike) -> Optional[Type]:
1539
1543
raw_items = [self .get_op_other_domain (it ) for it in tp .items ()]
1540
1544
items = [it for it in raw_items if it ]
1541
1545
if items :
1542
- return UnionType . make_simplified_union (items )
1546
+ return make_simplified_union (items )
1543
1547
return None
1544
1548
else :
1545
1549
assert False , "Need to check all FunctionLike subtypes here"
@@ -1973,7 +1977,7 @@ def check_assignment(self, lvalue: Lvalue, rvalue: Expression, infer_lvalue_type
1973
1977
if not self .current_node_deferred :
1974
1978
# Partial type can't be final, so strip any literal values.
1975
1979
rvalue_type = remove_instance_last_known_values (rvalue_type )
1976
- inferred_type = UnionType . make_simplified_union (
1980
+ inferred_type = make_simplified_union (
1977
1981
[rvalue_type , NoneType ()])
1978
1982
self .set_inferred_type (var , lvalue , inferred_type )
1979
1983
else :
@@ -2417,7 +2421,7 @@ def check_multi_assignment_from_union(self, lvalues: List[Expression], rvalue: E
2417
2421
rv_type = item , undefined_rvalue = True )
2418
2422
for t , lv in zip (transposed , self .flatten_lvalues (lvalues )):
2419
2423
t .append (self .type_map .pop (lv , AnyType (TypeOfAny .special_form )))
2420
- union_types = tuple (UnionType . make_simplified_union (col ) for col in transposed )
2424
+ union_types = tuple (make_simplified_union (col ) for col in transposed )
2421
2425
for expr , items in assignments .items ():
2422
2426
# Bind a union of types collected in 'assignments' to every expression.
2423
2427
if isinstance (expr , StarExpr ):
@@ -2432,8 +2436,8 @@ def check_multi_assignment_from_union(self, lvalues: List[Expression], rvalue: E
2432
2436
2433
2437
types , declared_types = zip (* clean_items )
2434
2438
self .binder .assign_type (expr ,
2435
- UnionType . make_simplified_union (list (types )),
2436
- UnionType . make_simplified_union (list (declared_types )),
2439
+ make_simplified_union (list (types )),
2440
+ make_simplified_union (list (declared_types )),
2437
2441
False )
2438
2442
for union , lv in zip (union_types , self .flatten_lvalues (lvalues )):
2439
2443
# Properly store the inferred types.
@@ -2856,9 +2860,9 @@ def try_infer_partial_type_from_indexed_assignment(
2856
2860
# TODO: Don't infer things twice.
2857
2861
key_type = self .expr_checker .accept (lvalue .index )
2858
2862
value_type = self .expr_checker .accept (rvalue )
2859
- full_key_type = UnionType . make_simplified_union (
2863
+ full_key_type = make_simplified_union (
2860
2864
[key_type , var .type .inner_types [0 ]])
2861
- full_value_type = UnionType . make_simplified_union (
2865
+ full_value_type = make_simplified_union (
2862
2866
[value_type , var .type .inner_types [1 ]])
2863
2867
if (is_valid_inferred_type (full_key_type ) and
2864
2868
is_valid_inferred_type (full_value_type )):
@@ -3176,7 +3180,7 @@ def check_except_handler_test(self, n: Expression) -> Type:
3176
3180
3177
3181
all_types .append (exc_type )
3178
3182
3179
- return UnionType . make_simplified_union (all_types )
3183
+ return make_simplified_union (all_types )
3180
3184
3181
3185
def get_types_from_except_handler (self , typ : Type , n : Expression ) -> List [Type ]:
3182
3186
"""Helper for check_except_handler_test to retrieve handler types."""
@@ -3524,7 +3528,7 @@ def partition_by_callable(self, typ: Type,
3524
3528
# do better.
3525
3529
# If it is possible for the false branch to execute, return the original
3526
3530
# type to avoid losing type information.
3527
- callables , uncallables = self .partition_by_callable (typ . erase_to_union_or_bound (),
3531
+ callables , uncallables = self .partition_by_callable (erase_to_union_or_bound (typ ),
3528
3532
unsound_partition )
3529
3533
uncallables = [typ ] if len (uncallables ) else []
3530
3534
return callables , uncallables
@@ -4087,7 +4091,7 @@ def conditional_type_map(expr: Expression,
4087
4091
if it was not the proposed type, if any. None means bot, {} means top"""
4088
4092
if proposed_type_ranges :
4089
4093
proposed_items = [type_range .item for type_range in proposed_type_ranges ]
4090
- proposed_type = UnionType . make_simplified_union (proposed_items )
4094
+ proposed_type = make_simplified_union (proposed_items )
4091
4095
if current_type :
4092
4096
if isinstance (proposed_type , AnyType ):
4093
4097
# We don't really know much about the proposed type, so we shouldn't
@@ -4170,7 +4174,7 @@ def builtin_item_type(tp: Type) -> Optional[Type]:
4170
4174
return tp .args [0 ]
4171
4175
elif isinstance (tp , TupleType ) and all (not isinstance (it , AnyType )
4172
4176
for it in get_proper_types (tp .items )):
4173
- return UnionType . make_simplified_union (tp .items ) # this type is not externally visible
4177
+ return make_simplified_union (tp .items ) # this type is not externally visible
4174
4178
elif isinstance (tp , TypedDictType ):
4175
4179
# TypedDict always has non-optional string keys. Find the key type from the Mapping
4176
4180
# base class.
@@ -4221,7 +4225,7 @@ def or_conditional_maps(m1: TypeMap, m2: TypeMap) -> TypeMap:
4221
4225
for n1 in m1 :
4222
4226
for n2 in m2 :
4223
4227
if literal_hash (n1 ) == literal_hash (n2 ):
4224
- result [n1 ] = UnionType . make_simplified_union ([m1 [n1 ], m2 [n2 ]])
4228
+ result [n1 ] = make_simplified_union ([m1 [n1 ], m2 [n2 ]])
4225
4229
return result
4226
4230
4227
4231
@@ -4435,7 +4439,7 @@ def overload_can_never_match(signature: CallableType, other: CallableType) -> bo
4435
4439
# the below subtype check and (surprisingly?) `is_proper_subtype(Any, Any)`
4436
4440
# returns `True`.
4437
4441
# TODO: find a cleaner solution instead of this ad-hoc erasure.
4438
- exp_signature = expand_type (signature , {tvar .id : tvar . erase_to_union_or_bound ( )
4442
+ exp_signature = expand_type (signature , {tvar .id : erase_def_to_union_or_bound ( tvar )
4439
4443
for tvar in signature .variables })
4440
4444
assert isinstance (exp_signature , CallableType )
4441
4445
return is_callable_compatible (exp_signature , other ,
@@ -4689,7 +4693,7 @@ class Status(Enum):
4689
4693
4690
4694
if isinstance (typ , UnionType ):
4691
4695
items = [try_expanding_enum_to_union (item , target_fullname ) for item in typ .items ]
4692
- return UnionType . make_simplified_union (items )
4696
+ return make_simplified_union (items )
4693
4697
elif isinstance (typ , Instance ) and typ .type .is_enum and typ .type .fullname () == target_fullname :
4694
4698
new_items = []
4695
4699
for name , symbol in typ .type .names .items ():
@@ -4704,7 +4708,7 @@ class Status(Enum):
4704
4708
# only using CPython, but we might as well for the sake of full correctness.
4705
4709
if sys .version_info < (3 , 7 ):
4706
4710
new_items .sort (key = lambda lit : lit .value )
4707
- return UnionType . make_simplified_union (new_items )
4711
+ return make_simplified_union (new_items )
4708
4712
else :
4709
4713
return typ
4710
4714
@@ -4716,7 +4720,7 @@ def coerce_to_literal(typ: Type) -> ProperType:
4716
4720
typ = get_proper_type (typ )
4717
4721
if isinstance (typ , UnionType ):
4718
4722
new_items = [coerce_to_literal (item ) for item in typ .items ]
4719
- return UnionType . make_simplified_union (new_items )
4723
+ return make_simplified_union (new_items )
4720
4724
elif isinstance (typ , Instance ) and typ .last_known_value :
4721
4725
return typ .last_known_value
4722
4726
else :
0 commit comments