-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Have namedtuple __replace__
return Self
#17475
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.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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 returnSelf
to ensure protocol compatibility withcopy.replace
in the typeshed, and to be accurate to the actual dunder.