Skip to content

Commit

Permalink
Deprecate Inputs.connect and handle ambiguous connections (#2053)
Browse files Browse the repository at this point in the history
* Propose fix to Inputs.connect() issue.

* Update tests

* Use warnings.warn(DeprecationWarning) as type_extensions is not in the DPF shipped Python

* Fix test to pass on master before sync
  • Loading branch information
PProfizi authored Feb 4, 2025
1 parent b673c4f commit 06567a3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
20 changes: 16 additions & 4 deletions src/ansys/dpf/core/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"""Inputs."""

from textwrap import wrap
import warnings
import weakref

from ansys.dpf import core
Expand Down Expand Up @@ -112,7 +113,7 @@ def connect(self, inpt):
self._python_expected_types, inpt, self._pin, corresponding_pins
)
if len(corresponding_pins) > 1:
err_str = "Pin connection is ambiguous, specify the pin with:\n"
err_str = "Pin connection is ambiguous, specify the input to connect to with:\n"
for pin in corresponding_pins:
err_str += (
" - operator.inputs."
Expand All @@ -121,7 +122,9 @@ def connect(self, inpt):
+ inpt._dict_outputs[pin[1]].name
+ ")"
)
raise ValueError(err_str)
err_str += "Connecting to first input in the list.\n"
warnings.warn(message=err_str)
corresponding_pins = [corresponding_pins[0]]

if len(corresponding_pins) == 0:
err_str = (
Expand Down Expand Up @@ -218,13 +221,20 @@ def connect(self, inpt):
Searches for the input type corresponding to the output.
.. deprecated::
Deprecated in favor of explicit output-to-input connections.
Parameters
----------
inpt : str, int, double, bool, list[int], list[float], Field, FieldsContainer, Scoping,
ScopingsContainer, MeshedRegion, MeshesContainer, DataSources, CyclicSupport, Outputs, os.PathLike # noqa: E501
Input of the operator.
"""
warnings.warn(
message="Use explicit output-to-input connections.", category=DeprecationWarning
)

from pathlib import Path

corresponding_pins = []
Expand All @@ -250,12 +260,14 @@ def connect(self, inpt):
corresponding_pins,
)
if len(corresponding_pins) > 1:
err_str = "Pin connection is ambiguous, specify the pin with:\n"
err_str = "Pin connection is ambiguous, specify the input to connect to with:\n"
for pin in corresponding_pins:
if isinstance(pin, tuple):
pin = pin[0]
err_str += " - operator.inputs." + self._dict_inputs[pin].name + "(input)\n"
raise ValueError(err_str)
err_str += "Connecting to first input in the list.\n"
warnings.warn(message=err_str)
corresponding_pins = [corresponding_pins[0]]

if len(corresponding_pins) == 0:
err_str = "The input should have one of the expected types:\n"
Expand Down
19 changes: 16 additions & 3 deletions tests/test_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,11 @@ def test_inputs_outputs_scopings_container(allkindofcomplexity):
assert scop.location == dpf.core.locations.elemental

stress = model.results.stress()
stress.inputs.connect(op.outputs)
with (
# pytest.warns(match="Pin connection is ambiguous"),
pytest.warns(DeprecationWarning, match="Use explicit"),
):
stress.inputs.connect(op.outputs)
fc = stress.outputs.fields_container()
assert fc.labels == ["elshape", "time"]
assert len(fc) == 4
Expand Down Expand Up @@ -589,8 +593,17 @@ def test_inputs_outputs_meshes_container(allkindofcomplexity):
sc = opsc.outputs.mesh_scoping()

stress = model.results.stress()
stress.inputs.connect(op.outputs)
stress.inputs.connect(opsc.outputs)
with (
# pytest.warns(match="Pin connection is ambiguous"),
pytest.warns(DeprecationWarning, match="Use explicit"),
):
stress.inputs.connect(op.outputs)

with (
# pytest.warns(match="Pin connection is ambiguous"),
pytest.warns(DeprecationWarning, match="Use explicit"),
):
stress.inputs.connect(opsc.outputs)
fc = stress.outputs.fields_container()
assert fc.labels == ["body", "elshape", "time"]
assert len(fc) == 4
Expand Down

0 comments on commit 06567a3

Please sign in to comment.