-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Patch Self for fallback-methods on NamedTuples and TypedDicts
#20328
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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Contributor
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
Contributor
|
Contributor
|
fbfc61f to
093c576
Compare
Self on fallback typesSelf on fallback types
093c576 to
92985c6
Compare
Self on fallback typesSelf for fallback-methods on NamedTuples and TypedDicts
dcreager
reviewed
Sep 12, 2025
e49a2f9 to
9649de7
Compare
982a68e to
7fd5d53
Compare
Contributor
Author
|
I will merge this without approval, because I want to have an easier way of rebasing #18007 on this. Happy to address any post-merge comments. |
Member
👍 Looks good! |
sharkdp
added a commit
that referenced
this pull request
Sep 15, 2025
## Summary This mainly removes an internal inconsistency, where we didn't remove the `Self` type variable when eagerly binding `Self` to an instance type. It has no observable effect, apparently. builds on top of #20328 ## Test Plan None
sharkdp
added a commit
that referenced
this pull request
Sep 22, 2025
## Summary Part of astral-sh/ty#159 This PR only adjusts the signature of a method so if it has a `self` argument then that argument will have type of `Typing.Self` even if it's not specified. If user provides an explicit annotation then Ty will not override that annotation. ## Follow Ups - astral-sh/ty#1131 - astral-sh/ty#1157 - astral-sh/ty#1156 - astral-sh/ty#1173 - #20328 - astral-sh/ty#1163 - astral-sh/ty#1196 ## Test Plan Added mdtests. Also some tests need #18473 to work completely. So I added a todo for those new cases that I added. --------- Co-authored-by: David Peter <mail@david-peter.de>
sharkdp
added a commit
that referenced
this pull request
Sep 23, 2025
Part of astral-sh/ty#159 This PR only adjusts the signature of a method so if it has a `self` argument then that argument will have type of `Typing.Self` even if it's not specified. If user provides an explicit annotation then Ty will not override that annotation. - astral-sh/ty#1131 - astral-sh/ty#1157 - astral-sh/ty#1156 - astral-sh/ty#1173 - #20328 - astral-sh/ty#1163 - astral-sh/ty#1196 Added mdtests. Also some tests need #18473 to work completely. So I added a todo for those new cases that I added. --------- Co-authored-by: David Peter <mail@david-peter.de>
sharkdp
added a commit
that referenced
this pull request
Sep 24, 2025
Part of astral-sh/ty#159 This PR only adjusts the signature of a method so if it has a `self` argument then that argument will have type of `Typing.Self` even if it's not specified. If user provides an explicit annotation then Ty will not override that annotation. - astral-sh/ty#1131 - astral-sh/ty#1157 - astral-sh/ty#1156 - astral-sh/ty#1173 - #20328 - astral-sh/ty#1163 - astral-sh/ty#1196 Added mdtests. Also some tests need #18473 to work completely. So I added a todo for those new cases that I added. --------- Co-authored-by: David Peter <mail@david-peter.de>
sharkdp
added a commit
that referenced
this pull request
Sep 25, 2025
Part of astral-sh/ty#159 This PR only adjusts the signature of a method so if it has a `self` argument then that argument will have type of `Typing.Self` even if it's not specified. If user provides an explicit annotation then Ty will not override that annotation. - astral-sh/ty#1131 - astral-sh/ty#1157 - astral-sh/ty#1156 - astral-sh/ty#1173 - #20328 - astral-sh/ty#1163 - astral-sh/ty#1196 Added mdtests. Also some tests need #18473 to work completely. So I added a todo for those new cases that I added. --------- Co-authored-by: David Peter <mail@david-peter.de>
sharkdp
added a commit
that referenced
this pull request
Sep 25, 2025
Part of astral-sh/ty#159 This PR only adjusts the signature of a method so if it has a `self` argument then that argument will have type of `Typing.Self` even if it's not specified. If user provides an explicit annotation then Ty will not override that annotation. - astral-sh/ty#1131 - astral-sh/ty#1157 - astral-sh/ty#1156 - astral-sh/ty#1173 - #20328 - astral-sh/ty#1163 - astral-sh/ty#1196 Added mdtests. Also some tests need #18473 to work completely. So I added a todo for those new cases that I added. --------- Co-authored-by: David Peter <mail@david-peter.de>
sharkdp
added a commit
that referenced
this pull request
Sep 29, 2025
Part of astral-sh/ty#159 This PR only adjusts the signature of a method so if it has a `self` argument then that argument will have type of `Typing.Self` even if it's not specified. If user provides an explicit annotation then Ty will not override that annotation. - astral-sh/ty#1131 - astral-sh/ty#1157 - astral-sh/ty#1156 - astral-sh/ty#1173 - #20328 - astral-sh/ty#1163 - astral-sh/ty#1196 Added mdtests. Also some tests need #18473 to work completely. So I added a todo for those new cases that I added. --------- Co-authored-by: David Peter <mail@david-peter.de>
sharkdp
added a commit
that referenced
this pull request
Sep 29, 2025
Part of astral-sh/ty#159 This PR only adjusts the signature of a method so if it has a `self` argument then that argument will have type of `Typing.Self` even if it's not specified. If user provides an explicit annotation then Ty will not override that annotation. - astral-sh/ty#1131 - astral-sh/ty#1157 - astral-sh/ty#1156 - astral-sh/ty#1173 - #20328 - astral-sh/ty#1163 - astral-sh/ty#1196 Added mdtests. Also some tests need #18473 to work completely. So I added a todo for those new cases that I added. --------- Co-authored-by: David Peter <mail@david-peter.de>
sharkdp
added a commit
that referenced
this pull request
Sep 29, 2025
Part of astral-sh/ty#159 This PR only adjusts the signature of a method so if it has a `self` argument then that argument will have type of `Typing.Self` even if it's not specified. If user provides an explicit annotation then Ty will not override that annotation. - astral-sh/ty#1131 - astral-sh/ty#1157 - astral-sh/ty#1156 - astral-sh/ty#1173 - #20328 - astral-sh/ty#1163 - astral-sh/ty#1196 Added mdtests. Also some tests need #18473 to work completely. So I added a todo for those new cases that I added. --------- Co-authored-by: David Peter <mail@david-peter.de>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
We use classes like
_typeshed._type_checker_internals.NamedTupleFallbackto tack on additional attributes/methods to instances of user-definedNamedTuples (orTypedDicts), even though these classes are not present in the MRO of those types.The problem is that those classes use implicit and explicit
Selfannotations which refer toNamedTupleFallbackitself, instead of to the actual type that we're adding those methods to:In effect, when we access
_replaceon an instance of a customNamedTupleinstance, itsselfparameter and return type refer to the wrongSelf. This leads to incorrect "Argument to bound method_replaceis incorrect: Argument typePersondoes not satisfy upper boundNamedTupleFallbackof type variableSelf" errors on #18007. It would also lead to similar errors onTypedDicts, if they would already implement assignability properly.Test Plan
I applied the following patch to typeshed and verified that no errors appear anymore.