Type hints in Hypothesis¶
We ship type hints with Hypothesis itself. Any PEP 561-compatible type-checker, such as mypy or pyright, will automatically pick up on them.
Note
Hypothesis’ type hints may make breaking changes between minor releases.
We may also find more precise ways to describe the type of various interfaces, or change their type and runtime behaviour together in a way which is otherwise backwards-compatible. We often omit type hints for deprecated features or arguments, as an additional form of warning.
There are known issues with inferring the type of examples generated by deferred()
, recursive()
, one_of()
, dictionaries()
, and fixed_dictionaries()
. We’re following proposed updates to Python’s typing standards, but unfortunately the long-standing interfaces of these strategies cannot (yet) be statically typechecked.
Writing downstream type hints¶
Projects that provide Hypothesis strategies and use type hints may wish to annotate their strategies too. For example:
def foo_strategy() -> SearchStrategy[Foo]: ...
SearchStrategy
is the type of all strategy objects. It is a generic type, and covariant in the type of the examples it creates. For example:
integers()
is of typeSearchStrategy[int]
.lists(integers())
is of typeSearchStrategy[List[int]]
.SearchStrategy[Dog]
is a subtype ofSearchStrategy[Animal]
ifDog
is a subtype ofAnimal
(as seems likely).