diff --git a/doc/changelog.d/5117.added.md b/doc/changelog.d/5117.added.md new file mode 100644 index 00000000000..6452a86829a --- /dev/null +++ b/doc/changelog.d/5117.added.md @@ -0,0 +1 @@ +Make ListObject a Sequence diff --git a/doc/source/user_guide/solver_settings/solver_settings_contents.rst b/doc/source/user_guide/solver_settings/solver_settings_contents.rst index 6858467972f..ac956d7a2ef 100644 --- a/doc/source/user_guide/solver_settings/solver_settings_contents.rst +++ b/doc/source/user_guide/solver_settings/solver_settings_contents.rst @@ -82,13 +82,13 @@ of container objects: :obj:`~ansys.fluent.core.solver.flobject.Group`, children can be accessed via ``.get_object_names()``. - The :obj:`~ansys.fluent.core.solver.flobject.ListObject` type is a container holding dynamically - created unnamed objects of + created, unnamed objects of its specified child type (accessible via a ``child_object_type`` attribute) in a - list. Children of a ``ListObject`` object can be accessed using the index operator. + :term:`sequence`\. ``ListObject`` behaves much like a tuple though it supports :meth:`object.__setitem__`. + Children of a ``ListObject`` object can be accessed using the index operator. For example, ``solver_session.settings.setup.cell_zone_conditions.fluid['fluid-1'].sources.terms['mass'][2]`` refers to the third (starting from index 0) mass source entry for the fluid zone - named ``fluid-1``. The current number of child objects can be accessed with the - ``get_size()`` method. + named ``fluid-1``. .. vale Google.Spacing = YES diff --git a/src/ansys/fluent/core/solver/flobject.py b/src/ansys/fluent/core/solver/flobject.py index 63af4864609..9c63e2a5bf3 100644 --- a/src/ansys/fluent/core/solver/flobject.py +++ b/src/ansys/fluent/core/solver/flobject.py @@ -41,6 +41,7 @@ from __future__ import annotations import collections +from collections.abc import Sequence from contextlib import contextmanager, nullcontext, suppress from enum import Enum import fnmatch @@ -69,6 +70,7 @@ _eval_type, get_args, get_origin, + overload, ) import warnings import weakref @@ -1352,7 +1354,7 @@ def __setitem__(self, name, value): self[name].set_state(value) -ChildTypeT = TypeVar("ChildTypeT") +ChildTypeT = TypeVar("ChildTypeT", bound="SettingsBase") class NamedObject(SettingsBase[DictStateType], Generic[ChildTypeT]): @@ -1618,7 +1620,7 @@ def _rename(obj: NamedObject | _Alias, new: str, old: str): obj._create_child_object(new) -class ListObject(SettingsBase[ListStateType], Generic[ChildTypeT]): +class ListObject(SettingsBase[ListStateType], Sequence[ChildTypeT]): """A ``ListObject`` container is a container object, similar to a Python list object. Generally, many such objects can be created. @@ -1692,10 +1694,18 @@ def get_size(self) -> int: """ return self.flproxy.get_list_size(self.path) - def __getitem__(self, index: int) -> ChildTypeT: + @overload + def __getitem__(self, index: int) -> ChildTypeT: ... + + @overload + def __getitem__(self, index: slice) -> list[ChildTypeT]: ... + + def __getitem__(self, index: int | slice) -> ChildTypeT | list[ChildTypeT]: size = self.get_size() - if index >= size: - raise IndexError(index) + if isinstance(index, int): + if index >= size: + raise IndexError(index) + if len(self._objects) != size: self._update_objects() return self._objects[index] diff --git a/tests/test_flobject.py b/tests/test_flobject.py index b7d481f745c..f5696a88e71 100644 --- a/tests/test_flobject.py +++ b/tests/test_flobject.py @@ -537,9 +537,22 @@ def test_list_object(): {"il_1": None, "bl_1": None}, {"il_1": [1, 2], "bl_1": None}, ] + + assert r.l_1[0:1][0].il_1() is None + assert r.l_1[1:2][0].il_1() == [1, 2] + r.l_1 = [{"il_1": [3], "bl_1": [True, False]}] assert r.l_1() == [{"il_1": [3], "bl_1": [True, False]}] + r.l_1 = [ + {"il_1": [1], "bl_1": None}, + {"il_1": None, "bl_1": None}, + {"il_1": [2], "bl_1": None}, + {"il_1": None, "bl_1": None}, + {"il_1": [3], "bl_1": None}, + ] + assert [inner.il_1() for inner in r.l_1[::2]] == [[1], [2], [3]] + def test_list_object_set_state_with_quantity_values(): class RealWithUnits(Real):