From ac3cf4325e16bed29e243aa36b8a82f36e874cca Mon Sep 17 00:00:00 2001 From: David Peter Date: Tue, 23 Sep 2025 14:48:38 +0200 Subject: [PATCH] Mark implicit `Self` as inferable --- .../resources/mdtest/generics/scoping.md | 2 +- .../resources/mdtest/narrow/isinstance.md | 4 ---- crates/ty_python_semantic/src/types/signatures.rs | 8 +++++++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/crates/ty_python_semantic/resources/mdtest/generics/scoping.md b/crates/ty_python_semantic/resources/mdtest/generics/scoping.md index 6d490fd8ac..fb8b74428c 100644 --- a/crates/ty_python_semantic/resources/mdtest/generics/scoping.md +++ b/crates/ty_python_semantic/resources/mdtest/generics/scoping.md @@ -117,7 +117,7 @@ reveal_type(bound_method.__func__) # revealed: def f(self, x: int) -> str reveal_type(C[int]().f(1)) # revealed: str reveal_type(bound_method(1)) # revealed: str -# error: [invalid-argument-type] "Argument to function `f` is incorrect: Argument type `Literal[1]` does not satisfy upper bound `C[Unknown]` of type variable `Self`" +# error: [invalid-argument-type] "Argument to function `f` is incorrect: Argument type `Literal[1]` does not satisfy upper bound `C[T@C]` of type variable `Self`" C[int].f(1) # error: [missing-argument] reveal_type(C[int].f(C[int](), 1)) # revealed: str diff --git a/crates/ty_python_semantic/resources/mdtest/narrow/isinstance.md b/crates/ty_python_semantic/resources/mdtest/narrow/isinstance.md index 36bef265ad..486c354c24 100644 --- a/crates/ty_python_semantic/resources/mdtest/narrow/isinstance.md +++ b/crates/ty_python_semantic/resources/mdtest/narrow/isinstance.md @@ -363,12 +363,8 @@ class Invariant[T]: def _(x: object): if isinstance(x, Invariant): reveal_type(x) # revealed: Top[Invariant[Unknown]] - # error: [invalid-argument-type] "Argument to bound method `get` is incorrect: Expected `Self@get`, found `Top[Invariant[Unknown]]`" - # error: [invalid-argument-type] "Argument to bound method `get` is incorrect: Argument type `Top[Invariant[Unknown]]` does not satisfy upper bound `Bottom[Invariant[Unknown]]` of type variable `Self`" reveal_type(x.get()) # revealed: object # error: [invalid-argument-type] "Argument to bound method `push` is incorrect: Expected `Never`, found `Literal[42]`" - # error: [invalid-argument-type] "Argument to bound method `push` is incorrect: Expected `Self@push`, found `Top[Invariant[Unknown]]`" - # error: [invalid-argument-type] "Argument to bound method `push` is incorrect: Argument type `Top[Invariant[Unknown]]` does not satisfy upper bound `Bottom[Invariant[Unknown]]` of type variable `Self`" x.push(42) ``` diff --git a/crates/ty_python_semantic/src/types/signatures.rs b/crates/ty_python_semantic/src/types/signatures.rs index 401c2099a7..d2011aa55c 100644 --- a/crates/ty_python_semantic/src/types/signatures.rs +++ b/crates/ty_python_semantic/src/types/signatures.rs @@ -1295,9 +1295,15 @@ impl<'db> Parameters<'db> { let typevar_binding_context = Some(definition); let index = semantic_index(db, scope_id.file(db)); let class = nearest_enclosing_class(db, index, scope_id).unwrap(); - Type::TypeVar( + Type::NonInferableTypeVar( get_self_type(db, scope_id, typevar_binding_context, class).unwrap(), ) + .apply_type_mapping( + db, + &TypeMapping::MarkTypeVarsInferable(Some(BindingContext::Definition( + definition, + ))), + ) }; Parameter { annotated_type: Some(implicit_annotation),