diff --git a/crates/ty_python_semantic/resources/mdtest/expression/lambda.md b/crates/ty_python_semantic/resources/mdtest/expression/lambda.md index dbafc217ac..b48efaad70 100644 --- a/crates/ty_python_semantic/resources/mdtest/expression/lambda.md +++ b/crates/ty_python_semantic/resources/mdtest/expression/lambda.md @@ -115,3 +115,22 @@ a5: Callable[[], None] = lambda x: None # error: [invalid-assignment] a6: Callable[[int], None] = lambda: None ``` + +## Function-like behavior of lambdas + +All `lambda` functions are instances of `types.FunctionType` and should have access to the same set +of attributes. + +```py +x = lambda y: y + +reveal_type(x.__code__) # revealed: CodeType +reveal_type(x.__name__) # revealed: str +reveal_type(x.__defaults__) # revealed: tuple[Any, ...] | None +reveal_type(x.__annotations__) # revealed: dict[str, @Todo(Support for `typing.TypeAlias`)] +reveal_type(x.__dict__) # revealed: dict[str, Any] +reveal_type(x.__doc__) # revealed: str | None +reveal_type(x.__kwdefaults__) # revealed: dict[str, Any] | None +reveal_type(x.__module__) # revealed: str +reveal_type(x.__qualname__) # revealed: str +``` diff --git a/crates/ty_python_semantic/src/types/infer.rs b/crates/ty_python_semantic/src/types/infer.rs index 699047d671..2120b4a944 100644 --- a/crates/ty_python_semantic/src/types/infer.rs +++ b/crates/ty_python_semantic/src/types/infer.rs @@ -4956,7 +4956,7 @@ impl<'db> TypeInferenceBuilder<'db> { // TODO: Useful inference of a lambda's return type will require a different approach, // which does the inference of the body expression based on arguments at each call site, // rather than eagerly computing a return type without knowing the argument types. - CallableType::single(self.db(), Signature::new(parameters, Some(Type::unknown()))) + CallableType::function_like(self.db(), Signature::new(parameters, Some(Type::unknown()))) } /// Returns the type of the first parameter if the given scope is function-like (i.e. function or lambda).