python / python/mypy

`dataclasses.replace` should be able to return differently-parametrized generic dataclasses

Aperta
#19,694 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Feature

Consider an instance of a generic dataclass on which we call dataclasses.replace in such a way that the replacement would change the type argument (because it changes the types of fields that involve the type parameter):

from dataclasses import dataclass, replace

@dataclass(frozen=True)
class A[T]:
  a: T

a_int: A[int] = A(a=1)
a_str: A[str] = replace(a_int, a="1")

Mypy currently reports errors for the last line:

example.py:9: error: Incompatible types in assignment (expression has type "A[int]", variable has type "A[str]")  [assignment]
example.py:9: error: Argument "a" to "replace" of "A[int]" has incompatible type "str"; expected "int"  [arg-type]
Found 2 errors in 1 file (checked 1 source file)

To me, it seems like this should be allowed and Mypy should understand that the replace call will return an instance of A[str], only reporting errors when the replacements have types that are completely incompatible with the class's field definitions.

Pitch

Users can currently work around the lack of this feature by defining their own replace method with overloads, e.g.:

@dataclass(frozen=True)
class A[T]:
  a: T

  @overload
  def replace[U](self, *, a: U) -> A[U]: ...

  @overload
  def replace(self) -> A[T]: ...

  # None as default for simplicity, in reality a unique sentinel may be better
  def replace(self, *, a: U | None = None) -> A[T] | A[U]:
    # With e.g. Pyright, we can just write:
    # return A(a=a) if a is not None else A(a=self.a)
    # But Mypy as of 1.17.1 doesn't like that
    # (see https://github.com/python/mypy/issues/19534),
    # so we have to be a bit more verbose:
    x: A[T] | A[U]
    if a is not None:
      x = A(a=a)
    else:
      x = A(a=self.a)
    return x

But this gets old fast, especially when there are N > 1 type parameters and you have to write out overloads for all 2N possible replace calls.

To me personally, it's not that urgent and I can live with the workaround for now (thankfully, in my specific situation I only have 2 type parameters at most). Still, it would be nice if this "just worked".

Additional context

The same issue would apply to the new (Python 3.13+) copy.replace and object.__replace__ methods, but these don't even have replacement argument type checking like dataclass.replace does yet, so I guess it's way too early for that.

Other languages

In Rust, the equivalent for its copy-replacing-only-some-fields idiom is available in nightly builds (but not stable yet):

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 con l’esempio minimo di dataclass generica nell’issue e segui come mypy inferisce gli argomenti e i tipi restituiti di dataclasses.replace. Aggiungi una copertura di regressione mirata che mostri che la modifica di un parametro di tipo è accettata, mentre le sostituzioni realmente incompatibili continuano a produrre errori; l’issue non indica alcun file sorgente né alcun percorso di test esistente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.