Skip to content

Commit 6bfdf67

Browse files
committed
Adds ability to use "set_fields" with parameter names
1 parent 169eff6 commit 6bfdf67

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

RATapi/classlist.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,15 @@ def extend(self, other: Sequence[T]) -> None:
296296
self._check_unique_name_fields(other)
297297
self.data.extend(other)
298298

299-
def set_fields(self, index: int, **kwargs) -> None:
299+
def set_fields(self, index: Union[int, slice, str, T], **kwargs) -> None:
300300
"""Assign the values of an existing object's attributes using keyword arguments."""
301301
self._validate_name_field(kwargs)
302302
pydantic_object = False
303303

304+
# Find index if name or object is supplied
305+
if isinstance(index, (str, self._class_handle)):
306+
index = self.index(index)
307+
304308
if importlib.util.find_spec("pydantic"):
305309
# Pydantic is installed, so set up a context manager that will
306310
# suppress custom validation errors until all fields have been set.

tests/test_classlist.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,7 @@ def test_extend_empty_classlist(extended_list: Sequence, one_name_class_list: Cl
729729
assert isinstance(extended_list[-1], class_list._class_handle)
730730

731731

732+
@pytest.mark.parametrize("index", [0, "Alice"])
732733
@pytest.mark.parametrize(
733734
["new_values", "expected_classlist"],
734735
[
@@ -739,10 +740,12 @@ def test_extend_empty_classlist(extended_list: Sequence, one_name_class_list: Cl
739740
),
740741
],
741742
)
742-
def test_set_fields(two_name_class_list: ClassList, new_values: dict[str, Any], expected_classlist: ClassList) -> None:
743+
def test_set_fields(
744+
two_name_class_list: ClassList, index: Union[int, str], new_values: dict[str, Any], expected_classlist: ClassList
745+
) -> None:
743746
"""We should be able to set field values in an element of a ClassList using keyword arguments."""
744747
class_list = two_name_class_list
745-
class_list.set_fields(0, **new_values)
748+
class_list.set_fields(index, **new_values)
746749
assert class_list == expected_classlist
747750

748751

0 commit comments

Comments
 (0)