GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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

New issue
Copy link
New issue
Copy link
Closed
#21535
Closed
[1.19 regression] copy.replace doesn't work with NamedTuple anymore#21528
#21535
Copy link
Labels
bugmypy got something wrongtopic-named-tupletopic-protocolstopic-self-typesTypes for self

Description

@loic-simon
loic-simon
opened on May 21, 2026
Issue body actions

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

  • 1.18.2 (playground) : success

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

  1. loic-simon
    added
    bugmypy got something wrong
    on May 21, 2026
  2. loic-simon commented on May 21, 2026

    @loic-simon
    loic-simon
    on May 21, 2026
    ContributorAuthor
    More actions

    Bisected to Sync typeshed

    So it looks like a typeshed problem, upsteam commit: Refine the copy._SupportsReplace.__replace__ signature

  3. ilevkivskyi
    added
    topic-protocols
    topic-self-typesTypes for self
    topic-named-tuple
    on May 21, 2026
  4. ilevkivskyi commented on May 21, 2026

    @ilevkivskyi
    ilevkivskyi
    on May 21, 2026
    Member
    More actions

    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)

  5. ilevkivskyi
    added a commit that references this issue on May 22, 2026

    Support protocol checks for self-types in tuple types (#21535)

    21e2859
  6. ilevkivskyi
    closed this as completedin #21535on May 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-named-tupletopic-protocolstopic-self-typesTypes for self

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.