Inferred types¶
stubpy writes the types the source and the runtime state. Where the source has none, it
fills in only what is certain from the code; types read from docstrings need
--infer-types (infer_types = true) unless they come from a forwarded callee.
What |
When |
Source of the type |
|---|---|---|
Parameter with a literal default |
always |
The literal’s type: |
Keyword forwarded from an unannotated callee |
always (imported modules) |
The callee’s literal default, else its NumPy, Google or Sphinx docstring
(for |
Unannotated return |
always |
The safe rules below. |
The function’s own parameters and return |
|
Its docstring. |
Safe return types¶
A function without a return annotation gets one only when every return agrees on a
type that cannot be wrong:
no
returnwith a value:-> None(stub bodies, generators and functions that end inraiseexcepted);every
returna literal of one type:return 1/return 2give-> int;return selfandreturn cls(...)/return type(self)(...):-> Selfwhentarget_pythonis 3.11 or later, else the class name;return Name(...)whereNameis a class of the module:-> Name;f-strings and
strmethods of a string literal (", ".join(x)):-> str;builtins whose result type is fixed:
len/hash→int,repr/str→str,isinstance/hasattr→bool, and so on;not xandis/is not/in/not intests:-> bool.
Nothing is inferred when two returns disagree, when the body can fall off the end after a
return value (an implicit None), for == and < comparisons (NumPy arrays
return arrays), when a name the rule relies on is rebound in the function, or when the
function has a decorator other than staticmethod, classmethod, property,
functools.cache / lru_cache / cached_property, override or final.
These rules apply by default because they read only the code and are exact for it; a
written annotation, a @hint.returns or a protocol type such as __len__ -> int always
wins. Docstring types are prose that may be stale, so for the function’s own signature they
stay behind --infer-types.
class Builder:
def chain(self):
return self
def label(self):
return f"b{self.n}"
def reset(self):
self.n = 0
def pick(self, other):
return other
class Builder:
def chain(self) -> Builder: ... # Self with target_python = "3.11"
def label(self) -> str: ...
def reset(self) -> None: ...
def pick(self, other): ...
Docstring types¶
--infer-types reads NumPy, Google and Sphinx docstrings, from the source when the module
is not imported (--imports none) and from the live object otherwise:
Docstring |
Stub |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
A type made only of builtins and typing names becomes an annotation. Any other type
(array_like, pathlib.Path) is kept as a # type: comment on the parameter, and a
return type is dropped. --verbose lists, per function, how many types were filled and
which were kept as comments.
Aliases from private modules¶
A type alias defined in a private module (one with a _-prefixed part, such as
pkg._types) is written as its value when the module does not list it in __all__:
users of the stub cannot import it by name.
# pkg/_types.py
VerifyType = bool | str
# pkg/api.py
from . import _types as _t
def get(url: str, verify: _t.VerifyType = True) -> None: ...
def get(url: str, verify: bool | str = True) -> None: ...
Aliases from public modules, and aliases a private module exports through __all__
(a shared _typing module, say), keep their names and are imported. NewType and
type statements always keep their names. The rewrite needs the module to be imported;
with --no-import the annotation is copied as written.