Description
largely copy/pasted from ethereum/py-evm#1398
Background
Type hints allow us to perform static type checking, among other things. They raise the security bar by catching bugs at development time that, without type support, may turn into runtime bugs.
This stackoverflow answer does a great job at describing their main benefits.
What is wrong?
The web3.py library isn't written with type hints.
This needs to be fixed by:
- Adding all missing type hints.
- Enforcing type checking in CI
How
There does exist tooling (monkeytype) to the generation of type hints for existing code bases. From my personal experience monkeytype
can be helpful but does still require manual fine tuning. Also, manually adding these type hints does serve as a great boost to the general understanding of the code base as it forces one to think about the code.
-
Run
mypy --follow-imports=silent --warn-unused-ignores --ignore-missing-imports --no-strict-optional --check-untyped-defs --disallow-incomplete-defs --disallow-untyped-defs --disallow-any-generics -p web3 -p ens
-
Eliminate every reported error by adding the right type hint
-
This should be done incrementally. The following steps are merely a suggestion on what those steps might be. A good target for pull request size is <400 lines of code changed but this isn't a hard rule.
- individual
web3._utils
modules (3762 sloc) web3.contract
module (1561 sloc)web3.middlewares
module (1307 slot)web3.providers
module (1212 slot)- everything that's left.
In order to incrementally enforce these in CI the mypy
command should be added to the lint
section of the tox.ini
and can be incrementally updated to only look at the modules which have had their type hints added.
- Send frequent pull requests for chunks of work
Definition of done
This issue is done when the following criteria are met:
- Type hinting is part of CI under the
lint
environment in thetox.ini
file
It needs to be:
mypy --follow-imports=silent --warn-unused-ignores --ignore-missing-imports --no-strict-optional --check-untyped-defs --disallow-incomplete-defs --disallow-untyped-defs --disallow-any-generics -p eth
-
Usage of
type: ignore
(silencing the type checker) is minimized and there's a reasonable explanation for its usage -
Expose the type hints in the same manner as was done in Enable discovery of type hints as per PEP561 eth-typing#10
Stretch goals
When this issue is done, stretch goals can be applied (and individually get funded) to tighten type support to qualify:
-
`mypy --strict --follow-imports=silent --ignore-missing-imports --no-strict-optional -p web3 -p ens
-
`mypy --strict --follow-imports=silent --ignore-missing-imports -p web3 -p ens