From 3e4f3ef3549fe00708e18bbbb4cb3a784a2d973e Mon Sep 17 00:00:00 2001 From: Jusong Yu Date: Fri, 22 Nov 2024 02:08:39 +0100 Subject: [PATCH 1/7] Bump mypy version to ~=1.13.0 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 3768fcb05c..6b3f630e1a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -211,7 +211,7 @@ notebook = [ ] pre-commit = [ 'aiida-core[atomic_tools,rest,tests,tui]', - 'mypy~=1.10.0', + 'mypy~=1.13.0', 'packaging~=23.0', 'pre-commit~=3.5', 'sqlalchemy[mypy]~=2.0', From b6800dc4902dd5391b3ae96e764cc0ffe67a7362 Mon Sep 17 00:00:00 2001 From: Jusong Yu Date: Fri, 22 Nov 2024 02:28:06 +0100 Subject: [PATCH 2/7] ignore --- src/aiida/common/lang.py | 2 +- src/aiida/manage/manager.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/aiida/common/lang.py b/src/aiida/common/lang.py index ec9fb45ddb..6a8f4d3d5b 100644 --- a/src/aiida/common/lang.py +++ b/src/aiida/common/lang.py @@ -96,4 +96,4 @@ def __init__(self, getter: Callable[[SelfType], ReturnType]) -> None: self.getter = getter def __get__(self, instance: Any, owner: SelfType) -> ReturnType: - return self.getter(owner) + return self.getter(owner) # type: ignore[arg-type] diff --git a/src/aiida/manage/manager.py b/src/aiida/manage/manager.py index 8621b324f4..651190454e 100644 --- a/src/aiida/manage/manager.py +++ b/src/aiida/manage/manager.py @@ -430,7 +430,7 @@ def create_runner(self, with_persistence: bool = True, **kwargs: Any) -> 'Runner if with_persistence and 'persister' not in settings: settings['persister'] = self.get_persister() - return runners.Runner(**settings) + return runners.Runner(**settings) # type: ignore[arg-type] def create_daemon_runner(self, loop: Optional['asyncio.AbstractEventLoop'] = None) -> 'Runner': """Create and return a new daemon runner. From f415f55377ed2decab4ec7d768425aaea6122ea0 Mon Sep 17 00:00:00 2001 From: Jusong Yu Date: Fri, 22 Nov 2024 16:33:59 +0100 Subject: [PATCH 3/7] Try fix --- src/aiida/engine/processes/calcjobs/calcjob.py | 2 +- src/aiida/engine/processes/workchains/restart.py | 2 +- src/aiida/orm/nodes/data/array/projection.py | 2 +- src/aiida/orm/nodes/data/list.py | 7 ++++--- src/aiida/orm/nodes/data/singlefile.py | 4 +--- src/aiida/plugins/utils.py | 4 ++-- src/aiida/tools/pytest_fixtures/daemon.py | 2 +- 7 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/aiida/engine/processes/calcjobs/calcjob.py b/src/aiida/engine/processes/calcjobs/calcjob.py index d5acfca5fc..8ced783a5f 100644 --- a/src/aiida/engine/processes/calcjobs/calcjob.py +++ b/src/aiida/engine/processes/calcjobs/calcjob.py @@ -1062,7 +1062,7 @@ def presubmit(self, folder: Folder) -> CalcInfo: def encoder(obj): if dataclasses.is_dataclass(obj): - return dataclasses.asdict(obj) + return dataclasses.asdict(obj) # type: ignore[arg-type] raise TypeError(f' {obj!r} is not JSON serializable') subfolder = folder.get_subfolder('.aiida', create=True) diff --git a/src/aiida/engine/processes/workchains/restart.py b/src/aiida/engine/processes/workchains/restart.py index ad5cd8a181..34544704f2 100644 --- a/src/aiida/engine/processes/workchains/restart.py +++ b/src/aiida/engine/processes/workchains/restart.py @@ -29,7 +29,7 @@ def validate_handler_overrides( - process_class: 'BaseRestartWorkChain', handler_overrides: Optional[orm.Dict], ctx: 'PortNamespace' + process_class: type['BaseRestartWorkChain'], handler_overrides: Optional[orm.Dict], ctx: 'PortNamespace' ) -> Optional[str]: """Validator for the ``handler_overrides`` input port of the ``BaseRestartWorkChain``. diff --git a/src/aiida/orm/nodes/data/array/projection.py b/src/aiida/orm/nodes/data/array/projection.py index e58442d9c5..881ac727c2 100644 --- a/src/aiida/orm/nodes/data/array/projection.py +++ b/src/aiida/orm/nodes/data/array/projection.py @@ -278,7 +278,7 @@ def array_list_checker(array_list, array_name, orb_length): raise exceptions.ValidationError('Tags must set a list of strings') self.base.attributes.set('tags', tags) - def set_orbitals(self, **kwargs): + def set_orbitals(self, **kwargs): # type: ignore[override] """This method is inherited from OrbitalData, but is blocked here. If used will raise a NotImplementedError """ diff --git a/src/aiida/orm/nodes/data/list.py b/src/aiida/orm/nodes/data/list.py index 9fdd6ec866..44a14af0ca 100644 --- a/src/aiida/orm/nodes/data/list.py +++ b/src/aiida/orm/nodes/data/list.py @@ -9,6 +9,7 @@ """`Data` sub class to represent a list.""" from collections.abc import MutableSequence +from typing import Any from .base import to_aiida_type from .data import Data @@ -81,15 +82,15 @@ def remove(self, value): self.set_list(data) return item - def pop(self, **kwargs): + def pop(self, index: int = 0) -> Any: """Remove and return item at index (default last).""" data = self.get_list() - item = data.pop(**kwargs) + item = data.pop(index) if not self._using_list_reference(): self.set_list(data) return item - def index(self, value): + def index(self, value: Any, start: int = 0, stop: int = 0) -> int: """Return first index of value..""" return self.get_list().index(value) diff --git a/src/aiida/orm/nodes/data/singlefile.py b/src/aiida/orm/nodes/data/singlefile.py index c0f3797f24..8faefc8cb4 100644 --- a/src/aiida/orm/nodes/data/singlefile.py +++ b/src/aiida/orm/nodes/data/singlefile.py @@ -71,9 +71,7 @@ def open(self, path: FilePath, mode: t.Literal['rb']) -> t.Iterator[t.BinaryIO]: @t.overload @contextlib.contextmanager - def open( # type: ignore[overload-overlap] - self, path: None = None, mode: t.Literal['r'] = ... - ) -> t.Iterator[t.TextIO]: ... + def open(self, path: None = None, mode: t.Literal['r'] = ...) -> t.Iterator[t.TextIO]: ... @t.overload @contextlib.contextmanager diff --git a/src/aiida/plugins/utils.py b/src/aiida/plugins/utils.py index c284b25912..3c18918204 100644 --- a/src/aiida/plugins/utils.py +++ b/src/aiida/plugins/utils.py @@ -13,7 +13,7 @@ import typing as t from importlib import import_module from logging import Logger -from types import FunctionType +from types import FunctionType, ModuleType from aiida.common import AIIDA_LOGGER from aiida.common.exceptions import EntryPointError @@ -38,7 +38,7 @@ def __init__(self): def logger(self) -> Logger: return self._logger - def get_version_info(self, plugin: str | type) -> dict[t.Any, dict[t.Any, t.Any]]: + def get_version_info(self, plugin: str | ModuleType | FunctionType | t.Any) -> dict[t.Any, dict[t.Any, t.Any]]: """Get the version information for a given plugin. .. note:: diff --git a/src/aiida/tools/pytest_fixtures/daemon.py b/src/aiida/tools/pytest_fixtures/daemon.py index 74e3620193..379b08e0ce 100644 --- a/src/aiida/tools/pytest_fixtures/daemon.py +++ b/src/aiida/tools/pytest_fixtures/daemon.py @@ -116,7 +116,7 @@ def test(submit_and_await): from aiida.engine import ProcessState def factory( - submittable: 'Process' | 'ProcessBuilder' | 'ProcessNode', + submittable: type[Process] | 'ProcessBuilder' | 'ProcessNode' | t.Any, state: ProcessState = ProcessState.FINISHED, timeout: int = 20, **kwargs, From 0dabadec7041fdab21ba90c66616fcfb6c65f61c Mon Sep 17 00:00:00 2001 From: Jusong Yu Date: Fri, 22 Nov 2024 16:55:46 +0100 Subject: [PATCH 4/7] Pydantic version caused mypy error --- src/aiida/common/pydantic.py | 4 ++-- tests/cmdline/groups/test_dynamic.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/aiida/common/pydantic.py b/src/aiida/common/pydantic.py index 633e83428d..4973a44bdf 100644 --- a/src/aiida/common/pydantic.py +++ b/src/aiida/common/pydantic.py @@ -14,7 +14,7 @@ def MetadataField( # noqa: N802 short_name: str | None = None, option_cls: t.Any | None = None, **kwargs, -): +) -> t.Any | None: """Return a :class:`pydantic.fields.Field` instance with additional metadata. .. code-block:: python @@ -41,7 +41,7 @@ class Model(BaseModel): field_info = Field(default, **kwargs) for key, value in (('priority', priority), ('short_name', short_name), ('option_cls', option_cls)): - if value is not None: + if value is not None and field_info is not None: field_info.metadata.append({key: value}) return field_info diff --git a/tests/cmdline/groups/test_dynamic.py b/tests/cmdline/groups/test_dynamic.py index 3c741b8fe2..48c18f0941 100644 --- a/tests/cmdline/groups/test_dynamic.py +++ b/tests/cmdline/groups/test_dynamic.py @@ -17,7 +17,7 @@ class Model(BaseModel): union_type: t.Union[int, float] = Field(title='Union type') without_default: str = Field(title='Without default') with_default: str = Field(title='With default', default='default') - with_default_factory: str = Field(title='With default factory', default_factory=lambda: True) + with_default_factory: str = Field(title='With default factory', default_factory=lambda: True) # type: ignore[assignment] def test_list_options(entry_points): From ac5473e0ceff03b14c4f9f031f327bf88035cf03 Mon Sep 17 00:00:00 2001 From: Jusong Yu Date: Fri, 22 Nov 2024 16:58:37 +0100 Subject: [PATCH 5/7] amend May fix doc failed build --- src/aiida/common/pydantic.py | 2 +- src/aiida/orm/nodes/data/list.py | 2 +- src/aiida/plugins/utils.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/aiida/common/pydantic.py b/src/aiida/common/pydantic.py index 4973a44bdf..2fa3d2bd68 100644 --- a/src/aiida/common/pydantic.py +++ b/src/aiida/common/pydantic.py @@ -14,7 +14,7 @@ def MetadataField( # noqa: N802 short_name: str | None = None, option_cls: t.Any | None = None, **kwargs, -) -> t.Any | None: +): """Return a :class:`pydantic.fields.Field` instance with additional metadata. .. code-block:: python diff --git a/src/aiida/orm/nodes/data/list.py b/src/aiida/orm/nodes/data/list.py index 44a14af0ca..d2c0857b35 100644 --- a/src/aiida/orm/nodes/data/list.py +++ b/src/aiida/orm/nodes/data/list.py @@ -82,7 +82,7 @@ def remove(self, value): self.set_list(data) return item - def pop(self, index: int = 0) -> Any: + def pop(self, index: int = -1) -> Any: """Remove and return item at index (default last).""" data = self.get_list() item = data.pop(index) diff --git a/src/aiida/plugins/utils.py b/src/aiida/plugins/utils.py index 3c18918204..7d1e16a363 100644 --- a/src/aiida/plugins/utils.py +++ b/src/aiida/plugins/utils.py @@ -13,7 +13,7 @@ import typing as t from importlib import import_module from logging import Logger -from types import FunctionType, ModuleType +from types import FunctionType from aiida.common import AIIDA_LOGGER from aiida.common.exceptions import EntryPointError @@ -38,7 +38,7 @@ def __init__(self): def logger(self) -> Logger: return self._logger - def get_version_info(self, plugin: str | ModuleType | FunctionType | t.Any) -> dict[t.Any, dict[t.Any, t.Any]]: + def get_version_info(self, plugin: str | t.Any) -> dict[t.Any, dict[t.Any, t.Any]]: """Get the version information for a given plugin. .. note:: From 7b9e02eedc1f000abbdd646bd155a2509bb3c13e Mon Sep 17 00:00:00 2001 From: Jusong Yu Date: Sun, 24 Nov 2024 00:39:02 +0100 Subject: [PATCH 6/7] rd --- src/aiida/orm/nodes/data/list.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/aiida/orm/nodes/data/list.py b/src/aiida/orm/nodes/data/list.py index d2c0857b35..fc39dd1acd 100644 --- a/src/aiida/orm/nodes/data/list.py +++ b/src/aiida/orm/nodes/data/list.py @@ -9,7 +9,6 @@ """`Data` sub class to represent a list.""" from collections.abc import MutableSequence -from typing import Any from .base import to_aiida_type from .data import Data @@ -82,15 +81,15 @@ def remove(self, value): self.set_list(data) return item - def pop(self, index: int = -1) -> Any: + def pop(self, **kwargs): # type: ignore[override] """Remove and return item at index (default last).""" data = self.get_list() - item = data.pop(index) + item = data.pop(**kwargs) if not self._using_list_reference(): self.set_list(data) return item - def index(self, value: Any, start: int = 0, stop: int = 0) -> int: + def index(self, value): # type: ignore[override] """Return first index of value..""" return self.get_list().index(value) From 0b6f37f04026e8f134018162ec884e055a87c1d5 Mon Sep 17 00:00:00 2001 From: Jusong Yu Date: Sun, 24 Nov 2024 00:42:52 +0100 Subject: [PATCH 7/7] Follow the abc contract of MutableSequence for orm.List --- src/aiida/orm/nodes/data/list.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/aiida/orm/nodes/data/list.py b/src/aiida/orm/nodes/data/list.py index fc39dd1acd..d2c0857b35 100644 --- a/src/aiida/orm/nodes/data/list.py +++ b/src/aiida/orm/nodes/data/list.py @@ -9,6 +9,7 @@ """`Data` sub class to represent a list.""" from collections.abc import MutableSequence +from typing import Any from .base import to_aiida_type from .data import Data @@ -81,15 +82,15 @@ def remove(self, value): self.set_list(data) return item - def pop(self, **kwargs): # type: ignore[override] + def pop(self, index: int = -1) -> Any: """Remove and return item at index (default last).""" data = self.get_list() - item = data.pop(**kwargs) + item = data.pop(index) if not self._using_list_reference(): self.set_list(data) return item - def index(self, value): # type: ignore[override] + def index(self, value: Any, start: int = 0, stop: int = 0) -> int: """Return first index of value..""" return self.get_list().index(value)