Skip to content

Add static typing to Hypothesis API #200

@DRMacIver

Description

@DRMacIver

The new typing module in 3.5 and PEP 0484 add static type annotations to Python. It would be nice for the Hypothesis API to support this, as the public API is actually pretty static in nature. stub files offer a compatibility mode, so this can be done transparently without dropping support for Python 2 (which is sadly there for the long haul).

Steps this would require:

  1. Making Strategy a generic type with a single parameter, Strategy[T] that specifies the type of value generated. Note that Strategy is covariant in its type parameter.
  2. Providing static types for all of the public API in hypothesis and hypothesis.strategies.
  3. Running a mypy checker on Hypothesis + its test suite as part of the build

Things to note:

  1. given and builds cannot be precisely typed with this type system, but they can still have types that are better than nothing by constraining their *args and **kwargs to have a strategy type (this is actually quite useful because it will catch two common gotchas when using this).
  2. Settings is going to be quite hard to type correctly and is probably not worth bothering with.
  3. I am not proposing to make Hypothesis internals typed. In particular templates for strategies will remain completely untyped. I've worked out what typing this accurately requires in the past and PEP 0484 can't begin to express it.

Metadata

Metadata

Assignees

Labels

enhancementit's not broken, but we want it to be bettermetafor wider topics than the software itself

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions