microsoft / microsoft/pyright

False postive "list[Self@Baz]" is not assignable to "list[Baz]" with `ClassVar` assignment

Open
#11,445 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

Code sample in [pyright playground](https://pyright-play.net/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoDCANgIYDOZAaiSADRQDKApkcAFBsDGpFUAQiQBeALjZRxhADIMA%2BpQCCAJWGEeVGgG0iSMjA3NWAXUMcBggHQFpcpVAC8UDYfEBiKAFEQ4EFAD8AQjYzS2sFRXMSBAQmFAATAAozeIBKZNcoAHkAaQ5ucjIoADEwMFEJKVkwlWJ86hAtHT0DYGN7R2coN2yOYrAQyqUIqJiE3pS0zsycrjV%2BGjKJKwHlVVrNbV19FhaTIJp%2Bm0U2jQEQFI6unKA)

```python
from typing import ClassVar, Self

class Baz:
CLS_VAR: ClassVar[list[Self]]

Baz.CLS_VAR = [] # Error ?!
Baz.CLS_VAR.append(Baz()) # OK

class Foo:
CLS_VAR: ClassVar[list[Self]] = [] # OK

Foo.CLS_VAR.append(Foo()) # OK

class Bar:
CLS_VAR: ClassVar[list[Self]]

Bar.CLS_VAR = [Bar()] # OK
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the diagnostic in the linked Pyright playground using the Baz, Foo, and Bar examples, then trace the ClassVar and Self assignment analysis that distinguishes them. Done means the Baz assignment no longer reports an incompatibility while the valid examples remain accepted, with regression coverage for the demonstrated cases.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.