-
Notifications
You must be signed in to change notification settings - Fork 275
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
Build: Initial mypy integration #1395
Conversation
98c6f43
to
116047a
Compare
rebased on develop |
The import is useful for mypy so it can check the types. Add a pylint disable just like json.py does in the same situation. Signed-off-by: Jussi Kukkonen <[email protected]>
Use ClassVar for extra protection, set default value to a string so type checking is ok with it Signed-off-by: Jussi Kukkonen <[email protected]>
Also define from_dict()/to_dict() as abstract: this helps mypy keep track of things. Rename derived argument *_dict in the derived classes to keep the linter happy. Signed-off-by: Jussi Kukkonen <[email protected]>
Signed-off-by: Jussi Kukkonen <[email protected]>
This allows mypy to track the argument types through the constructor calls. Signed-off-by: Jussi Kukkonen <[email protected]>
This is an initial setup: By default check only tuf/api/, and ignore securesystemslib imports. Change lint working directory to source root: This saves repeating a lot of {toxinidir} in the command lines. Signed-off-by: Jussi Kukkonen <[email protected]>
pylint on the legacy code is by far the slowest part of linting (to the extent that parallelizing the tox env itself doesn't really help): pylint can fortunately parallelize itself. Signed-off-by: Jussi Kukkonen <[email protected]>
Also mark the argument as Dict as we will pop() it. Signed-off-by: Jussi Kukkonen <[email protected]>
Without this mypy figures the dict is Dict[str, str] and then promptly fails when int value is inserted Signed-off-by: Jussi Kukkonen <[email protected]>
116047a
to
b643e5b
Compare
rebased on develop, added fix for an issue in new code |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think stricter options should be tried next? For example "disallow-untyped-defs" on api/metadata.py.
I see that now this results to errors coming from formats.py
and exceptions.py
.
Or maybe this is too strict and we can add type annotations to the coding guidelines for now?
|
||
if TYPE_CHECKING: | ||
# pylint: disable=cyclic-import | ||
from tuf.api.metadata import Metadata, Signed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So mypy recognizes that the quoted strings "Metadata" and "Signed" are the classes in this import?
Cool trick!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, AFAIK for mypy the type-names-as-string are just as valid.
We probably should start experimenting with stricter options: once it's all set up I imagine it's not hard to maintain... But probably the most valuable improvement would be to gradually start supporting securesystemslib modules. On the specific cases you mentioned: formats.py should go IMO: #1384. Fixing exceptions.py should be a few minutes job. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks. Might take a while getting used to inline type annotations, but should be worth it 🤞
Will be great to include ngclient in the lint test Tox environment, once it has merged.
Goal is to start using mypy to statically check our source code:
tuf/api
, and ignoring securesystemslib importsHow mypy is used after this PR:
tox -e lint
or justmypy
Fixes #1335