-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add type hints and expose them via PEP561 #1112
Comments
hey @pipermerriam thanks for the clear requirements - I'm going to get start on this now. ps. this is a fantastic initiative - as with anything we can to do minimise ambiguity. |
@johnsBeharry 👍 please get a pull request opened for this as soon as you have anything committed (as that helps us give feedback early). |
Just an update - took a while for me to get all the tests running so I could trace with |
Cleaning up stale issues; types have been implemented 🚢 |
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:
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.
web3._utils
modules (3762 sloc)web3.contract
module (1561 sloc)web3.middlewares
module (1307 slot)web3.providers
module (1212 slot)In order to incrementally enforce these in CI the
mypy
command should be added to thelint
section of thetox.ini
and can be incrementally updated to only look at the modules which have had their type hints added.Definition of done
This issue is done when the following criteria are met:
lint
environment in thetox.ini
fileIt needs to be:
Usage of
type: ignore
(silencing the type checker) is minimized and there's a reasonable explanation for its usageExpose 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
The text was updated successfully, but these errors were encountered: