`dataclasses.replace` should be able to return differently-parametrized generic dataclasses
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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):
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the minimal generic dataclass example in the issue and trace how mypy infers dataclasses.replace arguments and return types. Add focused regression coverage showing that changing a type parameter is accepted while genuinely incompatible replacements remain errors; the issue does not name a source file or existing test path.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100