-
Notifications
You must be signed in to change notification settings - Fork 192
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
Run typing on orm/{comments,computers}.py and orm/implementation/storage_backend.py #6704
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #6704 +/- ##
=======================================
Coverage 78.00% 78.00%
=======================================
Files 563 563
Lines 41766 41766
=======================================
Hits 32574 32574
Misses 9192 9192 ☔ View full report in Codecov by Sentry. |
cf1cf48
to
8d0fa45
Compare
@@ -54,7 +54,7 @@ def get_or_create(self, label: Optional[str] = None, **kwargs) -> Tuple[bool, 'C | |||
|
|||
def list_labels(self) -> List[str]: | |||
"""Return a list with all the labels of the computers in the DB.""" | |||
return self._backend.computers.list_names() | |||
return self._backend.computers.list_names() # type: ignore[attr-defined] |
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 am not sure why mypy complains here
@@ -224,7 +224,7 @@ def _mpirun_command_validator(self, mpirun_cmd: Union[List[str], Tuple[str, ...] | |||
"""Validates the mpirun_command variable. MUST be called after properly | |||
checking for a valid scheduler. | |||
""" | |||
if not isinstance(mpirun_cmd, (tuple, list)) or not all(isinstance(i, str) for i in mpirun_cmd): | |||
if not isinstance(mpirun_cmd, (tuple, list)) or not all(isinstance(i, str) for i in mpirun_cmd): # type: ignore[redundant-expr] |
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.
Mypy complains that the isinstance check should be always true since the mpirun_cmd is typed at list or tuple, but we obviously want to preserve the runtime check since we don't control all the callers.
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.
Or I think the better change would be we move the mpirun_cmd runtime check when set up the computer? But it seems out the scope of this PR.
@@ -456,7 +456,7 @@ def get_orm_entities(self, detailed: bool = False) -> dict: | |||
""" | |||
from aiida.orm import Comment, Computer, Group, Log, Node, QueryBuilder, User | |||
|
|||
data = {} | |||
data: dict[str, Any] = {} |
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.
Without this change I get these errors:
src/aiida/orm/implementation/storage_backend.py:465: error: Incompatible types in assignment (expression has type "list[Any]", target has type "int") [assignment]
src/aiida/orm/implementation/storage_backend.py:470: error: Incompatible types in assignment (expression has type "list[Any]", target has type "int") [assignment]
src/aiida/orm/implementation/storage_backend.py:478: error: Incompatible types in assignment (expression has type "list[Any]", target has type "int") [assignment]
src/aiida/orm/implementation/storage_backend.py:486: error: Incompatible types in assignment (expression has type "list[Any]", target has type "int") [assignment]
src/aiida/orm/implementation/storage_backend.py:491: error: Incompatible types in assignment (expression has type "list[Any]", target has type "int") [assignment]
I don't know if there is a better solution.
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 do the same.
@@ -18,7 +18,7 @@ | |||
|
|||
if TYPE_CHECKING: | |||
from aiida.orm import Node, User | |||
from aiida.orm.implementation import StorageBackend | |||
from aiida.orm.implementation import BackendComment, BackendNode, StorageBackend # noqa: F401 |
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 I don't import BackendComment
I get the following error:
src/aiida/orm/comments.py:63: error: Name "BackendComment" is not defined [name-defined]
From this code:
aiida-core/src/aiida/orm/comments.py
Line 63 in 199a027
class Comment(entities.Entity['BackendComment', CommentCollection]): |
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.
The noqa
is needed because ruff complains about an unused import. I think this is a bug in ruff, I'll file an issue.
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.
In seems a bit strange the type in the square of Entity is mixed, did you try entities.Entity['BackendComment', 'CommentCollection']
? I remember my basedpyright gave something that can not use string and class for typing at same time. But doesn't matter, let's use noqa for the moment.
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 look at the type annotation, I am wondering if it can be just have a generic type to hold both backendComment
and CommentCollection
?
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.
But is it even technically a type annotation? I don't know how the entities.Entity
is even supposed to work, since this is somehow specifying the parent class for Comment
.
I don't think I'd want to touch this in this PR in any case tbh.
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.
Fair, this typing annotation is quite confuse. But let's keep as it is. We can come back and check all noqa
. I'd say, the complex typing usually come from improper design, but it requires way more effort to touch some core parts of storage.
Then my review will not count ;p Just out of curious, the goal of the PR is to add more mypy checking to code base (I am totally in it, thanks! I was thinking maybe we can even have it as a project for this year's GSoC to find someone can working on it ) or you are targeting to bring some new features that may benefit from adding type? |
Haha, fair. I am mostly confused about this code:
Mypy complains about the missing |
Just adding type checking. These files just seemed like low-hanging fruit, they were already mostly typed! What I really don't like is that we have a list of ignored types in pre-commit config file. It's very easy to miss, and to mess with types in files that are not even checked. I think it would be better to have type: ignore comments at the top of the ignored files, which makes it super clear that they are not checked. EDIT: What originally prompted me was the other PR which add checking for the |
How you import the if typing.TYPE_CHECKING:
from aiida.xxx import BackendComment |
I agree, but it is not easy to make typing correct in python. No actually compiler will help us (yes, I am comparing it with Rust 🙈 ). But I am also trying to bring typing to engine part of the code base module by module (like this one #6641). |
Yes, that's what I am doing in this PR. |
8d0fa45
to
9bbfb06
Compare
9bbfb06
to
042e513
Compare
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 gave my go on this PR, but it would be nice that someone more familiar with orm can give it a look. I pin @edan-bainglass because your name is in the storage part of team responsibilities.
I am not sure if the typing changes here make sense, would be good to get reviewed by someone familiar with the orm implementation.
Depends on #6703