Skip to content

Commit d440490

Browse files
authored
Deduplicate iterable logic (#16006)
This e.g. makes sure both code paths receive my fix in #15688
1 parent 2a6d9cb commit d440490

File tree

1 file changed

+12
-23
lines changed

1 file changed

+12
-23
lines changed

mypy/checker.py

+12-23
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
from mypy.errorcodes import TYPE_VAR, UNUSED_AWAITABLE, UNUSED_COROUTINE, ErrorCode
4242
from mypy.errors import Errors, ErrorWatcher, report_internal_error
4343
from mypy.expandtype import expand_self_type, expand_type, expand_type_by_instance
44-
from mypy.join import join_types
4544
from mypy.literals import Key, extract_var_from_literal_hash, literal, literal_hash
4645
from mypy.maptype import map_instance_to_supertype
4746
from mypy.meet import is_overlapping_erased_types, is_overlapping_types
@@ -4653,42 +4652,32 @@ def analyze_async_iterable_item_type(self, expr: Expression) -> tuple[Type, Type
46534652

46544653
def analyze_iterable_item_type(self, expr: Expression) -> tuple[Type, Type]:
46554654
"""Analyse iterable expression and return iterator and iterator item types."""
4656-
echk = self.expr_checker
4657-
iterable = get_proper_type(echk.accept(expr))
4658-
iterator = echk.check_method_call_by_name("__iter__", iterable, [], [], expr)[0]
4659-
4655+
iterator, iterable = self.analyze_iterable_item_type_without_expression(
4656+
self.expr_checker.accept(expr), context=expr
4657+
)
46604658
int_type = self.analyze_range_native_int_type(expr)
46614659
if int_type:
46624660
return iterator, int_type
4663-
4664-
if (
4665-
isinstance(iterable, TupleType)
4666-
and iterable.partial_fallback.type.fullname == "builtins.tuple"
4667-
):
4668-
return iterator, tuple_fallback(iterable).args[0]
4669-
else:
4670-
# Non-tuple iterable.
4671-
return iterator, echk.check_method_call_by_name("__next__", iterator, [], [], expr)[0]
4661+
return iterator, iterable
46724662

46734663
def analyze_iterable_item_type_without_expression(
46744664
self, type: Type, context: Context
46754665
) -> tuple[Type, Type]:
46764666
"""Analyse iterable type and return iterator and iterator item types."""
46774667
echk = self.expr_checker
4668+
iterable: Type
46784669
iterable = get_proper_type(type)
46794670
iterator = echk.check_method_call_by_name("__iter__", iterable, [], [], context)[0]
46804671

4681-
if isinstance(iterable, TupleType):
4682-
joined: Type = UninhabitedType()
4683-
for item in iterable.items:
4684-
joined = join_types(joined, item)
4685-
return iterator, joined
4672+
if (
4673+
isinstance(iterable, TupleType)
4674+
and iterable.partial_fallback.type.fullname == "builtins.tuple"
4675+
):
4676+
return iterator, tuple_fallback(iterable).args[0]
46864677
else:
46874678
# Non-tuple iterable.
4688-
return (
4689-
iterator,
4690-
echk.check_method_call_by_name("__next__", iterator, [], [], context)[0],
4691-
)
4679+
iterable = echk.check_method_call_by_name("__next__", iterator, [], [], context)[0]
4680+
return iterator, iterable
46924681

46934682
def analyze_range_native_int_type(self, expr: Expression) -> Type | None:
46944683
"""Try to infer native int item type from arguments to range(...).

0 commit comments

Comments
 (0)