mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 14:21:53 +00:00

## Summary Add a typed representation of function signatures (parameters and return type) and infer it correctly from a function. Convert existing usage of function return types to use the signature representation. This does not yet add inferred types for parameters within function body scopes based on the annotations, but it should be easy to add as a next step. Part of #14161 and #13693. ## Test Plan Added tests.
1 KiB
1 KiB
Call expression
Simple
def get_int() -> int:
return 42
reveal_type(get_int()) # revealed: int
Async
async def get_int_async() -> int:
return 42
# TODO: we don't yet support `types.CoroutineType`, should be generic `Coroutine[Any, Any, int]`
reveal_type(get_int_async()) # revealed: @Todo
Generic
def get_int[T]() -> int:
return 42
reveal_type(get_int()) # revealed: int
Decorated
from typing import Callable
def foo() -> int:
return 42
def decorator(func) -> Callable[[], int]:
return foo
@decorator
def bar() -> str:
return "bar"
# TODO: should reveal `int`, as the decorator replaces `bar` with `foo`
reveal_type(bar()) # revealed: @Todo
Invalid callable
nonsense = 123
x = nonsense() # error: "Object of type `Literal[123]` is not callable"
Potentially unbound function
def flag() -> bool: ...
if flag():
def foo() -> int:
return 42
# error: [possibly-unresolved-reference]
reveal_type(foo()) # revealed: int