Have namedtuple __replace__ return Self#17475
Conversation
__replace__ return self__replace__ return self
__replace__ return self__replace__ return Self
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
|
|
||
| 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" |
There was a problem hiding this comment.
It's specialized, but the generic isn't in the revealed type which is odd.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Ah I see, makes sense - let me bring this out of draft in that case.
|
I am sorry but I am going to revert this. Even if it does fix something, this is a very wrong way to do fix it. |
This reverts commit 1882ed7.
Apologies there. How would you recommend going about fixing this in that case? |
For starter, it would be great to open an issue with a (detailed) explanation of what is wrong. |
Sure thing - in short summary it replaces to how Issue: #17497 |
Have
__replace__for named tuples returnSelfto ensure protocol compatibility withcopy.replacein the typeshed, and to be accurate to the actual dunder.