Dataclass doesn't match a protocol if a field is Callable (expected settable variable, got read-only attribute)
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 55/100
Direzione di ricerca
Inizia in subtypes.py, in get_member_flags(), e confronta la gestione di v.is_property con lo stato v.is_settable_property impostato da plugins/dataclasses.py:_propertize_callables(). Usa la riproduzione dell’issue con mypy 1.13.0 per verificare il comportamento, quindi conferma che la dataclass con un campo Callable corrisponde al protocollo senza l’errore di assegnazione segnalato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Bug Report
A dataclass containing a Callable field doesn't match the protocol. On the other hand if the field is defined using a callback protocol (another protocol with __call__) or the class is not a dataclass, no error is reported.
test.py:26: error: Incompatible types in assignment (expression has type "FooDC", variable has type "FooProto") [assignment]
test.py:26: note: Protocol member FooProto.f expected settable variable, got read-only attribute
The mypy's subtypes.py:get_member_flags() function contains:
if v.is_property:
return {IS_VAR}
which doesn't take the v.is_settable_property into account while it is set for Callables in plugins/dataclasses.py:_propertize_callables().
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=5c77ed1a30b03cba5cf0ba8e710a4214
import typing
import collections.abc
import dataclasses
CAC = collections.abc.Callable[..., None]
class FooProto(typing.Protocol):
f: CAC
class Foo:
f: CAC
def __init__(self, f: CAC):
self.f = f
@dataclasses.dataclass(frozen=False)
class FooDC:
f: CAC
foo: FooProto = Foo(lambda: None)
# only foo_dc fails:
# test.py:23: error: Incompatible types in assignment (expression has type "FooDC", variable has type "FooProto") [assignment]
# test.py:23: note: Protocol member FooProto.f expected settable variable, got read-only attribute
foo_dc: FooProto = FooDC(lambda: None)
class CallbackProto(typing.Protocol):
def __call__(self, *args, **kwargs) -> None: ...
class FooProtoCallback(typing.Protocol):
f: CallbackProto
class FooCallback:
f: CallbackProto
def __init__(self, f: CAC):
self.f = f
@dataclasses.dataclass(frozen=False)
class FooDCCallback:
f: CallbackProto
foo_callback: FooProtoCallback = FooCallback(lambda: None)
foo_dc_callback: FooProtoCallback = FooDCCallback(lambda: None)
Expected Behavior
I expect a dataclass with a Callable to match the protocol.
Actual Behavior
The dataclass doesn't match the protocol.
Your Environment
- Mypy version used: 1.13.0
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.11.2
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Merge medio
- 1g 18h
- PR unite (30g)
- 54
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di python/mypy
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
-
documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
bug topic-configuration topic-error-reporting
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
Issue simili
-
area/auth bug comp/agent P3 platform/discord type/security
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
NousResearch/hermes-agent#117848 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
bancolombia/sentinel#23 ·
-
test md ApertaCI
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100