Forwarding *args and **kwargs¶
When a function takes *args or **kwargs and passes them on, the caller can supply
whatever the callee accepts. stubpy finds the callee and writes those parameters into the
stub, so editors and type checkers see them. Parameters reached through **kwargs are
keyword-only and come after the function’s own; parameters reached through *args are
positional-only (see Forwarding *args).
What the body does with the arguments before passing them on (pop, reading a key,
branching on a literal, copying an object with changes) is in What the body does with the arguments.
Every output below was produced by stubpy render <file>; --no-import gives the same
parameters (without -> None on __init__).
Through the MRO: super().__init__(**kwargs)¶
# mro.py
class Element:
def __init__(self, id: str = "", hidden: bool = False): ...
class Styled(Element):
def __init__(self, fill: str = "none", *, stroke: str = "black", **kwargs):
super().__init__(**kwargs)
class Circle(Styled):
def __init__(self, r: float, **kwargs):
super().__init__(**kwargs)
class Element:
def __init__(self, id: str = '', hidden: bool = False) -> None: ...
class Styled(Element):
def __init__(
self,
fill: str = 'none',
*,
stroke: str = 'black',
id: str = '',
hidden: bool = False,
) -> None: ...
class Circle(Styled):
def __init__(
self,
r: float,
*,
fill: str = 'none',
stroke: str = 'black',
id: str = '',
hidden: bool = False,
) -> None: ...
Circle collects the parameters of every class on the way up. Names the child already
declares are not repeated.
cls(**kwargs) in a class method¶
A class method that forwards into cls(...) is resolved against cls.__init__.
# clsfwd.py
class Base:
def __init__(self, a: int, b: str = "x"): ...
class Widget(Base):
@classmethod
def create(cls, name: str, **kwargs) -> "Widget":
return cls(**kwargs)
class Widget(Base):
@classmethod
def create(
cls,
name: str,
*,
a: int,
b: str = 'x',
) -> Widget: ...
self.method(**kwargs)¶
class Panel:
def __init__(self, title: str, border: int = 1): ...
def configure(self, *, wrap: bool = False, indent: int = 0) -> None: ...
def update(self, title: str, **kwargs) -> None:
self.configure(**kwargs)
class Panel:
def __init__(self, title: str, border: int = 1) -> None: ...
def configure(
self,
*,
wrap: bool = False,
indent: int = 0,
) -> None: ...
def update(
self,
title: str,
*,
wrap: bool = False,
indent: int = 0,
) -> None: ...
Plain function forwarding¶
Module-level functions are resolved too, including chains of them.
# funcs.py
def connect(host: str, port: int = 80, *, timeout: float = 5.0, ssl: bool = False) -> None: ...
def fetch(url: str, **kwargs) -> bytes:
connect("localhost", **kwargs)
return b""
def fetch(
url: str,
*,
port: int = 80,
timeout: float = 5.0,
ssl: bool = False,
) -> bytes: ...
host is missing because the call binds it ("localhost").
Pre-bound arguments with *args, **kwargs¶
Arguments the call binds are removed from the result. When *args is forwarded together
with **kwargs, the remaining positional parameters take its place.
def prebound(*args, **kwargs):
connect("example.org", *args, **kwargs)
def prebound(
port: int = 80,
*,
timeout: float = 5.0,
ssl: bool = False,
): ...
host is bound by the call, so *args becomes port.
def draw(x: int, y: int, *, color: str = "red") -> None: ...
def draw_all(*args, **kwargs) -> None:
draw(*args, **kwargs)
def draw_all(
x: int,
y: int,
*,
color: str = 'red',
) -> None: ...
Positional-only parameters of a callee that end up behind **kwargs are promoted to
positional-or-keyword, so the stub stays valid. An explicitly annotated *args
(*elements: Element) is kept.
Forwarding *args¶
Alone, *args is expanded into positional-only parameters, because the caller can only
pass them by position. Keyword-only parameters of the callee are not offered, and a typed
*rest in the callee types the caller’s *args.
# pos.py
def target(a: int, b: str = "x", /, c: float = 1.0, *rest: bytes, k: bool = False) -> None: ...
def f(*args):
target(*args)
def g(x: str, *args):
target(x, *args)
def f(
a: int,
b: str = 'x',
c: float = 1.0,
/,
*args: bytes,
): ...
def g(
x: str,
b: str = 'x',
c: float = 1.0,
/,
*args: bytes,
): ...
k is dropped. In g the call binds a to x, so *args starts at b; the
caller’s own x comes before positional-only parameters and becomes positional-only too.
Unpacking is followed like f(*args). A slice that drops the first arguments
(*args[1:]) adds one args_i parameter per dropped argument; a slice with an end stays
*args:
# unpack.py
def target(a: int, b: str = "x", /) -> None: ...
def f(*args):
first, *rest = args
target(first, *rest)
def h(*args):
target(*args[1:])
def f(
a: int,
b: str = 'x',
/,
): ...
def h(
args_0: Any,
a: int,
b: str = 'x',
/,
): ...
The same call shapes work as for **kwargs: super().__init__(*args), cls(*args)
in a class method, self.method(*args), plain functions and functools.partial
variables.
Callees with a text signature resolve too: math.atan2(*args) gives
def f(y, x, /): .... An @overload-ed callee gives one @overload per variant,
each with positional-only parameters:
@overload
def ov(a: int, /) -> int: ...
@overload
def ov(a: str, b: str, /) -> str: ...
def ov(*a): ...
def wrapped(*args):
return ov(*args)
@overload
def wrapped(a: int, /) -> int: ...
@overload
def wrapped(
a: str,
b: str,
/,
) -> str: ...
*args and **kwargs can go to different callees; the results combine:
def pos_only(a: int, b: int, /): ...
def kw_only(*, k: int): ...
def both(*args, **kwargs):
pos_only(*args)
kw_only(**kwargs)
def both(
a: int,
b: int,
/,
*,
k: int,
): ...
Other callees and call shapes¶
Attribute callees:
net.connect(**kw)afterfrom . import net,pkg.sub.f(**kw)afterimport pkg.sub, at module level or imported inside the function.A
functools.partialstored in a variable (quick = partial(connect, port=8080)): the bound arguments are applied asinspect.signaturedoes.Merged dictionaries:
inner(**{**defaults, **kwargs, "c": 5})forwards**kwargs; keys written after**kwargs(c) are fixed by the call and left out.A
functools.wrapsdecorator gives the decorated function the signature of the wrapped one.Dataclass,
attrs,NamedTupleandTypedDictconstructors:Point(**kw)expands to the fields;TypedDictkeys become keyword-only, optional keys default to.... Anattrsclass (@attr.s,@attrs.define,@attr.dataclass, …) also gets its synthesised__init__written in its stub: types come from the annotation,type=, a literaldefaultor a builtinfactory,kw_onlyandinit=Falseare respected, a leading underscore is dropped from the parameter name (oralias=used), and a converter function’s annotated input type types the parameter.**kwargs: Unpack[Options]is kept as written in the function’s own stub; a caller forwarding into it gets the keys ofOptions. This holds alike for module functions and methods (self.request(**kwargs)), and whenOptionslives in another module or is imported underif TYPE_CHECKING:. A function that is itself typedUnpack[Options]but forwards into such a callee gets the callee’s keys.A function or method whose parameters are exactly
*args, **kwargs, passed on unchanged to an@overload-ed callee, gets one@overloadper callee variant.A decorator typed with
ParamSpec((*args: P.args, **kwargs: P.kwargs)closing over the wrapped function) keeps the wrapped function’s signature, even withoutfunctools.wraps.
What is not resolved¶
When stubpy cannot find the callee, or the call builds the arguments some other way, the signature is left as written:
def untouched(x, **kwargs): ... # nothing forwarded: unchanged
def g(a, **kwargs):
inner(a, **{k: v for k, v in kwargs.items()}) # computed dictionary: unchanged, WARNING
Turn the expansion off¶
resolve_kwargs = false (--no-resolve-kwargs) keeps **kwargs as written;
resolve_args = false (--no-resolve-args) does the same for *args. The two are
independent.
# multi.py
class Base:
def __init__(self, a: int, b: int = 0): ...
class Child(Base):
def __init__(self, c: str, **kwargs):
super().__init__(**kwargs)
stubpy render multi.py --no-resolve-kwargs:
class Base:
def __init__(self, a: int, b: int = 0) -> None: ...
class Child(Base):
def __init__(self, c: str, **kwargs) -> None: ...
How it works¶
When the module is imported, stubpy combines the live classes (for the MRO and the real
signatures) with an AST pass over the source (for the forwarding calls, pop and
del).
With --imports none (--no-import), or when the import fails or is blocked by the
import policy, nothing is imported. One run keeps an index of the harvested sources; names
are followed through imports (relative, absolute within the package, and imports inside the
function), module attributes, aliases and partial variables; class MROs are linearised
from the source bases. The same call-site rules apply (positional pre-binding, pop /
del, cls(...), type(self)(...), self.m(...), super() and
Base.m(self, ...)), so the result matches the live result wherever the source says enough.
Annotations and defaults written in another module are imported into the stub
(from .base import Color) or dropped when they cannot be.
A base class or callee outside the package is read from the source of the installed
package (or its bundled .pyi or <name>-stubs package), located without importing it.
Standard-library and builtin callees (open, subprocess.run, json.dumps,
logging.Logger.log reached through a typed attribute) resolve in both modes, since
importing the standard library is always allowed. A base that cannot be found stops the
walk. See Source and runtime and Architecture.