Skip to content

Commit b82a152

Browse files
authored
Fix with_signature docstring and api docs: it does support type hints (#83)
1 parent 87ff051 commit b82a152

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

docs/api.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ with_signature(
121121
```
122122

123123
Add a signature to a function of type `f(*args, **kwargs)`. Can be used as a decorator
124-
with or without arguments.
124+
with or without arguments. The dict form of `args`/`kwargs` maps parameter names to
125+
type-hint strings and `return_annotation` sets the return type — both written to the
126+
wrapper's `__signature__`. Default values cannot be set.
125127

126128
`dags.signature`
127129

src/dags/signature.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,30 +83,34 @@ def with_signature(
8383
) -> Callable[P, R] | Callable[[Callable[P, R]], Callable[P, R]]:
8484
"""Add a signature to a function of type `f(*args, **kwargs)` (decorator).
8585
86-
The user-described view (parameter names, kinds, and any type strings
87-
passed via `args` / `kwargs`) is written to the wrapper's
88-
`__signature__`. The wrapper's `__annotations__` advertises the
89-
`*args, **kwargs` forwarder shape instead — the wrapper genuinely
86+
The user-described view (parameter names, kinds, and any type hints passed
87+
via the dict form of `args` / `kwargs` or via `return_annotation`) is written
88+
to the wrapper's `__signature__`. The wrapper's `__annotations__` advertises
89+
the `*args, **kwargs` forwarder shape instead — the wrapper genuinely
9090
accepts anything at the Python level, and only the wrapped function
9191
expects the user-typed arguments. Runtime type checkers (beartype,
9292
typeguard) read `__annotations__` and therefore treat the wrapper as
9393
permissive; `dags.get_annotations` recovers the user view from
9494
`__signature__` via its built-in args/kwargs-mismatch fallback.
9595
96-
Caveats: The created signature only contains the names of arguments and whether
97-
they are keyword-only. There is no way of setting default values or type hints.
96+
The created signature carries argument names, whether each argument is
97+
keyword-only, and—when the dict form of `args`/`kwargs` is used or
98+
`return_annotation` is set—parameter and return type hints (written to
99+
`__signature__`, as described above). Default values cannot be set.
98100
99101
Args:
100102
func: The function to be decorated. Should take `*args`
101103
and `**kwargs` as only arguments.
102104
args: If a list, the names of positional or keyword arguments. If a dict,
103-
the names of positional or keyword arguments and their types as strings.
105+
the names of positional or keyword arguments mapped to their type hints
106+
(as strings).
104107
kwargs: If a list, the names of keyword only arguments. If a dict,
105-
the names of keyword only arguments and their types as strings.
108+
the names of keyword only arguments mapped to their type hints (as
109+
strings).
106110
enforce: Whether the signature should be enforced or just
107111
added to the function for introspection. This creates runtime
108112
overhead.
109-
return_annotation: The return annotation. By default, the return annotation is
113+
return_annotation: The return annotation. By default, it is
110114
`inspect.Parameter.empty`.
111115
112116
Returns:

0 commit comments

Comments
 (0)