[ty] Support implicit type of cls in signatures#21771
Conversation
cls in signaturescls in signatures
Diagnostic diff on typing conformance testsChanges were detected when running ty on typing conformance tests--- old-output.txt 2025-12-10 20:28:22.520858317 +0000
+++ new-output.txt 2025-12-10 20:28:26.248881010 +0000
@@ -511,17 +511,13 @@
generics_self_advanced.py:28:25: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `Self@method1`
generics_self_advanced.py:36:9: error[type-assertion-failure] Type `list[Self@method2]` does not match asserted type `list[<special form 'typing.Self'>]`
generics_self_advanced.py:37:9: error[type-assertion-failure] Type `Self@method2` does not match asserted type `<special form 'typing.Self'>`
-generics_self_advanced.py:38:9: error[type-assertion-failure] Type `Self@method2` does not match asserted type `Unknown`
generics_self_advanced.py:43:9: error[type-assertion-failure] Type `list[Self@method3]` does not match asserted type `list[<special form 'typing.Self'>]`
generics_self_advanced.py:44:9: error[type-assertion-failure] Type `Self@method3` does not match asserted type `<special form 'typing.Self'>`
-generics_self_advanced.py:45:9: error[type-assertion-failure] Type `Self@method3` does not match asserted type `Unknown`
generics_self_attributes.py:26:33: error[invalid-argument-type] Argument is incorrect: Expected `<special form 'typing.Self'> | None`, found `LinkedList[int]`
generics_self_attributes.py:29:5: error[invalid-assignment] Object of type `OrdinalLinkedList` is not assignable to attribute `next` of type `<special form 'typing.Self'> | None`
generics_self_attributes.py:32:5: error[invalid-assignment] Object of type `LinkedList[int]` is not assignable to attribute `next` of type `<special form 'typing.Self'> | None`
generics_self_basic.py:20:16: error[invalid-return-type] Return type does not match returned value: expected `Self@method2`, found `Shape`
generics_self_basic.py:33:16: error[invalid-return-type] Return type does not match returned value: expected `Self@cls_method2`, found `Shape`
-generics_self_basic.py:54:1: error[type-assertion-failure] Type `Shape` does not match asserted type `Unknown`
-generics_self_basic.py:55:1: error[type-assertion-failure] Type `Circle` does not match asserted type `Unknown`
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`
@@ -1025,4 +1021,4 @@
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
-Found 1027 diagnostics
+Found 1023 diagnostics
|
|
CodSpeed Performance ReportMerging #21771 will degrade performances by 5.94%Comparing Summary
Benchmarks breakdown
Footnotes
|
05560fb to
a7521b1
Compare
be2c966 to
0317023
Compare
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-argument-type |
160 | 2 | 3 |
type-assertion-failure |
2 | 137 | 7 |
possibly-missing-attribute |
75 | 0 | 46 |
unresolved-attribute |
55 | 0 | 0 |
unused-ignore-comment |
0 | 49 | 0 |
invalid-return-type |
29 | 0 | 1 |
invalid-assignment |
8 | 1 | 2 |
unsupported-operator |
5 | 0 | 0 |
not-iterable |
2 | 0 | 0 |
deprecated |
1 | 0 | 0 |
no-matching-overload |
1 | 0 | 0 |
non-subscriptable |
1 | 0 | 0 |
unsupported-base |
0 | 1 | 0 |
| Total | 339 | 190 | 59 |
carljm
left a comment
There was a problem hiding this comment.
Code changes look fine here, conformance suite changes look good.
Ecosystem report reveals a problem that looks widespread enough that I think we probably need to fix it before shipping this. For a final generic class with a classmethod, when the classmethod is accessed off the class with no explicit specialization, we expect the default specialization of the class as cls parameter, but we receive the unspecialized ClassLiteral instead. Repro example:
from typing import final
@final
class P[T]:
x: T
@classmethod
def method(cls):
pass
# this works fine due to the explicit specialization: we expect `P[int]` and receive `P[int]`
P[int].method()
# this breaks: we expect `P[Unknown]` but we receive the class-literal `P`
P.method()This also repros on main (in a desugared version). I filed astral-sh/ty#1832 and put up #21883 to fix. I expect that if this PR is rebased on that, these ecosystem false positives should go away.
|
I wanted to rebase this, but I guess we should fix and merge #21770 first |
2b923ed to
7dfe6e0
Compare
0317023 to
db3eb6b
Compare
|
It looks like the |
|
The rest of the ecosystem report looks correct, and the typing conformance changes are all good. |
…-cycle * origin/main: [ty] Support implicit type of `cls` in signatures (#21771) [ty] add `SyntheticTypedDictType` and implement `normalized` and `is_equivalent_to` (#21784) [ty] Fix disjointness checks with type-of `@final` classes (#21770) [ty] Fix negation upper bounds in constraint sets (#21897)
* origin/main: (29 commits) Document range suppressions, reorganize suppression docs (#21884) Ignore ruff:isort like ruff:noqa in new suppressions (#21922) [ty] Handle `Definition`s in `SemanticModel::scope` (#21919) [ty] Attach salsa db when running ide tests for easier debugging (#21917) [ty] Don't show hover for expressions with no inferred type (#21924) [ty] avoid unions of generic aliases of the same class in fixpoint (#21909) [ty] Squash false positive logs for failing to find `builtins` as a real module [ty] Uniformly use "not supported" in diagnostics (#21916) [ty] Reduce size of ty-ide snapshots (#21915) [ty] Adjust scope completions to use all reachable symbols [ty] Rename `all_members_of_scope` to `all_end_of_scope_members` [ty] Remove `all_` prefix from some routines on UseDefMap Enable `--document-private-items` for `ruff_python_formatter` (#21903) Remove `BackwardsTokenizer` based `parenthesized_range` references in `ruff_linter` (#21836) [ty] Revert "Do not infer types for invalid binary expressions in annotations" (#21914) Skip over trivia tokens after re-lexing (#21895) [ty] Avoid inferring types for invalid binary expressions in string annotations (#21911) [ty] Improve overload call resolution tracing (#21913) [ty] fix missing heap_size on Salsa query (#21912) [ty] Support implicit type of `cls` in signatures (#21771) ...
Summary
Extends #20517 to support the implicit type of
clsin@classmethodsignatures. Part of astral-sh/ty#159.This PR is stacked on #21770.