Skip to content

Commit b0e1a22

Browse files
Refactor Model classes out of Entity classes
1 parent b2b97d6 commit b0e1a22

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1064
-935
lines changed

docs/source/nitpick-exceptions

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ py:meth tempfile.TemporaryDirectory
3333
### AiiDA
3434
py:class ReturnType
3535
py:class SelfType
36-
py:class CollectionType
36+
py:class EntityCollectionType
3737
py:class EntityType
38+
py:class EntityModelType
3839
py:class EntityClsType
3940
py:class ProjectType
4041
py:class BackendEntityType
@@ -51,8 +52,9 @@ py:class UserDefinedType
5152
py:class LinkAnnotateType
5253
py:obj ReturnType
5354
py:obj SelfType
54-
py:obj CollectionType
55+
py:obj EntityCollectionType
5556
py:obj EntityType
57+
py:obj EntityModelType
5658
py:obj BackendEntityType
5759
py:obj BackendNodeType
5860
py:obj TransactionType
@@ -62,23 +64,19 @@ py:class aiida.common.ReturnType
6264
py:class aiida.common.SelfType
6365
py:class aiida.common.lang.MethodType
6466
py:class aiida.cmdline.params._shims.C
65-
py:class aiida.orm.entitites.CollectionType
66-
py:class aiida.orm.entitites.EntityType
67-
py:class aiida.orm.entitites.BackendEntityType
6867
py:class aiida.orm.groups.SelfType
69-
py:class aiida.orm.implementation.entitites.EntityType
7068
py:class aiida.engine.processes.functions.FunctionType
7169
py:class aiida.engine.processes.workchains.workchain.MethodType
7270
py:class aiida.orm.entities.EntityInputModel
73-
py:class aiida.orm.entities.EntityModelType
7471
py:class aiida.orm.entities.EntityType
72+
py:class aiida.orm.entities.EntityModelType
7573
py:class aiida.orm.entities.BackendEntityType
76-
py:class aiida.orm.entities.CollectionType
74+
py:class aiida.orm.entities.EntityCollectionType
7775
py:class aiida.orm.implementation.nodes.BackendNodeType
7876
py:class aiida.orm.implementation.storage_backend.TransactionType
7977
py:class aiida.orm.implementation.entities.EntityType
8078
py:class aiida.orm.nodes.data.enum.EnumType
81-
py:class aiida.orm.nodes.NodeType
79+
py:class aiida.orm.nodes.node.NodeType
8280
py:class aiida.storage.psql_dos.orm.ModelType
8381
py:class aiida.storage.psql_dos.orm.SelfType
8482
py:class aiida.storage.psql_dos.orm.entities.SelfType
@@ -91,18 +89,15 @@ py:class aiida.common.lang.ReturnType
9189
py:obj aiida.common.ReturnType
9290
py:obj aiida.common.SelfType
9391
py:obj aiida.common.lang.ReturnType
94-
py:obj aiida.orm.entitites.CollectionType
95-
py:obj aiida.orm.entitites.EntityType
96-
py:obj aiida.orm.entitites.BackendEntityType
9792
py:obj aiida.orm.groups.SelfType
98-
py:obj aiida.orm.entities.CollectionType
93+
py:obj aiida.orm.entities.EntityCollectionType
9994
py:obj aiida.orm.entities.BackendEntityType
10095
py:obj aiida.orm.entities.EntityType
96+
py:obj aiida.orm.entities.EntityModelType
10197
py:obj aiida.orm.implementation.entities.EntityType
10298
py:obj aiida.orm.implementation.nodes.BackendNodeType
10399
py:obj aiida.orm.implementation.storage_backend.TransactionType
104100
py:obj aiida.orm.nodes.node.NodeType
105-
py:obj aiida.orm.nodes.NodeType
106101
py:obj aiida.storage.psql_dos.orm.ModelType
107102
py:obj aiida.storage.psql_dos.orm.SelfType
108103
py:obj aiida.storage.psql_dos.orm.entities.ModelType

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ select = [
479479
# Mark some classes as generic, per https://docs.astral.sh/ruff/settings/#lint_pyflakes_extend-generics
480480
# Needed due to https://github.com/astral-sh/ruff/issues/9298
481481
[tool.ruff.lint.pyflakes]
482-
extend-generics = ["aiida.orm.entities.Entity", "aiida.orm.entities.Collection"]
482+
extend-generics = ["aiida.orm.entities.Entity", "aiida.orm.entities.EntityCollection", "aiida.orm.entities.EntityModel"]
483483

484484
[tool.tox]
485485
legacy_tox_ini = """

src/aiida/common/typing.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
else:
2020
from typing_extensions import Self
2121

22-
__all__ = ('FilePath', 'Self')
22+
if sys.version_info >= (3, 10):
23+
from typing import TypeAlias
24+
else:
25+
from typing_extensions import TypeAlias
26+
27+
__all__ = ('FilePath', 'Self', 'TypeAlias')
2328

2429
FilePath = Union[str, pathlib.PurePath]

src/aiida/orm/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@
4545
'CifData',
4646
'Code',
4747
'CodeEntityLoader',
48-
'Collection',
4948
'Comment',
5049
'Computer',
5150
'ComputerEntityLoader',
5251
'ContainerizedCode',
5352
'Data',
5453
'Dict',
5554
'Entity',
55+
'EntityCollection',
5656
'EntityExtras',
5757
'EntityTypes',
5858
'EnumData',

src/aiida/orm/authinfos.py

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from aiida.common import exceptions
1616
from aiida.common.pydantic import MetadataField
17+
from aiida.common.typing import TypeAlias
1718
from aiida.manage import get_manager
1819
from aiida.plugins import TransportFactory
1920

@@ -29,7 +30,7 @@
2930
__all__ = ('AuthInfo',)
3031

3132

32-
class AuthInfoCollection(entities.Collection['AuthInfo']):
33+
class AuthInfoCollection(entities.EntityCollection['AuthInfo']):
3334
"""The collection of `AuthInfo` entries."""
3435

3536
@staticmethod
@@ -44,40 +45,44 @@ def delete(self, pk: int) -> None:
4445
self._backend.authinfos.delete(pk)
4546

4647

48+
class AuthInfoModel(entities.Entity.Model):
49+
computer: int = MetadataField(
50+
description='The PK of the computer',
51+
is_attribute=False,
52+
orm_class=Computer,
53+
orm_to_model=lambda auth_info: cast(AuthInfo, auth_info).computer.pk,
54+
)
55+
user: int = MetadataField(
56+
description='The PK of the user',
57+
is_attribute=False,
58+
orm_class=User,
59+
orm_to_model=lambda auth_info: cast(AuthInfo, auth_info).user.pk,
60+
)
61+
enabled: bool = MetadataField(
62+
True,
63+
description='Whether the instance is enabled',
64+
is_attribute=False,
65+
)
66+
auth_params: Dict[str, Any] = MetadataField(
67+
default_factory=dict,
68+
description='Dictionary of authentication parameters',
69+
is_attribute=False,
70+
)
71+
metadata: Dict[str, Any] = MetadataField(
72+
default_factory=dict,
73+
description='Dictionary of metadata',
74+
is_attribute=False,
75+
)
76+
77+
4778
class AuthInfo(entities.Entity['BackendAuthInfo', AuthInfoCollection]):
4879
"""ORM class that models the authorization information that allows a `User` to connect to a `Computer`."""
4980

81+
Model: TypeAlias = AuthInfoModel
82+
5083
_CLS_COLLECTION = AuthInfoCollection
51-
PROPERTY_WORKDIR = 'workdir'
5284

53-
class Model(entities.Entity.Model):
54-
computer: int = MetadataField(
55-
description='The PK of the computer',
56-
is_attribute=False,
57-
orm_class=Computer,
58-
orm_to_model=lambda auth_info: cast(AuthInfo, auth_info).computer.pk,
59-
)
60-
user: int = MetadataField(
61-
description='The PK of the user',
62-
is_attribute=False,
63-
orm_class=User,
64-
orm_to_model=lambda auth_info: cast(AuthInfo, auth_info).user.pk,
65-
)
66-
enabled: bool = MetadataField(
67-
True,
68-
description='Whether the instance is enabled',
69-
is_attribute=False,
70-
)
71-
auth_params: Dict[str, Any] = MetadataField(
72-
default_factory=dict,
73-
description='Dictionary of authentication parameters',
74-
is_attribute=False,
75-
)
76-
metadata: Dict[str, Any] = MetadataField(
77-
default_factory=dict,
78-
description='Dictionary of metadata',
79-
is_attribute=False,
80-
)
85+
PROPERTY_WORKDIR = 'workdir'
8186

8287
def __init__(
8388
self,

src/aiida/orm/comments.py

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from uuid import UUID
1616

1717
from aiida.common.pydantic import MetadataField
18+
from aiida.common.typing import TypeAlias
1819
from aiida.manage import get_manager
1920

2021
from . import entities
@@ -28,7 +29,7 @@
2829
__all__ = ('Comment',)
2930

3031

31-
class CommentCollection(entities.Collection['Comment']):
32+
class CommentCollection(entities.EntityCollection['Comment']):
3233
"""The collection of Comment entries."""
3334

3435
@staticmethod
@@ -65,43 +66,46 @@ def delete_many(self, filters: dict) -> List[int]:
6566
return self._backend.comments.delete_many(filters)
6667

6768

69+
class CommentModel(entities.Entity.Model):
70+
uuid: UUID = MetadataField(
71+
description='The UUID of the comment',
72+
is_attribute=False,
73+
exclude_to_orm=True,
74+
)
75+
ctime: datetime = MetadataField(
76+
description='Creation time of the comment',
77+
is_attribute=False,
78+
exclude_to_orm=True,
79+
)
80+
mtime: datetime = MetadataField(
81+
description='Modified time of the comment',
82+
is_attribute=False,
83+
exclude_to_orm=True,
84+
)
85+
node: int = MetadataField(
86+
description='Node PK that the comment is attached to',
87+
is_attribute=False,
88+
orm_class='core.node',
89+
orm_to_model=lambda comment: cast(Comment, comment).node.pk,
90+
)
91+
user: int = MetadataField(
92+
description='User PK that created the comment',
93+
is_attribute=False,
94+
orm_class='core.user',
95+
orm_to_model=lambda comment: cast(Comment, comment).user.pk,
96+
)
97+
content: str = MetadataField(
98+
description='Content of the comment',
99+
is_attribute=False,
100+
)
101+
102+
68103
class Comment(entities.Entity['BackendComment', CommentCollection]):
69104
"""Base class to map a DbComment that represents a comment attached to a certain Node."""
70105

71-
_CLS_COLLECTION = CommentCollection
106+
Model: TypeAlias = CommentModel
72107

73-
class Model(entities.Entity.Model):
74-
uuid: UUID = MetadataField(
75-
description='The UUID of the comment',
76-
is_attribute=False,
77-
exclude_to_orm=True,
78-
)
79-
ctime: datetime = MetadataField(
80-
description='Creation time of the comment',
81-
is_attribute=False,
82-
exclude_to_orm=True,
83-
)
84-
mtime: datetime = MetadataField(
85-
description='Modified time of the comment',
86-
is_attribute=False,
87-
exclude_to_orm=True,
88-
)
89-
node: int = MetadataField(
90-
description='Node PK that the comment is attached to',
91-
is_attribute=False,
92-
orm_class='core.node',
93-
orm_to_model=lambda comment: cast(Comment, comment).node.pk,
94-
)
95-
user: int = MetadataField(
96-
description='User PK that created the comment',
97-
is_attribute=False,
98-
orm_class='core.user',
99-
orm_to_model=lambda comment: cast(Comment, comment).user.pk,
100-
)
101-
content: str = MetadataField(
102-
description='Content of the comment',
103-
is_attribute=False,
104-
)
108+
_CLS_COLLECTION = CommentCollection
105109

106110
def __init__(
107111
self, node: 'Node', user: 'User', content: Optional[str] = None, backend: Optional['StorageBackend'] = None

src/aiida/orm/computers.py

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from aiida.common import exceptions
1818
from aiida.common.log import AIIDA_LOGGER, AiidaLoggerType
1919
from aiida.common.pydantic import MetadataField
20+
from aiida.common.typing import TypeAlias
2021
from aiida.manage import get_manager
2122
from aiida.plugins import SchedulerFactory, TransportFactory
2223

@@ -31,7 +32,7 @@
3132
__all__ = ('Computer',)
3233

3334

34-
class ComputerCollection(entities.Collection['Computer']):
35+
class ComputerCollection(entities.EntityCollection['Computer']):
3536
"""The collection of Computer entries."""
3637

3738
@staticmethod
@@ -64,9 +65,45 @@ def delete(self, pk: int) -> None:
6465
return self._backend.computers.delete(pk)
6566

6667

68+
class ComputerModel(entities.Entity.Model):
69+
uuid: UUID = MetadataField(
70+
description='The UUID of the computer',
71+
is_attribute=False,
72+
exclude_to_orm=True,
73+
)
74+
label: str = MetadataField(
75+
description='Label for the computer',
76+
is_attribute=False,
77+
)
78+
description: str = MetadataField(
79+
'',
80+
description='Description of the computer',
81+
is_attribute=False,
82+
)
83+
hostname: str = MetadataField(
84+
description='Hostname of the computer',
85+
is_attribute=False,
86+
)
87+
transport_type: str = MetadataField(
88+
description='Transport type of the computer',
89+
is_attribute=False,
90+
)
91+
scheduler_type: str = MetadataField(
92+
description='Scheduler type of the computer',
93+
is_attribute=False,
94+
)
95+
metadata: Dict[str, Any] = MetadataField(
96+
default_factory=dict,
97+
description='Metadata of the computer',
98+
is_attribute=False,
99+
)
100+
101+
67102
class Computer(entities.Entity['BackendComputer', ComputerCollection]):
68103
"""Computer entity."""
69104

105+
Model: TypeAlias = ComputerModel
106+
70107
_logger = AIIDA_LOGGER.getChild('orm.computers')
71108

72109
PROPERTY_MINIMUM_SCHEDULER_POLL_INTERVAL = 'minimum_scheduler_poll_interval'
@@ -76,39 +113,6 @@ class Computer(entities.Entity['BackendComputer', ComputerCollection]):
76113

77114
_CLS_COLLECTION = ComputerCollection
78115

79-
class Model(entities.Entity.Model):
80-
uuid: UUID = MetadataField(
81-
description='The UUID of the computer',
82-
is_attribute=False,
83-
exclude_to_orm=True,
84-
)
85-
label: str = MetadataField(
86-
description='Label for the computer',
87-
is_attribute=False,
88-
)
89-
description: str = MetadataField(
90-
'',
91-
description='Description of the computer',
92-
is_attribute=False,
93-
)
94-
hostname: str = MetadataField(
95-
description='Hostname of the computer',
96-
is_attribute=False,
97-
)
98-
transport_type: str = MetadataField(
99-
description='Transport type of the computer',
100-
is_attribute=False,
101-
)
102-
scheduler_type: str = MetadataField(
103-
description='Scheduler type of the computer',
104-
is_attribute=False,
105-
)
106-
metadata: Dict[str, Any] = MetadataField(
107-
default_factory=dict,
108-
description='Metadata of the computer',
109-
is_attribute=False,
110-
)
111-
112116
def __init__(
113117
self,
114118
label: Optional[str] = None,

0 commit comments

Comments
 (0)