Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
address review
  • Loading branch information
randolf-scholz committed Aug 16, 2025
commit e169d3e33ac75a463c257aaf32d00ac0e0ee0cc8
28 changes: 15 additions & 13 deletions mypy/argmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from collections.abc import Sequence
from typing import TYPE_CHECKING, Callable, cast
from typing_extensions import NewType, TypeGuard
from typing_extensions import NewType, TypeGuard, TypeIs

from mypy import nodes
from mypy.maptype import map_instance_to_supertype
Expand Down Expand Up @@ -278,11 +278,12 @@ def expand_actual_type(
return original_actual

def is_iterable(self, typ: Type) -> bool:
"""Check if the type is an iterable, i.e. implements the Iterable Protocol."""
from mypy.subtypes import is_subtype

return is_subtype(typ, self.context.iterable_type)

def is_iterable_instance_type(self, typ: Type) -> TypeGuard[IterableType]:
def is_iterable_instance_type(self, typ: Type) -> TypeIs[IterableType]:
"""Check if the type is an Iterable[T]."""
p_t = get_proper_type(typ)
return isinstance(p_t, Instance) and p_t.type == self.context.iterable_type.type
Expand All @@ -300,8 +301,6 @@ def _solve_as_iterable(self, typ: Type) -> IterableType | AnyType:
from mypy.nodes import ARG_POS
from mypy.solve import solve_constraints

iterable_kind = self.context.iterable_type.type

# We first create an upcast function:
# def [T] (Iterable[T]) -> Iterable[T]: ...
# and then solve for T, given the input type as the argument.
Expand All @@ -310,21 +309,20 @@ def _solve_as_iterable(self, typ: Type) -> IterableType | AnyType:
"T",
TypeVarId(-1),
values=[],
upper_bound=AnyType(TypeOfAny.special_form),
default=AnyType(TypeOfAny.special_form),
upper_bound=AnyType(TypeOfAny.from_omitted_generics),
default=AnyType(TypeOfAny.from_omitted_generics),
)
target = Instance(iterable_kind, [T])

target = self._make_iterable_instance_type(T)
upcast_callable = CallableType(
variables=[T],
arg_types=[target],
arg_kinds=[ARG_POS],
arg_names=[None],
ret_type=T,
ret_type=target,
fallback=self.context.function_type,
)
constraints = infer_constraints_for_callable(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can mypy.infer.infer_function_type_arguments replace this and following line at once?

upcast_callable, [typ], [ARG_POS], [None], [[0]], context=self.context
upcast_callable, [typ], [ARG_POS], [None], [[0]], self.context
)

(sol,), _ = solve_constraints([T], constraints)
Expand All @@ -334,7 +332,11 @@ def _solve_as_iterable(self, typ: Type) -> IterableType | AnyType:
return self._make_iterable_instance_type(sol)

def as_iterable_type(self, typ: Type) -> IterableType | AnyType:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we reuse logic from analyze_iterable_item_type_without_expression or similar?

"""Reinterpret a type as Iterable[T], or return AnyType if not possible."""
"""Reinterpret a type as Iterable[T], or return AnyType if not possible.

This function specially handles certain types like UnionType, TupleType, and UnpackType.
Otherwise, the upcasting is performed using the solver.
"""
p_t = get_proper_type(typ)
if self.is_iterable_instance_type(p_t) or isinstance(p_t, AnyType):
return p_t
Expand Down Expand Up @@ -386,8 +388,8 @@ def parse_star_args_type(
) -> TupleType | IterableType | ParamSpecType | AnyType:
"""Parse the type of a ``*args`` argument.

Returns one of TupleType, IterableType, ParamSpecType,
or AnyType(TypeOfAny.from_error) if the type cannot be parsed or is invalid.
Returns one of TupleType, IterableType, ParamSpecType or AnyType.
Returns AnyType(TypeOfAny.from_error) if the type cannot be parsed or is invalid.
"""
p_t = get_proper_type(typ)
if isinstance(p_t, (TupleType, ParamSpecType, AnyType)):
Expand Down
Loading