Skip to content

Commit 936185b

Browse files
authored
Fix the pydantic model for RemoteData (aiidateam#6845)
The `__qb_fields__` has been removed and a `orm_to_model` function has been added that correctly returns the `remote_path`. Tests have been added to `test_models.py`.
1 parent 6cb2c71 commit 936185b

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

src/aiida/orm/nodes/data/remote/base.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
import logging
1414
import os
1515
from pathlib import Path
16+
from typing import Union
1617

1718
from aiida.common.pydantic import MetadataField
1819
from aiida.orm import AuthInfo
19-
from aiida.orm.fields import add_field
2020
from aiida.transports import Transport
2121

2222
from ..data import Data
@@ -33,25 +33,23 @@ class RemoteData(Data):
3333
"""
3434

3535
KEY_EXTRA_CLEANED = 'cleaned'
36-
__qb_fields__ = [
37-
add_field(
38-
'remote_path',
39-
dtype=str,
40-
),
41-
]
4236

4337
class Model(Data.Model):
44-
remote_path: str = MetadataField(description='Filepath on the remote computer.')
38+
remote_path: Union[str, None] = MetadataField(
39+
title='Remote path',
40+
description='Filepath on the remote computer.',
41+
orm_to_model=lambda node, _: node.get_remote_path(),
42+
)
4543

46-
def __init__(self, remote_path=None, **kwargs):
44+
def __init__(self, remote_path: Union[str, None] = None, **kwargs):
4745
super().__init__(**kwargs)
4846
if remote_path is not None:
4947
self.set_remote_path(remote_path)
5048

5149
def get_remote_path(self) -> str:
5250
return self.base.attributes.get('remote_path')
5351

54-
def set_remote_path(self, val):
52+
def set_remote_path(self, val: str):
5553
self.base.attributes.set('remote_path', val)
5654

5755
@property
@@ -233,7 +231,7 @@ def get_size_on_disk(
233231

234232
with authinfo.get_transport() as transport:
235233
if not transport.path_exists(str(full_path)):
236-
exc_message = f'The required remote path {full_path} on Computer <{computer_label}> ' 'does not exist.'
234+
exc_message = f'The required remote path {full_path} on Computer <{computer_label}> does not exist.'
237235
raise FileNotFoundError(exc_message)
238236

239237
if method not in ('du', 'stat'):

tests/orm/models/test_models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
Int,
2222
JsonableData,
2323
List,
24+
RemoteData,
2425
SinglefileData,
2526
Str,
2627
StructureData,
@@ -48,6 +49,7 @@
4849
SinglefileData,
4950
Str,
5051
StructureData,
52+
RemoteData,
5153
)
5254

5355

@@ -134,6 +136,8 @@ def required_arguments(request, default_user, aiida_localhost, tmp_path):
134136
return Str, {'value': 'string'}
135137
if request.param is StructureData:
136138
return StructureData, {'cell': [[1, 0, 0], [0, 1, 0], [0, 0, 1]]}
139+
if request.param is RemoteData:
140+
return RemoteData, {'remote_path': '/some/path', 'computer': aiida_localhost}
137141

138142
raise NotImplementedError()
139143

tests/orm/test_fields/fields_aiida.data.core.remote.RemoteData.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mtime: QbNumericField('mtime', dtype=typing.Optional[datetime.datetime], is_attr
1010
node_type: QbStrField('node_type', dtype=typing.Optional[str], is_attribute=False)
1111
pk: QbNumericField('pk', dtype=typing.Optional[int], is_attribute=False)
1212
process_type: QbStrField('process_type', dtype=typing.Optional[str], is_attribute=False)
13-
remote_path: QbStrField('remote_path', dtype=<class 'str'>, is_attribute=True)
13+
remote_path: QbStrField('remote_path', dtype=typing.Optional[str], is_attribute=True)
1414
repository_content: QbDictField('repository_content', dtype=typing.Optional[dict[str,
1515
bytes]], is_attribute=False)
1616
repository_metadata: QbDictField('repository_metadata', dtype=typing.Optional[typing.Dict[str,

0 commit comments

Comments
 (0)