Skip to content

Improve Results and Result classes #2108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ansys/dpf/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 15 additions & 5 deletions src/ansys/dpf/core/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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:
Expand Down
34 changes: 34 additions & 0 deletions tests/test_results.py
Original file line number Diff line number Diff line change
@@ -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
Loading