GeezerVison is showing github.com — [1.19 regression] copy.replace doesn't work with NamedTuple anymore · Issue #21528 · python/mypy
[1.19 regression] copy.replace doesn't work with NamedTuple anymore #21528
Description
Bug Report
Using copy.replace on a NamedTuple produces an object that is not considered assignable to the NamedTuple type by mypy 1.19.0 and newer.
To Reproduce
from copy import replace
from typing import NamedTuple
class NT(NamedTuple):
foo: int
def mre(nt: NT) -> NT:
return replace(nt, foo=3)
Expected Behavior
Actual Behavior
- 1.19.0 (playground) :
main.py:10: error: Incompatible return value type (got "NT", expected "NT") [return-value]
Some reveal_types (playground):
def mre(nt: NT) -> NT:
reveal_type(nt)
replaced = replace(nt, foo=3)
reveal_type(replaced)
return replaced
give
main.py:10: note: Revealed type is "tuple[builtins.int, fallback=__main__.NT]"
main.py:12: note: Revealed type is "__main__.NT"
main.py:13: error: Incompatible return value type (got "NT", expected "NT") [return-value]
Your Environment
- Reproduced from 1.19.0 to master. I'll try to bisect it to the exact commit.
- Python version used: 3.14
Activity
loic-simon commented on May 21, 2026
ContributorAuthor
More actions
ilevkivskyi commented on May 21, 2026
A self-contained test to reproduce this
[case testTupleTypeSelfTypeProto]
from typing import Protocol, TypeVar
R = TypeVar("R", covariant=True)
class P(Protocol[R]):
def rep(self) -> R: ...
T = TypeVar("T")
def rep(x: P[T]) -> T: ...
class C(tuple[int, str]):
def rep(self: T) -> T: ...
t: C
reveal_type(t) # N: Revealed type is "tuple[builtins.int, builtins.str, fallback=__main__.C]"
reveal_type(rep(t)) # N: Revealed type is "tuple[builtins.int, builtins.str, fallback=__main__.C]"
[builtins fixtures/tuple.pyi]
(second reveal fails)
Metadata
Metadata
Development
No branches or pull requestsIssue actions