MyPy infers descriptors are always ClassVars when testing assignability to protocols
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 38/100
Rechercherichtung
Beginne mit dem eigenständigen Descriptor und dem Protocol-Beispiel in err_report.py und reproduziere die Ergebnisse mit mypy 1.17.1 ohne Flags. Verfolge anschließend das Verhalten der Protocol-Zuweisbarkeit für Descriptor-Attribute und überprüfe dann, dass der erwartete Fall mit einer Instanzvariablen erfolgreich ist, während der ClassVar-Fall wie beschrieben abgelehnt wird.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Bug Report
When defining a Protocol for objects containing descriptors, mypy appears to assume descriptors are always ClassVars. Annotating the protocol's descriptor attributes with ClassVar silences mypy, but raises errors in other type checkers (e.g. pyright). Given descriptors are allowed to be used as both instance and class variables (and __get__ / __set__ provide obj: object | None semantics to support this), I think this is a mypy bug.
A real-world example of this is defining a protocol for an SQLAlchemy model, e.g. a protocol might define id: orm.Mapped[int] to match models with an id integer attribute.
To Reproduce
Given the complexity of SQLAlchemy, here's an entirely self-contained example:
from typing import ClassVar, Protocol
class MyDescriptor:
def __init__(self):
self.value = "test"
def __get__(self, obj: object | None, objtype: type[object]) -> str:
return self.value
def __set__(self, obj: object | None, value: str) -> None:
self.value = value
class Foo:
bar = MyDescriptor()
class HasBar(Protocol):
bar: MyDescriptor
class HasBarWithClassVar(Protocol):
bar: ClassVar[MyDescriptor]
instance1: HasBar = Foo() # fails in mypy, passes in pyright
model1: type[HasBar] = Foo # fails in mypy, passes in pyright
instance2: HasBarWithClassVar = Foo() # fails in pyright, passes in mypy
model2: type[HasBarWithClassVar] = Foo # fails in pyright, passes in mypy
Expected Behavior
Assigning Foo to HasBar should type check correctly, and assigning to HasBarWithClassVar should fail.
Actual Behavior
Assigning Foo to HasBar errors in mypy, but checks in pyright. Conversely, assigning to HasBarWithClassVar checks in mypy, but fails in pyright. They seem to fundamentally disagree.
$ mypy err_report.py
err_report.py:27: error: Incompatible types in assignment (expression has type "Foo", variable has type "HasBar") [assignment]
err_report.py:27: note: Following member(s) of "Foo" have conflicts:
err_report.py:27: note: bar: expected "MyDescriptor", got "str"
err_report.py:28: error: Incompatible types in assignment (expression has type "type[Foo]", variable has type "type[HasBar]") [assignment]
Found 2 errors in 1 file (checked 1 source file)
$ pyright err_report.py
/path/to/my/err_report.py
/path/to/my/err_report.py:30:33 - error: Type "Foo" is not assignable to declared type "HasBarWithClassVar"
"Foo" is incompatible with protocol "HasBarWithClassVar"
"bar" is defined as a ClassVar in protocol (reportAssignmentType)
/path/to/my/err_report.py:31:36 - error: Type "type[Foo]" is not assignable to declared type "type[HasBarWithClassVar]"
"Foo" is incompatible with protocol "HasBarWithClassVar"
Type "type[Foo]" is not assignable to type "type[HasBarWithClassVar]"
"bar" is defined as a ClassVar in protocol (reportAssignmentType)
2 errors, 0 warnings, 0 informations
If I were to annotate Foo.bar with ClassVar, then pyright accepts assigning Foo to HasBarWithClassVar (which is what I would expect), but now mypy won't:
class Foo:
bar: ClassVar = MyDescriptor()
$ mypy err_report.py
err_report.py:27: error: Incompatible types in assignment (expression has type "Foo", variable has type "HasBar") [assignment]
err_report.py:27: note: Protocol member HasBar.bar expected instance variable, got class variable
err_report.py:28: error: Incompatible types in assignment (expression has type "type[Foo]", variable has type "type[HasBar]") [assignment]
err_report.py:30: error: Incompatible types in assignment (expression has type "Foo", variable has type "HasBarWithClassVar") [assignment]
err_report.py:30: note: Protocol member HasBarWithClassVar.bar expected instance variable, got class variable
err_report.py:31: error: Incompatible types in assignment (expression has type "type[Foo]", variable has type "type[HasBarWithClassVar]") [assignment]
Found 4 errors in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.17.1
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): n/a - Python version used: 3.13.5
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- Ø Merge
- 1 T. 18 Std.
- Gemergte PRs (30 T.)
- 54
Beitragsleitfaden
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus python/mypy
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
-
documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
-
bug topic-configuration topic-error-reporting
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
bancolombia/sentinel#23 ·
-
test md OffenCI
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100