@@ -107,7 +107,7 @@ def class_callable(init_type: CallableType, info: TypeInfo, type_type: Instance,
107
107
explicit_type = init_ret_type if is_new else orig_self_type
108
108
if (
109
109
isinstance (explicit_type , (Instance , TupleType ))
110
- # We have to skip protocols, because it can can be a subtype of a return type
110
+ # We have to skip protocols, because it can be a subtype of a return type
111
111
# by accident. Like `Hashable` is a subtype of `object`. See #11799
112
112
and isinstance (default_ret_type , Instance )
113
113
and not default_ret_type .type .is_protocol
@@ -354,10 +354,17 @@ def make_simplified_union(items: Sequence[Type],
354
354
355
355
Note: This must NOT be used during semantic analysis, since TypeInfos may not
356
356
be fully initialized.
357
+
357
358
The keep_erased flag is used for type inference against union types
358
359
containing type variables. If set to True, keep all ErasedType items.
360
+
361
+ The contract_literals flag indicates whether we need to contract literal types
362
+ back into a sum type. Set it to False when called by try_expanding_sum_type_
363
+ to_union().
359
364
"""
360
365
items = get_proper_types (items )
366
+
367
+ # Step 1: expand all nested unions
361
368
while any (isinstance (typ , UnionType ) for typ in items ):
362
369
all_items : List [ProperType ] = []
363
370
for typ in items :
@@ -367,10 +374,11 @@ def make_simplified_union(items: Sequence[Type],
367
374
all_items .append (typ )
368
375
items = all_items
369
376
377
+ # Step 2: remove redundant unions
370
378
simplified_set = _remove_redundant_union_items (items , keep_erased )
371
379
372
- # If more than one literal exists in the union, try to simplify
373
- if ( contract_literals and sum (isinstance (item , LiteralType ) for item in simplified_set ) > 1 ) :
380
+ # Step 3: If more than one literal exists in the union, try to simplify
381
+ if contract_literals and sum (isinstance (item , LiteralType ) for item in simplified_set ) > 1 :
374
382
simplified_set = try_contracting_literals_in_union (simplified_set )
375
383
376
384
return UnionType .make_union (simplified_set , line , column )
@@ -384,7 +392,7 @@ def _remove_redundant_union_items(items: List[ProperType], keep_erased: bool) ->
384
392
385
393
# NB: having a separate fast path for Union of Literal and slow path for other things
386
394
# would arguably be cleaner, however it breaks down when simplifying the Union of two
387
- # different enum types as try_expanding_enum_to_union works recursively and will
395
+ # different enum types as try_expanding_sum_type_to_union works recursively and will
388
396
# trigger intermediate simplifications that would render the fast path useless
389
397
for i , item in enumerate (items ):
390
398
if i in removed :
@@ -408,7 +416,7 @@ def _remove_redundant_union_items(items: List[ProperType], keep_erased: bool) ->
408
416
if safe_skip :
409
417
continue
410
418
411
- # Keep track of the truishness info for deleted subtypes which can be relevant
419
+ # Keep track of the truthiness info for deleted subtypes which can be relevant
412
420
cbt = cbf = False
413
421
for j , tj in enumerate (items ):
414
422
if (
@@ -609,7 +617,7 @@ def try_getting_str_literals(expr: Expression, typ: Type) -> Optional[List[str]]
609
617
Otherwise, returns None.
610
618
611
619
Specifically, this function is guaranteed to return a list with
612
- one or more strings if one one the following is true:
620
+ one or more strings if one of the following is true:
613
621
614
622
1. 'expr' is a StrExpr
615
623
2. 'typ' is a LiteralType containing a string
@@ -651,7 +659,7 @@ def try_getting_literals_from_type(typ: Type,
651
659
target_literal_type : TypingType [T ],
652
660
target_fullname : str ) -> Optional [List [T ]]:
653
661
"""If the given expression or type corresponds to a Literal or
654
- union of Literals where the underlying values corresponds to the given
662
+ union of Literals where the underlying values correspond to the given
655
663
target type, returns a list of those underlying values. Otherwise,
656
664
returns None.
657
665
"""
0 commit comments