Skip to content

Have namedtuple __replace__ return Self #17475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mypy/semanal_namedtuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
TYPED_NAMEDTUPLE_NAMES,
AnyType,
CallableType,
Instance,
LiteralType,
TupleType,
Type,
Expand Down Expand Up @@ -631,9 +632,10 @@ def add_method(
args=[Argument(var, var.type, EllipsisExpr(), ARG_NAMED_OPT) for var in vars],
)
if self.options.python_version >= (3, 13):
type_vars = [tv for tv in info.defn.type_vars]
add_method(
"__replace__",
ret=None,
ret=Instance(info, type_vars),
args=[Argument(var, var.type, EllipsisExpr(), ARG_NAMED_OPT) for var in vars],
)

Expand Down
16 changes: 15 additions & 1 deletion test-data/unit/check-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -1407,9 +1407,23 @@ from typing import NamedTuple
class A(NamedTuple):
x: int

A(x=0).__replace__(x=1)
replaced = A(x=0).__replace__(x=1)
reveal_type(replaced) # N: Revealed type is "__main__.A"

A(x=0).__replace__(x="asdf") # E: Argument "x" to "__replace__" of "A" has incompatible type "str"; expected "int"
A(x=0).__replace__(y=1) # E: Unexpected keyword argument "y" for "__replace__" of "A"

from typing import TypeVar, Generic

T = TypeVar("T")

class GenericA(NamedTuple, Generic[T]):
x: T

replaced_2 = GenericA(x=0).__replace__(x=1)
reveal_type(replaced_2) # N: Revealed type is "__main__.GenericA"
GenericA(x=0).__replace__(x="abc") # E: Argument "x" to "__replace__" of "GenericA" has incompatible type "str"; expected "int"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's specialized, but the generic isn't in the revealed type which is odd.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's how mypy generally works:

% mypy -c 'x: list[str] = []; x.append(1)'
<string>:1: error: Argument 1 to "append" of "list" has incompatible type "int"; expected "str"  [arg-type]

Agree this is potentially confusing but it's not something that should be changed in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, makes sense - let me bring this out of draft in that case.


[builtins fixtures/tuple.pyi]
[typing fixtures/typing-namedtuple.pyi]

Expand Down
Loading