Skip to content

[1.19 regression] copy.replace doesn't work with NamedTuple anymore #21528

@loic-simon

Description

@loic-simon

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

Metadata

Metadata

Assignees

No one assigned
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions