diff --git a/src/ansys/dpf/core/inputs.py b/src/ansys/dpf/core/inputs.py index 9357a8e7ff4..c981b8b0349 100644 --- a/src/ansys/dpf/core/inputs.py +++ b/src/ansys/dpf/core/inputs.py @@ -118,15 +118,25 @@ def connect(self, inpt): self._operator()._find_outputs_corresponding_pins( self._python_expected_types, inpt, self._pin, corresponding_pins, input_type_name ) + # We can have a single output with multiple types compatible with this input + # if it accepts several of these types so we need to check for unique combinations + corresponding_pins = list(set(corresponding_pins)) if len(corresponding_pins) > 1: - err_str = "Pin connection is ambiguous, specify the input to connect to with:\n" + op_name = ( + self._operator().specification.properties["scripting_name"] + if "scripting_name" in self._operator().specification.properties + else self._operator().name + ) + err_str = f"Operator {op_name}:\n" + err_str += "Pin connection is ambiguous, specify the input to connect to with:\n" + inpt_name = inpt._operator.name for pin in corresponding_pins: err_str += ( - " - operator.inputs." + f" - {op_name}.inputs." + self._spec.name - + "(out_op." + + f"({inpt_name}." + inpt._dict_outputs[pin[1]].name - + ")" + + ")\n" ) err_str += "Connecting to first input in the list.\n" warnings.warn(message=err_str) diff --git a/tests/test_operator.py b/tests/test_operator.py index 1a6b28d0884..60bf0c12867 100644 --- a/tests/test_operator.py +++ b/tests/test_operator.py @@ -26,6 +26,7 @@ from pathlib import Path import shutil import types +import warnings import weakref import numpy @@ -576,6 +577,34 @@ def test_inputs_outputs_scopings_container(allkindofcomplexity): assert len(fc) == 4 +def test_connection_to_input_is_ambiguous(): + field = dpf.core.fields_factory.field_from_array(arr=[1.0, 2.0, 3.0]) + field.scoping = dpf.core.mesh_scoping_factory.nodal_scoping(node_ids=[1, 2, 3]) + min_max_op_1 = dpf.core.operators.min_max.min_max(field=field) + with pytest.warns(match="Pin connection is ambiguous"): + dpf.core.operators.min_max.min_max(field=min_max_op_1) + + +@pytest.mark.skipif( + not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_5_0, + reason="extract_scoping unavailable for server version < 5.0", +) +def test_connection_to_input_is_not_ambiguous(): + # Ensures that connecting an operator with a single output with multiple types all compatible + # does not raise an ambiguity warning. + # Here extract_scoping has only one output of type either Scoping or ScopingsContainer + # This output was reported twice and raised an ambiguity warning despite both types being + # compatible with the input + # This behavior is now fixed and enforced by this test + field = dpf.core.fields_factory.field_from_array(arr=[1.0, 2.0, 3.0]) + field.scoping = dpf.core.mesh_scoping_factory.nodal_scoping(node_ids=[1, 2, 3]) + scop = dpf.core.operators.utility.extract_scoping(field_or_fields_container=field) + stress = dpf.core.operators.result.stress() + with warnings.catch_warnings(): + warnings.filterwarnings(action="error", category=UserWarning, message="Operator stress:") + stress.inputs.mesh_scoping.connect(scop) + + def test_inputs_outputs_meshes_container(allkindofcomplexity): data_sources = dpf.core.DataSources(allkindofcomplexity) model = dpf.core.Model(data_sources)