Skip to content

Commit 9ea0d98

Browse files
committed
Rename to NamePlaceholder
1 parent 18688f0 commit 9ea0d98

5 files changed

Lines changed: 31 additions & 28 deletions

File tree

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
from .mixin import BaseElement
77

88

9-
class ElementReference(BaseModel):
10-
"""A pydantic model that represents a reference to a named element.
9+
class PlaceholderName(BaseModel):
10+
"""Represents a reference to a named element.
11+
12+
This placeholder is replaced with a physically distinct
13+
element during beamline expansion.
1114
1215
This class behaves like a string (via __str__ and __eq__) but stores
1316
a true reference to the actual element object once it's resolved.
@@ -19,7 +22,7 @@ class ElementReference(BaseModel):
1922
element: A reference to the resolved element object (None until resolved)
2023
2124
Example:
22-
>>> ref = ElementReference(name="drift1")
25+
>>> ref = PlaceholderName(name="drift1")
2326
>>> ref.name
2427
'drift1'
2528
>>> str(ref)
@@ -65,7 +68,7 @@ def __eq__(self, other: object) -> bool:
6568
"""Enable string comparison."""
6669
if isinstance(other, str):
6770
return self.name == other
68-
if isinstance(other, ElementReference):
71+
if isinstance(other, PlaceholderName):
6972
return self.name == other.name and self.element is other.element
7073
return False
7174

@@ -78,6 +81,6 @@ def is_resolved(self) -> bool:
7881
return self.element is not None
7982

8083
def __repr__(self) -> str:
81-
"""Return a representation of the ElementReference."""
84+
"""Return a representation of the PlaceholderName."""
8285
resolved = "resolved" if self.is_resolved() else "unresolved"
83-
return f"ElementReference('{self.name}', {resolved})"
86+
return f"PlaceholderName('{self.name}', {resolved})"

src/pals/kinds/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from .CrabCavity import CrabCavity # noqa: F401
1212
from .Drift import Drift # noqa: F401
1313
from .EGun import EGun # noqa: F401
14-
from .ElementReference import ElementReference # noqa: F401
1514
from .Feedback import Feedback # noqa: F401
1615
from .Fiducial import Fiducial # noqa: F401
1716
from .FloorShift import FloorShift # noqa: F401
@@ -27,6 +26,7 @@
2726
from .NullEle import NullEle # noqa: F401
2827
from .Octupole import Octupole # noqa: F401
2928
from .Patch import Patch # noqa: F401
29+
from .PlaceholderName import PlaceholderName # noqa: F401
3030
from .Quadrupole import Quadrupole # noqa: F401
3131
from .RBend import RBend # noqa: F401
3232
from .RFCavity import RFCavity # noqa: F401

src/pals/kinds/all_elements.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from .CrabCavity import CrabCavity
1515
from .Drift import Drift
1616
from .EGun import EGun
17-
from .ElementReference import ElementReference
17+
from .PlaceholderName import PlaceholderName
1818
from .Feedback import Feedback
1919
from .Fiducial import Fiducial
2020
from .FloorShift import FloorShift
@@ -83,13 +83,13 @@ def get_all_element_types(extra_types: tuple = None):
8383

8484

8585
def get_all_elements_as_annotation(extra_types: tuple = None):
86-
"""Return the Union type of all allowed elements with their name as the discriminator field.
86+
"""Return the Union type of all allowed elements with their kind as the discriminator field.
8787
88-
Note: ElementReference is included to support string references to named elements.
89-
Since ElementReference doesn't have a 'kind' field, we cannot use discriminator.
88+
Note: PlaceholderName is included to support string references to named elements.
89+
Since PlaceholderName doesn't have a 'kind' field, we cannot use discriminator.
9090
Pydantic will still properly validate the union by trying each type in order in
9191
our unpack_element_list_structure method.
9292
"""
93-
types = get_all_element_types(extra_types) + (ElementReference,)
94-
# We can't use discriminator with ElementReference in the union since it has no 'kind' field
93+
types = get_all_element_types(extra_types) + (PlaceholderName,)
94+
# We can't use discriminator with PlaceholderName in the union since it has no 'kind' field
9595
return Union[types]

src/pals/kinds/mixin/all_element_mixin.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66

77
from . import BaseElement
8-
from ..ElementReference import ElementReference
8+
from ..PlaceholderName import PlaceholderName
99

1010

1111
def unpack_element_list_structure(
@@ -45,12 +45,12 @@ def unpack_element_list_structure(
4545
for item in data[field_name]:
4646
# An element can be a string that refers to another element
4747
if isinstance(item, str):
48-
# Wrap the string in an ElementReference object
49-
new_list.append(ElementReference(item))
48+
# Wrap the string in a Placeholder name object
49+
new_list.append(PlaceholderName(item))
5050
continue
51-
# An element can be an ElementReference instance directly
52-
elif isinstance(item, ElementReference):
53-
# Keep the ElementReference as-is
51+
# An element can be a PlaceholderName instance directly
52+
elif isinstance(item, PlaceholderName):
53+
# Keep the PlaceholderName as-is
5454
new_list.append(item)
5555
continue
5656
# An element can be a dict
@@ -77,7 +77,7 @@ def unpack_element_list_structure(
7777
continue
7878

7979
raise TypeError(
80-
f"Value must be a reference string, ElementReference, or a dict, but we got {item!r}"
80+
f"Value must be a reference string, PlaceholderName, or a dict, but we got {item!r}"
8181
)
8282

8383
data[field_name] = new_list

tests/test_elements.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -561,15 +561,15 @@ def test_BeamLine_with_string_references():
561561
assert beamline.name == "fodo_cell"
562562
assert len(beamline.line) == 3
563563

564-
# First element should be an ElementReference that behaves like the string "drift1"
565-
assert isinstance(beamline.line[0], pals.ElementReference)
564+
# First element should be a PlaceholderName that behaves like the string "drift1"
565+
assert isinstance(beamline.line[0], pals.PlaceholderName)
566566
assert beamline.line[0] == "drift1"
567567
assert beamline.line[0].name == "drift1"
568568
assert beamline.line[0].element is None # Not yet resolved
569569
assert not beamline.line[0].is_resolved()
570570

571-
# Second element should be an ElementReference that behaves like the string "quad1"
572-
assert isinstance(beamline.line[1], pals.ElementReference)
571+
# Second element should be a PlaceholderName that behaves like the string "quad1"
572+
assert isinstance(beamline.line[1], pals.PlaceholderName)
573573
assert beamline.line[1] == "quad1"
574574
assert beamline.line[1].name == "quad1"
575575
assert beamline.line[1].element is None # Not yet resolved
@@ -588,17 +588,17 @@ def test_BeamLine_with_string_references():
588588
assert beamline.line[0].element.length == 1.0
589589

590590

591-
def test_ElementReference_direct():
592-
"""Test ElementReference creation and behavior directly"""
591+
def test_PlaceholderName_direct():
592+
"""Test PlaceholderName creation and behavior directly"""
593593
# Test creation with positional argument
594-
ref1 = pals.ElementReference("test_element")
594+
ref1 = pals.PlaceholderName("test_element")
595595
assert ref1.name == "test_element"
596596
assert str(ref1) == "test_element"
597597
assert ref1 == "test_element"
598598
assert not ref1.is_resolved()
599599

600600
# Test creation with keyword argument
601-
ref2 = pals.ElementReference(name="another_element")
601+
ref2 = pals.PlaceholderName(name="another_element")
602602
assert ref2.name == "another_element"
603603
assert str(ref2) == "another_element"
604604
assert ref2 == "another_element"

0 commit comments

Comments
 (0)