Hints

Decorators that set what introspection cannot see. Worked examples are in Hints.

Decorators that tell stubpy what introspection cannot see; each only sets an attribute and returns the object unchanged. stubpy also reads them from the source, so they work without importing the module and never appear in the stub.

ignore(obj: T) → T[source]

Leave a function, method or class out of the stub.

Parameters:

obj (callable or type) – The function, method or class to hide.

Returns:

callable or type – obj, unchanged apart from a marker attribute.

See also

signature

Replace the stub signature instead.

Examples

>>> @ignore
... def _debug_dump():
...     pass
>>> _debug_dump.__stubpy_ignore__
True
returns(annotation: str) → Callable[[T], T][source]

Replace only the return annotation in the stub.

The parameters are still taken from the function itself.

Parameters:

annotation (str) – The return type as it should appear in the stub, e.g. "list[int]".

Returns:

callable – A decorator that records annotation and returns its argument unchanged.

Raises:

TypeError – If annotation is not a string.

See also

signature

Replace the whole signature.

Examples

>>> @returns("list[int]")
... def evens(n):
...     return list(range(0, n, 2))
>>> evens(5)
[0, 2, 4]
signature(text: str) → Callable[[T], T][source]

Replace the stub signature of a function or method.

Use it for callables introspection cannot describe: JIT-compiled, C-implemented or dynamically built ones.

Parameters:

text (str) – Everything that follows the name in the def line: the parenthesised parameters and, optionally, -> return. Methods include self / cls, e.g. "(self, x: int) -> str".

Returns:

callable – A decorator that records text and returns its argument unchanged.

Raises:

See also

returns

Replace only the return annotation.

ignore

Leave the object out of the stub.

Examples

>>> @signature("(x: float) -> float")
... def square(x):
...     return x * x
>>> square(3)
9