Skip to content

Commit 496e8e4

Browse files
committed
Reject anchor entries from a foreign AiiDA profile
A run file's `profile` field was an unconstrained string, so an entry written under a different profile than the one `koopmans status`/ `attach` load would resolve as if it belonged here. - Type `AnchorEntry.profile` as `Literal[PROFILE_NAME]` (mypy needs a `# type: ignore[valid-type]`: it doesn't implement PEP 586's Final-variable-in-Literal support, python/mypy#10403). pydantic itself now rejects a foreign profile at validation, naming both the value found and the one required, through the same error path the rest of AnchorEntry's validation already uses. - Mark `PROFILE_NAME` `Final` so the Literal reference is valid.
1 parent 690d204 commit 496e8e4

3 files changed

Lines changed: 42 additions & 3 deletions

File tree

‎src/koopmans/aiida/anchor.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
import tempfile
1919
import time
2020
from pathlib import Path
21-
from typing import NamedTuple
21+
from typing import Literal, NamedTuple
2222

2323
import yaml
2424
from pydantic import Field, RootModel, ValidationError
2525

26+
from koopmans.aiida.setup.profile import PROFILE_NAME
2627
from koopmans.base import BaseModel
2728

2829
__all__ = [
@@ -42,7 +43,13 @@ class AnchorEntry(BaseModel):
4243
uuid: str = Field(description="the submitted process's AiiDA node UUID")
4344
pk: int = Field(description="the submitted process's AiiDA node pk")
4445
input: str = Field(description="the input file's name, relative to the anchor file")
45-
profile: str = Field(description="the AiiDA profile the process was submitted under")
46+
# mypy does not support a Final variable inside Literal[...] (PEP 586
47+
# permits it; mypy has not implemented it — python/mypy#10403), hence
48+
# the ignore. Referencing PROFILE_NAME rather than a copied string
49+
# keeps the allowed value in one place.
50+
profile: Literal[PROFILE_NAME] = Field( # type: ignore[valid-type]
51+
description="the AiiDA profile the process was submitted under"
52+
)
4653
submitted: str = Field(description="an ISO-8601 timestamp of the submission")
4754

4855

‎src/koopmans/aiida/setup/profile.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
import atexit
1010
import logging
1111
from pathlib import Path
12+
from typing import Final
1213

1314
import click
1415

1516
logger = logging.getLogger(__name__)
1617

17-
PROFILE_NAME = "koopmans"
18+
PROFILE_NAME: Final = "koopmans"
1819

1920
_CLOSE_HOOK_REGISTERED = False
2021

‎tests/test_cli_submit_status.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,37 @@ def test_a_deleted_node_is_a_clean_error(
301301
assert node_uuid in result.output
302302
assert "deleted" in result.output
303303

304+
def test_an_entry_for_a_foreign_profile_is_rejected(
305+
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
306+
) -> None:
307+
"""A run file naming a different profile than this install's own is refused.
308+
309+
``AnchorEntry.profile`` is typed ``Literal[PROFILE_NAME]``, so
310+
pydantic itself rejects a foreign value at validation — there is
311+
no separate guard to test, only that its error reaches the user
312+
naming both the value found and the one required.
313+
"""
314+
anchor_path = tmp_path / "si.run.yaml"
315+
anchor_path.write_text(
316+
yaml.safe_dump(
317+
[
318+
{
319+
"uuid": "some-uuid",
320+
"pk": 1,
321+
"input": "si.yaml",
322+
"profile": "someone-elses-profile",
323+
"submitted": "2026-08-11T12:00:00+00:00",
324+
}
325+
]
326+
)
327+
)
328+
329+
result = CliRunner().invoke(cli, ["status", str(anchor_path)])
330+
331+
assert result.exit_code != 0
332+
assert "someone-elses-profile" in result.output
333+
assert PROFILE_NAME in result.output
334+
304335
def test_no_target_and_no_run_file_is_a_clean_error(
305336
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
306337
) -> None:

0 commit comments

Comments
 (0)