microsoft / microsoft/pyright

Incorrect type of the returned value of `map()` when applying to a class property

Open
#11,519 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

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](https://github.com/microsoft/pyright/discussions).

**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.

```python
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](https://pyright-play.net/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoCSMApiAIYy4A0UAysQDbACwAUGwMYNkDOPUAWWIwyAYW58AFPATEAlAC42UFVAjCyAfQowQUALxQGSHjEnl0xSQEYADHLltlqgAIJwsvHGcqAJsWA1DU13MARJLh45KABaAD4jExgAbVQYAF0lVlUcqBBhAFcQFETTczJLG3tHdlYuXn4BOHEGyXURer59IREWvkUfKAA3MgYFTBQYQc6ebRhdA1KzCzQrOwcnbNV-QM1NVCQYPckeRmAaEbGJmAGt3KhTpgA6S8XLzZyXGfaACzBfQZuDykeCDHZQGYhDwRBhRWIJYymVKTTKDHL5GBFEqI5YVVZVDa1T6hTygu5QcGoUxQsInM7ReJLZEZLL3FQYrFLcqVdY1QHfYR-AHk8GgSCaSEkmFwxlEUgUXDJehMVHknIkxaRJ7tLQaqAAYigpHAelQajI4UiNBJiigACJlcAXE0%2BvwAD5QTS0FwQC12zD8FBgfBkB4FABGMmIUDAgQdZ2dzQkPDtaNUho1hi1ko8KkNPDI6iNIBNabzUEzENh2uCOj0BqgQeLpbV6Yruaz1chdfLTeNuDLDZJ4xxzPSmurOppCF7YGbA9b7MKxXNlth1o8fJFASgYog%2BxQ1Klj2ADIScvIlBASrOqrZ7bCixPTypRz1hr7JYX945K994SjOkmDkDcwjkIA) with Pyright version `Latest (1.1.410)`.

Contributor guide

Open the contributing guide

Research direction

Start with the self-contained Python example in the issue and reproduce it in the Pyright Playground using Pyright 1.1.410. Compare inference for map(cls, prop) when prop is a class property with the working class-attribute and instance-property cases. Done means the returned map is accepted as Iterator[Self] without weakening the other cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.