Skip to content

Commit 6873d3c

Browse files
Fix computer type
1 parent 0b37ef6 commit 6873d3c

File tree

4 files changed

+28
-35
lines changed

4 files changed

+28
-35
lines changed

src/aiida/cmdline/groups/dynamic.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,7 @@ def list_options(self, entry_point: str) -> list[t.Callable[[FC], FC]]:
179179

180180
default = field_info.default_factory if field_info.default is PydanticUndefined else field_info.default
181181

182-
option_type = get_metadata(field_info, 'option_type', None)
183-
184-
if option_type is not None:
185-
field_type = option_type
186-
elif hasattr(field_info.annotation, '__args__'):
182+
if hasattr(field_info.annotation, '__args__'):
187183
# If the annotation has the ``__args__`` attribute it is an instance of a type from ``typing`` and
188184
# the real type can be gotten from the arguments. For example it could be ``typing.Union[str, None]``
189185
# calling ``typing.Union[str, None].__args__`` will return the tuple ``(str, NoneType)``. So to get

src/aiida/common/pydantic.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def MetadataField( # noqa: N802
3434
priority: int = 0,
3535
short_name: str | None = None,
3636
option_cls: t.Any | None = None,
37-
option_type: type | None = None,
3837
orm_class: type[Entity] | str | None = None,
3938
orm_to_model: t.Callable[[Entity, Path], t.Any] | None = None,
4039
model_to_orm: t.Callable[['BaseModel'], t.Any] | None = None,
@@ -66,9 +65,6 @@ class Model(BaseModel):
6665
:param priority: Used to order the list of all fields in the model. Ordering is done from small to large priority.
6766
:param short_name: Optional short name to use for an option on a command line interface.
6867
:param option_cls: The :class:`click.Option` class to use to construct the option.
69-
:param option_type: The type to use for the option on the command line interface. This is typically a built-in type
70-
such as ``str``, ``int``, ``float``, or ``bool``. If not specified, the type will be inferred from the type
71-
annotation of the field.
7268
:param orm_class: The class, or entry point name thereof, to which the field should be converted. If this field is
7369
defined, the value of this field should accept an integer which will automatically be converted to an instance
7470
of said ORM class using ``orm_class.collection.get(id={field_value})``. This is useful, for example, where a
@@ -90,7 +86,6 @@ class Model(BaseModel):
9086
('priority', priority),
9187
('short_name', short_name),
9288
('option_cls', option_cls),
93-
('option_type', option_type),
9489
('orm_class', orm_class),
9590
('orm_to_model', orm_to_model),
9691
('model_to_orm', model_to_orm),

src/aiida/orm/nodes/data/code/installed.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import pathlib
2020
from typing import cast
2121

22-
from pydantic import field_validator
23-
2422
from aiida.common import exceptions
2523
from aiida.common.lang import type_check
2624
from aiida.common.log import override_log_level
@@ -44,13 +42,12 @@ class InstalledCode(Code):
4442
class Model(AbstractCode.Model):
4543
"""Model describing required information to create an instance."""
4644

47-
computer: int = MetadataField(
45+
computer: str = MetadataField(
4846
title='Computer',
49-
description='The remote computer on which the executable resides.',
47+
description='The label of the remote computer on which the executable resides.',
5048
is_attribute=False,
51-
orm_to_model=lambda node, _: cast('InstalledCode', node).computer.pk,
52-
orm_class=Computer,
53-
option_type=str,
49+
orm_to_model=lambda node, _: cast('InstalledCode', node).computer.label,
50+
model_to_orm=lambda model: cast('InstalledCode.Model', model).load_computer(),
5451
short_name='-Y',
5552
priority=2,
5653
)
@@ -62,24 +59,16 @@ class Model(AbstractCode.Model):
6259
priority=1,
6360
)
6461

65-
@field_validator('computer', mode='before')
66-
@classmethod
67-
def validate_computer(cls, value: str | int) -> int:
68-
"""Validate computer input.
69-
70-
If provided a string (e.g., via CLI computer setup), try to load the computer and return its PK.
62+
def load_computer(self) -> Computer:
63+
"""Load the computer instance.
7164
72-
:param value: The input value, either a string or an integer
73-
:return: The PK of the computer
74-
:raises ValueError: If the computer does not exist
65+
:return: The computer instance.
66+
:raises ValueError: If the computer does not exist.
7567
"""
7668
from aiida.orm import load_computer
7769

78-
if isinstance(value, int):
79-
return value
80-
8170
try:
82-
return cast(int, load_computer(value).pk)
71+
return load_computer(self.computer)
8372
except exceptions.NotExistent as exception:
8473
raise ValueError(exception) from exception
8574

src/aiida/orm/nodes/node.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,17 +262,17 @@ class Model(Entity.Model):
262262
is_subscriptable=True,
263263
exclude_from_cli=True,
264264
)
265-
computer: Optional[int] = MetadataField(
265+
computer: Optional[str] = MetadataField(
266266
None,
267-
description='The PK of the computer',
267+
description='The label of the computer',
268268
is_attribute=False,
269-
orm_to_model=lambda node, _: cast('Node', node).computer.pk if cast('Node', node).computer else None,
270-
orm_class=Computer,
269+
orm_to_model=lambda node, _: cast('Node', node).computer.label if cast('Node', node).computer else None, # type: ignore[union-attr]
270+
model_to_orm=lambda model: cast('Node.Model', model).load_computer(),
271271
exclude_from_cli=True,
272272
exclude_to_orm=True,
273273
)
274274
user: int = MetadataField(
275-
default_factory=lambda: User.collection.get_default().pk,
275+
default_factory=lambda: User.collection.get_default().pk, # type: ignore[union-attr]
276276
description='The PK of the user who owns the node',
277277
is_attribute=False,
278278
orm_to_model=lambda node, _: cast('Node', node).user.pk,
@@ -293,6 +293,19 @@ class Model(Entity.Model):
293293
exclude_to_orm=True,
294294
)
295295

296+
def load_computer(self) -> Computer:
297+
"""Load the computer instance.
298+
299+
:return: The computer instance.
300+
:raises ValueError: If the computer does not exist.
301+
"""
302+
from aiida.orm import load_computer
303+
304+
try:
305+
return load_computer(self.computer)
306+
except exceptions.NotExistent as exception:
307+
raise ValueError(exception) from exception
308+
296309
def __init__(
297310
self,
298311
backend: Optional['StorageBackend'] = None,

0 commit comments

Comments
 (0)