Skip to content

Commit c56046c

Browse files
Fix typos (#12635)
Improve docs and fix typos
1 parent 50213b5 commit c56046c

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

mypy/typeops.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def class_callable(init_type: CallableType, info: TypeInfo, type_type: Instance,
107107
explicit_type = init_ret_type if is_new else orig_self_type
108108
if (
109109
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
111111
# by accident. Like `Hashable` is a subtype of `object`. See #11799
112112
and isinstance(default_ret_type, Instance)
113113
and not default_ret_type.type.is_protocol
@@ -354,10 +354,17 @@ def make_simplified_union(items: Sequence[Type],
354354
355355
Note: This must NOT be used during semantic analysis, since TypeInfos may not
356356
be fully initialized.
357+
357358
The keep_erased flag is used for type inference against union types
358359
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().
359364
"""
360365
items = get_proper_types(items)
366+
367+
# Step 1: expand all nested unions
361368
while any(isinstance(typ, UnionType) for typ in items):
362369
all_items: List[ProperType] = []
363370
for typ in items:
@@ -367,10 +374,11 @@ def make_simplified_union(items: Sequence[Type],
367374
all_items.append(typ)
368375
items = all_items
369376

377+
# Step 2: remove redundant unions
370378
simplified_set = _remove_redundant_union_items(items, keep_erased)
371379

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:
374382
simplified_set = try_contracting_literals_in_union(simplified_set)
375383

376384
return UnionType.make_union(simplified_set, line, column)
@@ -384,7 +392,7 @@ def _remove_redundant_union_items(items: List[ProperType], keep_erased: bool) ->
384392

385393
# NB: having a separate fast path for Union of Literal and slow path for other things
386394
# 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
388396
# trigger intermediate simplifications that would render the fast path useless
389397
for i, item in enumerate(items):
390398
if i in removed:
@@ -408,7 +416,7 @@ def _remove_redundant_union_items(items: List[ProperType], keep_erased: bool) ->
408416
if safe_skip:
409417
continue
410418

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
412420
cbt = cbf = False
413421
for j, tj in enumerate(items):
414422
if (
@@ -609,7 +617,7 @@ def try_getting_str_literals(expr: Expression, typ: Type) -> Optional[List[str]]
609617
Otherwise, returns None.
610618
611619
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:
613621
614622
1. 'expr' is a StrExpr
615623
2. 'typ' is a LiteralType containing a string
@@ -651,7 +659,7 @@ def try_getting_literals_from_type(typ: Type,
651659
target_literal_type: TypingType[T],
652660
target_fullname: str) -> Optional[List[T]]:
653661
"""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
655663
target type, returns a list of those underlying values. Otherwise,
656664
returns None.
657665
"""

0 commit comments

Comments
 (0)