python / python/mypy

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

Offen
#19,694 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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):

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie mit dem minimalen generischen dataclass-Beispiel im Issue und verfolgen Sie, wie mypy die Argumente und Rückgabetypen von dataclasses.replace ableitet. Fügen Sie gezielte Regressionstests hinzu, die zeigen, dass das Ändern eines Typparameters akzeptiert wird, während tatsächlich inkompatible Ersetzungen weiterhin Fehler bleiben; das Issue nennt keine Quelldatei oder keinen vorhandenen Testpfad.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
tooling
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.