MCPcopy Create free account
hub / github.com/python/mypy / _fix_iter

Method _fix_iter

mypy/stubgenc.py:675–697  ·  view source on GitHub ↗

Ensure that objects which implement old-style iteration via __getitem__ are considered iterable.

(
        self, ctx: FunctionContext, inferred: list[FunctionSig], output: list[str]
    )

Source from the content-addressed store, hash-verified

673 return "".join(lines)
674
675 def _fix_iter(
676 self, ctx: FunctionContext, inferred: list[FunctionSig], output: list[str]
677 ) -> None:
678 """Ensure that objects which implement old-style iteration via __getitem__
679 are considered iterable.
680 """
681 if (
682 ctx.class_info
683 and ctx.class_info.cls is not None
684 and ctx.name == "__getitem__"
685 and "__iter__" not in ctx.class_info.cls.__dict__
686 ):
687 item_type: str | None = None
688 for sig in inferred:
689 if sig.args and sig.args[-1].type == "int":
690 item_type = sig.ret_type
691 break
692 if item_type is None:
693 return
694 obj = CFunctionStub(
695 "__iter__", f"def __iter__(self) -> typing.Iterator[{item_type}]\n"
696 )
697 self.generate_function_stub("__iter__", obj, output=output, class_info=ctx.class_info)
698
699 def generate_property_stub(
700 self,

Callers 1

Calls 2

CFunctionStubClass · 0.85

Tested by

no test coverage detected