-
Notifications
You must be signed in to change notification settings - Fork 229
More typing for cmdline.params module #7009
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
More typing for cmdline.params module #7009
Conversation
15c22ce
to
3c14634
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #7009 +/- ##
==========================================
- Coverage 79.04% 79.03% -0.01%
==========================================
Files 566 566
Lines 43720 43798 +78
==========================================
+ Hits 34555 34612 +57
- Misses 9165 9186 +21 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
import click | ||
|
||
if t.TYPE_CHECKING: | ||
from click.decorators import FC |
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.
FC is a TypeVar (type parameter) defined here:
It's not clear whether it's better to import it or redefine it here (since this is a type-checking only import I think it is fine, but I don't know what is a best practice in these cases).
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.
I'd say import is fine. Better than to re-define!
return None | ||
def convert(self, value: t.Any, param: click.Parameter | None, ctx: click.Context | None) -> str: | ||
# NOTE: The return value of click.StringParamType.convert is typed as t.Any, | ||
# but from its implementation its clear that it returns a string. |
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.
def convert(self, value, param, ctx): | ||
newval = super().convert(value, param, ctx) | ||
|
||
# Note: Valid :py:class:`click.ParamType`s need to pass through None unchanged |
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.
This was perhaps true in click 7.x but doesn't seem to be true anymore; see docstring of ParamType.convert()
method:
And also the code here that calls the convert method only when the value is not None.
https://github.com/pallets/click/blob/2a0e3ba907927ade6951d5732b775f11b54cb766/src/click/types.py#L89
"""Container class that represents a collection of entries of a particular backend entity.""" | ||
|
||
ENTITY_CLASS: ClassVar[Type[EntityType]] # type: ignore[misc] | ||
ENTITY_CLASS: ClassVar[Type[EntityType]] |
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.
ignore not needed thanks to mypy upgrade 🎉
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.
If the PR passes strict mypy checking, chances are high that it also passes my review :3
import click | ||
|
||
if t.TYPE_CHECKING: | ||
from click.decorators import FC |
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.
I'd say import is fine. Better than to re-define!
if job_resource_cls is None: | ||
# Odd situation... | ||
return False | ||
return False # type: ignore[unreachable] |
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.
Can probably be removed in the future...
if t.TYPE_CHECKING: | ||
try: | ||
from typing import TypeAlias | ||
except ImportError: | ||
from typing_extensions import TypeAlias |
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.
Just a reminder that we can remove this once we drop py 3.9
T = TypeVar('T') | ||
|
||
|
||
def type_check(what: T, of_type: Any, msg: 'str | None' = None, allow_none: bool = False) -> 'T | None': |
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.
Any reason for the forward reference quotes here instead of adding from future import annotations
?
|
||
def test_fail_unreachable_url(): | ||
"""Test the parameter in case of a valid URL that cannot be reached.""" | ||
# TODO: Mock the request to make this faster |
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.
waiting for the PR hehe
pre-commit = [ | ||
'aiida-core[atomic_tools,rest,tests,tui]', | ||
'mypy~=1.17.0', | ||
'mypy~=1.18.0', |
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 that in a separate PR? Or fine to do it alongside this one? Not sure what is best practice here...
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.
I proclaim that because there was a no fallout from this upgrade it's okay to do it here :-)
I ran mypy in strict mode on the
aiida.cmdline.params
module and fixed all issues (mostly untyped functions). Then I enabled stricter settings for this module in pyproject.tomlExtra safety before the click 8.2 update (to which I've become paranoid again 😅 )
Also upgraded mypy to 1.18.1. Changelog promises big performance speedup in this release, and this is what I also see locally! 28s -> 15s without cache and 4.5s -> 0.5 with cache! 🎉
(also removed one
type: ignore
)Mypy==1.17.1
Mypy==1.18.1