Incorrect type of the returned value of `map()` when applying to a class property
Note: if you are reporting a wrong signature of a function or a class in the standard library, then the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues.
If you have a question about typing or a behavior that you’re seeing in Pyright (as opposed to a bug report or enhancement request), consider posting to the Pyright discussion forum.
Describe the bug A clear and concise description of the behavior you are seeing and the expected behavior along with steps to reproduce it.
Pyright reports an incorrect error when validating the type of the returned iterator from map(). The error is found only when the iteratable being mapped is a class property.
Code or Screenshots If possible, provide a minimal, self-contained code sample (surrounded by triple back ticks) to demonstrate the issue. The code should define or import all referenced symbols.
from typing import Iterator, Self
class MetaClass(type):
meta_attr = list(range(10))
@property
def meta_prop(cls) -> list[int]:
return list(range(10))
class MyClass(metaclass=MetaClass):
val: int
class_attr = list(range(10))
def __init__(self, val: int):
self.val = val
@classmethod
@property
def class_prop(cls) -> list[int]:
return list(range(10))
@property
def inst_prop(self) -> list[int]:
return list(range(10))
@classmethod
def from_class_prop(cls) -> Iterator[Self]:
prop = cls.meta_prop # error in map(cls, prop): "Self@MyClass | _S@map" is not a subtype of "Self@MyClass"
# prop = cls.class_prop # same error
# prop = cls.meta_attr # no error
# prop = cls.class_attr # no error
# prop: list[int] = cls.meta_prop # no error
return map(cls, prop)
def from_inst_prop(self) -> Iterator[Self]:
prop = self.inst_prop # no error
return map(type(self), prop)Error message:
Type "map[Self@MyClass]*" is not assignable to return type "Iterator[Self@MyClass]"
"map[Self@MyClass]*" is incompatible with protocol "Iterator[Self@MyClass]"
Type parameter "_T_co@Iterator" is covariant, but "Self@MyClass | _S@map" is not a subtype of "Self@MyClass"
Type "Self@MyClass | _S@map" is not assignable to type "Self@MyClass" (reportReturnType)No error is reported when using mypy.
If your code relies on symbols that are imported from a third-party library, include the associated import statements and specify which versions of those libraries you have installed.
VS Code extension or command-line Are you running pyright as a VS Code extension, a language server in another editor, integrated into Pylance, or the command-line tool? Which version?
Using Pylance v2026.2.1 in VS Code. But the bug is also reproducible on Pyright Playground with Pyright version Latest (1.1.410).
Source: microsoft/pyright