-
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
base: main
Are you sure you want to change the base?
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-21 21:39:25.413968494 +0000
+++ new-output.txt 2025-11-21 21:39:28.924985349 +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
@@ -1001,5 +1002,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 1003 diagnostics
+Found 1004 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 |
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.