python / python/mypy

Dataclass doesn't match a protocol if a field is Callable (expected settable variable, got read-only attribute)

Aperta
#18,179 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

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.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
55/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.