From d6447cc07859d73c629ffdfb59e302afaaffab50 Mon Sep 17 00:00:00 2001 From: Mainak Kundu <94432368+mkundu1@users.noreply.github.com> Date: Sun, 24 Nov 2024 09:27:37 -0500 Subject: [PATCH] feat: Store the original scheme name of aliases (#3509) * feat: Store the original scheme name of aliases * fix: test --- .../fluent/core/codegen/settingsgen_old.py | 2 +- src/ansys/fluent/core/solver/flobject.py | 15 ++++++++-- tests/test_settings_api.py | 29 ++++++++++--------- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/ansys/fluent/core/codegen/settingsgen_old.py b/src/ansys/fluent/core/codegen/settingsgen_old.py index 333c4c8dc6b..990d32bbb3b 100644 --- a/src/ansys/fluent/core/codegen/settingsgen_old.py +++ b/src/ansys/fluent/core/codegen/settingsgen_old.py @@ -461,7 +461,7 @@ def _populate_classes(parent_dir): child_aliases = getattr(cls, "_child_aliases", None) if child_aliases: f.write(f"{istr1}_child_aliases = dict(\n") - f.writelines([f'{istr2}{k}="{v}",\n' for k, v in child_aliases.items()]) + f.writelines([f"{istr2}{k}={v!r},\n" for k, v in child_aliases.items()]) f.write(f"{istr1})\n\n") # write object type diff --git a/src/ansys/fluent/core/solver/flobject.py b/src/ansys/fluent/core/solver/flobject.py index f8b3137b58f..c0a41fe3db0 100644 --- a/src/ansys/fluent/core/solver/flobject.py +++ b/src/ansys/fluent/core/solver/flobject.py @@ -730,7 +730,7 @@ def _unalias(self, cls, value): outer_set_states = [] for k, v in value.items(): if hasattr(cls, "_child_aliases") and k in cls._child_aliases: - alias = cls._child_aliases[k] + alias, _ = cls._child_aliases[k] comps = alias.split("/") if comps[0] == "..": outer_obj = self @@ -1122,6 +1122,7 @@ def __getattribute__(self, name): raise InactiveObjectError(self.python_path) alias = super().__getattribute__("_child_aliases").get(name) if alias: + alias = alias[0] alias_obj = self._child_alias_objs.get(name) if alias_obj is None: obj = self.find_object(alias) @@ -1457,6 +1458,7 @@ def get(self, name: str) -> ChildTypeT: def __getattr__(self, name: str): alias = self._child_aliases.get(name) if alias: + alias = alias[0] alias_obj = self._child_alias_objs.get(name) if alias_obj is None: obj = self.find_object(alias) @@ -1574,6 +1576,7 @@ def __setitem__(self, index: int, value): def __getattr__(self, name: str): alias = self._child_aliases.get(name) if alias: + alias = alias[0] alias_obj = self._child_alias_objs.get(name) if alias_obj is None: obj = self.find_object(alias) @@ -1664,6 +1667,7 @@ def get_completer_info(self, prefix="", excluded=None) -> List[List[str]]: def __getattr__(self, name: str): alias = self._child_aliases.get(name) if alias: + alias = alias[0] alias_obj = self._child_alias_objs.get(name) if alias_obj is None: obj = self.find_object(alias) @@ -2153,8 +2157,13 @@ def _process_cls_names(info_dict, names, write_doc=False): for k, v in ( child_aliases | command_aliases | query_aliases | argument_aliases ).items(): - cls._child_aliases[to_python_name(k)] = "/".join( - x if x == ".." else to_python_name(x) for x in v.split("/") + # Storing the original name as we don't have any other way + # to recover it at runtime. + cls._child_aliases[to_python_name(k)] = ( + "/".join( + x if x == ".." else to_python_name(x) for x in v.split("/") + ), + k, ) except Exception: diff --git a/tests/test_settings_api.py b/tests/test_settings_api.py index 44a57e5359f..6dc045a111e 100644 --- a/tests/test_settings_api.py +++ b/tests/test_settings_api.py @@ -226,13 +226,13 @@ def test_deprecated_settings_with_custom_aliases(new_solver_session): solver = new_solver_session case_path = download_file("mixing_elbow.cas.h5", "pyfluent/mixing_elbow") download_file("mixing_elbow.dat.h5", "pyfluent/mixing_elbow") - solver.file._setattr("_child_aliases", {"rcd": "read_case_data"}) + solver.file._setattr("_child_aliases", {"rcd": ("read_case_data", "rcd")}) with pytest.warns(DeprecatedSettingWarning): solver.file.rcd(file_name=case_path) solver.setup.boundary_conditions.velocity_inlet.child_object_type._child_aliases[ "mom" - ] = "momentum" + ] = ("momentum", "mom") with pytest.warns(DeprecatedSettingWarning): solver.setup.boundary_conditions.velocity_inlet["hot-inlet"].mom.velocity = 20 assert ( @@ -261,12 +261,9 @@ def test_deprecated_settings_with_custom_aliases(new_solver_session): ) > 0 ) - assert ( - solver.setup.boundary_conditions.wall[ - "wall-inlet" - ].thermal.temperature._child_aliases["constant"] - == "value" - ) + assert solver.setup.boundary_conditions.wall[ + "wall-inlet" + ].thermal.temperature._child_aliases["constant"] == ("value", "constant") with pytest.warns(DeprecatedSettingWarning): solver.setup.boundary_conditions.wall[ "wall-inlet" @@ -298,7 +295,7 @@ def test_deprecated_settings_with_custom_aliases(new_solver_session): == 410 ) - solver.setup.boundary_conditions._setattr("_child_aliases", {"w": "wall"}) + solver.setup.boundary_conditions._setattr("_child_aliases", {"w": ("wall", "w")}) with pytest.warns(DeprecatedSettingWarning): solver.setup.boundary_conditions.w["wall-inlet"].thermal.temperature.value = 420 @@ -307,7 +304,7 @@ def test_deprecated_settings_with_custom_aliases(new_solver_session): == 420 ) - solver.setup._setattr("_child_aliases", {"bc": "boundary_conditions"}) + solver.setup._setattr("_child_aliases", {"bc": ("boundary_conditions", "bc")}) with pytest.warns(DeprecatedSettingWarning): solver.setup.bc.wall["wall-inlet"].thermal.temperature.value = 430 @@ -326,7 +323,7 @@ def test_deprecated_settings_with_custom_aliases(new_solver_session): == 400 ) - solver.results._setattr("_child_aliases", {"gr": "graphics"}) + solver.results._setattr("_child_aliases", {"gr": ("graphics", "gr")}) with pytest.warns(DeprecatedSettingWarning): solver.results.gr.contour.create("c1") @@ -342,7 +339,10 @@ def test_deprecated_settings_with_custom_aliases(new_solver_session): solver.setup.boundary_conditions.velocity_inlet[ "hot-inlet" - ].momentum.velocity._child_aliases["hd"] = "../../turbulence/hydraulic_diameter" + ].momentum.velocity._child_aliases["hd"] = ( + "../../turbulence/hydraulic_diameter", + "hd", + ) with pytest.warns(DeprecatedSettingWarning): solver.setup.boundary_conditions.velocity_inlet[ "hot-inlet" @@ -481,7 +481,10 @@ def test_child_alias_with_parent_path(mixing_elbow_settings_session): solver.settings.solution.initialization.hybrid_initialize() assert ( solver.settings.setup.models.discrete_phase.numerics.node_based_averaging.kernel._child_aliases - == {"gaussian_factor": "../gaussian_factor", "option": "../kernel_type"} + == { + "gaussian_factor": ("../gaussian_factor", "gaussian-factor"), + "option": ("../kernel_type", "option"), + } ) solver.settings.setup.models.discrete_phase.numerics.node_based_averaging.enabled = ( True