simplify
diff --git a/crates/ty_python_semantic/resources/mdtest/call/methods.md b/crates/ty_python_semantic/resources/mdtest/call/methods.md index 55cb506..22b0f45 100644 --- a/crates/ty_python_semantic/resources/mdtest/call/methods.md +++ b/crates/ty_python_semantic/resources/mdtest/call/methods.md
@@ -446,8 +446,6 @@ reveal_type(DerivedBox.from_value(value)) # revealed: DerivedBox[int] reveal_type(Box[str].from_value("value")) # revealed: Box[str] -# revealed: bound method <class 'Box[str]'>.from_value(value: str) -> Box[str] -reveal_type(Box[str].from_value) Box[str].from_value(1) # error: [invalid-argument-type] class ReceiverBox[T]: @@ -576,12 +574,8 @@ class Wrapper[T](HasModel[Inner[T]]): ... -def want(wrapper: Wrapper[int]) -> None: ... - obj = Concrete() reveal_type(Wrapper.from_model(obj)) # revealed: Wrapper[int] -reveal_type(Wrapper[int].from_model(obj)) # revealed: Wrapper[int] -want(Wrapper.from_model(obj)) ``` ### Type parameter defaults do not prevent method inference
diff --git a/crates/ty_python_semantic/src/types.rs b/crates/ty_python_semantic/src/types.rs index dc9c17c..0cb3bcc 100644 --- a/crates/ty_python_semantic/src/types.rs +++ b/crates/ty_python_semantic/src/types.rs
@@ -1064,6 +1064,31 @@ ) } + /// Adds an inherited generic context to callable elements of this type. + fn with_inherited_generic_context( + self, + db: &'db dyn Db, + generic_context: GenericContext<'db>, + ) -> Self { + match self { + Type::FunctionLiteral(function) => { + Type::FunctionLiteral(function.with_inherited_generic_context(db, generic_context)) + } + Type::Callable(callable) => Type::Callable(CallableType::new( + db, + callable + .signatures(db) + .with_inherited_generic_context(db, generic_context), + callable.kind(db), + callable.provenance(db), + )), + Type::Union(union) => union.map(db, |element| { + element.with_inherited_generic_context(db, generic_context) + }), + _ => self, + } + } + /// Returns `true` if `self` is [`Type::Callable`]. pub(crate) const fn is_callable_type(&self) -> bool { matches!(self, Type::Callable(..)) @@ -2986,16 +3011,8 @@ let return_ty = if ty.is_instance_of(db, KnownClass::Staticmethod) && let Type::ClassLiteral(class) = owner && let Some(generic_context) = class.generic_context(db) - && let Type::Callable(callable) = return_ty { - Type::Callable(CallableType::new( - db, - callable - .signatures(db) - .with_inherited_generic_context(db, generic_context), - callable.kind(db), - callable.provenance(db), - )) + return_ty.with_inherited_generic_context(db, generic_context) } else { return_ty };
diff --git a/crates/ty_python_semantic/src/types/class/static_literal.rs b/crates/ty_python_semantic/src/types/class/static_literal.rs index bd2436a..f5a6b23 100644 --- a/crates/ty_python_semantic/src/types/class/static_literal.rs +++ b/crates/ty_python_semantic/src/types/class/static_literal.rs
@@ -992,30 +992,6 @@ } } - fn with_inherited_generic_context<'d>( - db: &'d dyn Db, - ty: Type<'d>, - generic_context: GenericContext<'d>, - ) -> Type<'d> { - match ty { - Type::FunctionLiteral(function) => Type::FunctionLiteral( - function.with_inherited_generic_context(db, generic_context), - ), - Type::Callable(callable) => Type::Callable(CallableType::new( - db, - callable - .signatures(db) - .with_inherited_generic_context(db, generic_context), - callable.kind(db), - callable.provenance(db), - )), - Type::Union(union) => union.map(db, |element| { - with_inherited_generic_context(db, *element, generic_context) - }), - _ => ty, - } - } - fn into_function_like_callable<'d>(db: &'d dyn Db, ty: Type<'d>) -> Type<'d> { match ty { Type::Callable(callable_ty) => Type::Callable(CallableType::new( @@ -1048,7 +1024,7 @@ name, policy, ) - .map_type(|ty| with_inherited_generic_context(db, ty, generic_context)); + .map_type(|ty| ty.with_inherited_generic_context(db, generic_context)); } // We generally treat dunder attributes with `Callable` types as function-like callables.