-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Substitute for typing.Self when checking protocol members
#21569
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
Conversation
| static_assert(is_assignable_to(NominalWithSelf, UsesSelf)) | ||
| # TODO: should pass | ||
| static_assert(is_subtype_of(NominalWithSelf, UsesSelf)) # error: [static-assert-error] | ||
| static_assert(is_subtype_of(NominalWithSelf, UsesSelf)) |
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.
There are other Self-related tests in this file that are still TODO, since they also require being able to check against generic protocol members correctly.
Diagnostic diff on typing conformance testsChanges were detected when running ty on typing conformance tests--- old-output.txt 2025-11-24 18:49:42.174021411 +0000
+++ new-output.txt 2025-11-24 18:49:45.948033270 +0000
@@ -504,6 +504,7 @@
generics_self_basic.py:64:38: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `Self@set_value`
generics_self_basic.py:67:26: error[invalid-type-form] Special form `typing.Self` expected no type parameter
generics_self_protocols.py:61:19: error[invalid-argument-type] Argument to function `accepts_shape` is incorrect: Expected `ShapeProtocol`, found `BadReturnType`
+generics_self_protocols.py:64:19: error[invalid-argument-type] Argument to function `accepts_shape` is incorrect: Expected `ShapeProtocol`, found `ReturnDifferentClass`
generics_self_usage.py:50:34: error[invalid-assignment] Object of type `def foo(self) -> int` is not assignable to `(typing.Self, /) -> int`
generics_self_usage.py:73:14: error[invalid-type-form] Variable of type `typing.Self` is not allowed in a type expression
generics_self_usage.py:73:23: error[invalid-type-form] Variable of type `typing.Self` is not allowed in a type expression
@@ -1002,5 +1003,5 @@
typeddicts_usage.py:28:17: error[missing-typed-dict-key] Missing required key 'name' in TypedDict `Movie` constructor
typeddicts_usage.py:28:18: error[invalid-key] Unknown key "title" for TypedDict `Movie`: Unknown key "title"
typeddicts_usage.py:40:24: error[invalid-type-form] The special form `typing.TypedDict` is not allowed in type expressions. Did you mean to use a concrete TypedDict or `collections.abc.Mapping[str, object]` instead?
-Found 1004 diagnostics
+Found 1005 diagnostics
WARN A fatal error occurred while checking some files. Not all project files were analyzed. See the diagnostics list above for details.
|
|
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-argument-type |
0 | 0 | 12 |
unused-ignore-comment |
0 | 1 | 0 |
| Total | 0 | 1 | 12 |
|
Typing conformance result is a new true positive |
|
Ecosystem results look good now, too — showing a more precise type in the diagnostics |
|
(I'd love to take a look at this one before it goes in, if that's okay, just to make sure I understand everything that's going on!) |
👍 no problem, I'll wait to merge on an okay from you |
AlexWaygood
left a comment
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.
Thank you!!
Typing conformance result is a new true positive
Would you be able to add a regression test for this to protocols.md?
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
* origin/main: (27 commits) [ty] Add hint about resolved Python version when a user attempts to import a member added on a newer version (#21615) Use release commit for actions/checkout (#21610) [ty] Add failing mdtest for known `Protocol` panic (#21594) [`parser`] Fix panic when parsing IPython escape command expressions (#21480) Fix cargo shear in CI (#21609) Update actions/checkout digest to c2d88d3 (#21601) Update dependency ruff to v0.14.6 (#21603) Update astral-sh/setup-uv action to v7.1.4 (#21602) Update Rust crate clap to v4.5.53 (#21604) Update taiki-e/install-action action to v2.62.56 (#21608) Update Rust crate hashbrown to v0.16.1 (#21605) Update Rust crate indexmap to v2.12.1 (#21606) Update Rust crate syn to v2.0.111 (#21607) [ty] Check method definitions on subclasses for Liskov violations (#21436) [ty] Fix panic for unclosed string literal in type annotation position (#21592) [ty] Fix rendering of unused suppression diagnostic (#21580) [ty] Improve lsp handling of hover/goto on imports (#21572) [ty] Improve diagnostics when a submodule is not available as an attribute on a module-literal type (#21561) [ty] Improve concise diagnostics for invalid exceptions when a user catches a tuple of objects (#21578) [ty] upgrade salsa (#21575) ...
Done |
…d-typevar * origin/main: (30 commits) [ty] Double click to insert inlay hint (#21600) [ty] Switch the error code from `unresolved-attribute` to `possibly-missing-attribute` for submodules that may not be available (#21618) [ty] Substitute for `typing.Self` when checking protocol members (#21569) [ty] Don't suggest things that aren't subclasses of `BaseException` after `raise` [ty] Add hint about resolved Python version when a user attempts to import a member added on a newer version (#21615) Use release commit for actions/checkout (#21610) [ty] Add failing mdtest for known `Protocol` panic (#21594) [`parser`] Fix panic when parsing IPython escape command expressions (#21480) Fix cargo shear in CI (#21609) Update actions/checkout digest to c2d88d3 (#21601) Update dependency ruff to v0.14.6 (#21603) Update astral-sh/setup-uv action to v7.1.4 (#21602) Update Rust crate clap to v4.5.53 (#21604) Update taiki-e/install-action action to v2.62.56 (#21608) Update Rust crate hashbrown to v0.16.1 (#21605) Update Rust crate indexmap to v2.12.1 (#21606) Update Rust crate syn to v2.0.111 (#21607) [ty] Check method definitions on subclasses for Liskov violations (#21436) [ty] Fix panic for unclosed string literal in type annotation position (#21592) [ty] Fix rendering of unused suppression diagnostic (#21580) ...
* main: [ty] Extend Liskov checks to also cover classmethods and staticmethods (astral-sh#21598) Dogfood ty on the `scripts` directory (astral-sh#21617) [ty] support generic aliases in `type[...]`, like `type[C[int]]` (astral-sh#21552) [ty] Retain the function-like-ness of `Callable` types when binding `self` (astral-sh#21614) [ty] Distinguish "unconstrained" from "constrained to any type" (astral-sh#21539) Disable ty workspace diagnostics for VSCode users (astral-sh#21620) [ty] Double click to insert inlay hint (astral-sh#21600) [ty] Switch the error code from `unresolved-attribute` to `possibly-missing-attribute` for submodules that may not be available (astral-sh#21618) [ty] Substitute for `typing.Self` when checking protocol members (astral-sh#21569) [ty] Don't suggest things that aren't subclasses of `BaseException` after `raise` [ty] Add hint about resolved Python version when a user attempts to import a member added on a newer version (astral-sh#21615)
This patch updates our protocol assignability checks to substitute for any occurrences of
typing.Selfin method signatures, replacing it with the class being checked for assignability against the protocol.This requires a new helper method on signatures,
apply_self, which substitutes occurrences oftyping.Selfwithout binding theselfparameter.We also update the
try_upcast_to_callablemethod. Before, it would return aType, since certain types upcast to a union of callables, not to a single callable. However, even in that case, we know that every element of the union is a callable. We now return a vector ofCallableType. (Actually a smallvec to handle the most common case of a single callable; and wrapped in a new type so that we can provide helper methods.) If there is more than one element in the result, it represents a union of callables. This lets callers get at theCallableTypeinstances in a more type-safe way. (This makes it easier for our protocol checking code to call the newapply_selfhelper.) We also provide aninto_typemethod so that callers that really do want aTypecan get the original result easily.