Skip to content

Commit 06567a3

Browse files
authored
Deprecate Inputs.connect and handle ambiguous connections (#2053)
* 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
1 parent b673c4f commit 06567a3

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/ansys/dpf/core/inputs.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"""Inputs."""
2424

2525
from textwrap import wrap
26+
import warnings
2627
import weakref
2728

2829
from ansys.dpf import core
@@ -112,7 +113,7 @@ def connect(self, inpt):
112113
self._python_expected_types, inpt, self._pin, corresponding_pins
113114
)
114115
if len(corresponding_pins) > 1:
115-
err_str = "Pin connection is ambiguous, specify the pin with:\n"
116+
err_str = "Pin connection is ambiguous, specify the input to connect to with:\n"
116117
for pin in corresponding_pins:
117118
err_str += (
118119
" - operator.inputs."
@@ -121,7 +122,9 @@ def connect(self, inpt):
121122
+ inpt._dict_outputs[pin[1]].name
122123
+ ")"
123124
)
124-
raise ValueError(err_str)
125+
err_str += "Connecting to first input in the list.\n"
126+
warnings.warn(message=err_str)
127+
corresponding_pins = [corresponding_pins[0]]
125128

126129
if len(corresponding_pins) == 0:
127130
err_str = (
@@ -218,13 +221,20 @@ def connect(self, inpt):
218221
219222
Searches for the input type corresponding to the output.
220223
224+
.. deprecated::
225+
Deprecated in favor of explicit output-to-input connections.
226+
221227
Parameters
222228
----------
223229
inpt : str, int, double, bool, list[int], list[float], Field, FieldsContainer, Scoping,
224230
ScopingsContainer, MeshedRegion, MeshesContainer, DataSources, CyclicSupport, Outputs, os.PathLike # noqa: E501
225231
Input of the operator.
226232
227233
"""
234+
warnings.warn(
235+
message="Use explicit output-to-input connections.", category=DeprecationWarning
236+
)
237+
228238
from pathlib import Path
229239

230240
corresponding_pins = []
@@ -250,12 +260,14 @@ def connect(self, inpt):
250260
corresponding_pins,
251261
)
252262
if len(corresponding_pins) > 1:
253-
err_str = "Pin connection is ambiguous, specify the pin with:\n"
263+
err_str = "Pin connection is ambiguous, specify the input to connect to with:\n"
254264
for pin in corresponding_pins:
255265
if isinstance(pin, tuple):
256266
pin = pin[0]
257267
err_str += " - operator.inputs." + self._dict_inputs[pin].name + "(input)\n"
258-
raise ValueError(err_str)
268+
err_str += "Connecting to first input in the list.\n"
269+
warnings.warn(message=err_str)
270+
corresponding_pins = [corresponding_pins[0]]
259271

260272
if len(corresponding_pins) == 0:
261273
err_str = "The input should have one of the expected types:\n"

tests/test_operator.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,11 @@ def test_inputs_outputs_scopings_container(allkindofcomplexity):
556556
assert scop.location == dpf.core.locations.elemental
557557

558558
stress = model.results.stress()
559-
stress.inputs.connect(op.outputs)
559+
with (
560+
# pytest.warns(match="Pin connection is ambiguous"),
561+
pytest.warns(DeprecationWarning, match="Use explicit"),
562+
):
563+
stress.inputs.connect(op.outputs)
560564
fc = stress.outputs.fields_container()
561565
assert fc.labels == ["elshape", "time"]
562566
assert len(fc) == 4
@@ -589,8 +593,17 @@ def test_inputs_outputs_meshes_container(allkindofcomplexity):
589593
sc = opsc.outputs.mesh_scoping()
590594

591595
stress = model.results.stress()
592-
stress.inputs.connect(op.outputs)
593-
stress.inputs.connect(opsc.outputs)
596+
with (
597+
# pytest.warns(match="Pin connection is ambiguous"),
598+
pytest.warns(DeprecationWarning, match="Use explicit"),
599+
):
600+
stress.inputs.connect(op.outputs)
601+
602+
with (
603+
# pytest.warns(match="Pin connection is ambiguous"),
604+
pytest.warns(DeprecationWarning, match="Use explicit"),
605+
):
606+
stress.inputs.connect(opsc.outputs)
594607
fc = stress.outputs.fields_container()
595608
assert fc.labels == ["body", "elshape", "time"]
596609
assert len(fc) == 4

0 commit comments

Comments
 (0)