diff --git a/src/ansys/dpf/core/model.py b/src/ansys/dpf/core/model.py index 95ca770996..dd67f25d4e 100644 --- a/src/ansys/dpf/core/model.py +++ b/src/ansys/dpf/core/model.py @@ -131,7 +131,7 @@ def metadata(self): return self._metadata @property - def results(self): + def results(self) -> Results: """Available results of the model. Organizes the results from DPF into accessible methods. All the available diff --git a/src/ansys/dpf/core/results.py b/src/ansys/dpf/core/results.py index a1801ee4f5..61e0169310 100644 --- a/src/ansys/dpf/core/results.py +++ b/src/ansys/dpf/core/results.py @@ -27,9 +27,12 @@ to easily access results in result files. """ +from __future__ import annotations + import functools from ansys.dpf.core import Operator, errors +from ansys.dpf.core.available_result import AvailableResult from ansys.dpf.core.custom_fields_container import ( BodyFieldsContainer, ElShapeFieldsContainer, @@ -121,24 +124,31 @@ def __init__( self._connect_operators(result_info) self._str = str(result_info) - def __result__(self, result_type, *args): + def __result__(self, result_type: AvailableResult, *args) -> Result: """ Create and return a result of the specified type. Parameters ---------- - result_type : any + result_type: The type of the result to generate. - *args : tuple + *args: Additional arguments required for creating the result. Returns ------- - Result + Result: An instance of the `Result` class, providing access to the specified result. """ return Result(self._connector, self._mesh_by_default, result_type, self._server) + def __getattr__(self, item): + """Inform the requested result is unavailable.""" + raise AttributeError( + f"Result '{item}' is not available. " + f"Available results are:\n{list(self._op_map_rev.keys())}" + ) + def _connect_operators(self, result_info): """Dynamically add operators for results. @@ -300,7 +310,7 @@ def __init__(self, connector, mesh_by_default, result_info, server): print(self._result_info.name) raise e - def __call__(self, time_scoping=None, mesh_scoping=None): + def __call__(self, time_scoping=None, mesh_scoping=None) -> Operator: """Provide for Result instances to be callable for operator retrieval.""" op = self._operator if time_scoping: diff --git a/tests/test_results.py b/tests/test_results.py new file mode 100644 index 0000000000..00d18104e7 --- /dev/null +++ b/tests/test_results.py @@ -0,0 +1,34 @@ +# Copyright (C) 2020 - 2025 ANSYS, Inc. and/or its affiliates. +# SPDX-License-Identifier: MIT +# +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +import pytest + +import ansys.dpf.core as dpf + + +def test_results_raise_unavailable_result(simple_rst): + model = dpf.Model(simple_rst) + results = model.results + _ = results.displacement + with pytest.raises( + AttributeError, match="Result 'test' is not available. Available results are:\n" + ): + _ = results.test