Skip to content

callable arrow syntax

basedpython adds an arrow form for callable types so function-typed annotations read naturally:

on_done: (int, str) -> bool
fetch: () -> bytes
curry: (int) -> (str) -> bool

transpiles to:

on_done: Callable[[int, str], bool]
fetch: Callable[[], bytes]
curry: Callable[[int], Callable[[str], bool]]

syntax

  • -> separates parameters from return type
  • form nests: right side can itself be callable, producing curried Callable[[A], Callable[[B], C]]
  • bare positional types ((int, str)) get implicit positional-only treatment when followed by named parameters

parameter forms

gradual (...)

a single bare ellipsis parameter list is the gradual "any arguments" callable:

f: (...) -> int

Callable[..., int] (not Callable[[...], int]). it accepts any argument list, and the reverse transpile maps Callable[..., R] back to (...) -> R

bare positional

f: (int, str) -> bool

Callable[[int, str], bool]

named parameter

f: (a: int, b: str) -> bool

named parameters are non-denotable in Callable[...]; basedpython emits an equivalent shape behind the scenes

positional-only marker /

f: (int, /, name: str) -> bool

explicit / marks preceding params positional-only. implicit / is inserted after the last bare positional when followed by a named parameter

keyword-only marker *

f: (int, *, name: str) -> bool

bare * marks following params keyword-only

variadic *args

f: (*: int) -> int             # anonymous
g: (*args: int) -> int         # named

kwargs **kwargs

f: (**: str) -> int            # anonymous
g: (**kwargs: str) -> int      # named

full form

f: (int, /, name: str, *args: bool, **kwargs: int) -> None

scope

arrow form recognized only in syntactic type positions: parameter annotations, return-type annotations, AnnAssign targets, type aliases, and subscript slices that are themselves type contexts (list[(int) -> int]). value-context (int) -> int remains a parse error

nesting

callable nests inside callable, union, intersection, tuple, subscript:

a: (int) -> int | None
b: list[(int) -> int]
c: (int) -> (str) -> bool
d: ((int) -> int) & Hashable

polyfill

  • Callable imported from typing once per module using denotable form
  • Protocol imported from typing once per module using non-denotable form