Skip to content

Commit cc25009

Browse files
committed
Raise for version too low
1 parent 7043f9c commit cc25009

File tree

5 files changed

+54
-21
lines changed

5 files changed

+54
-21
lines changed

src/ansys/dpf/core/field.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
from ansys import dpf
3232
from ansys.dpf.core import dimensionality, errors, meshed_region, scoping, time_freq_support
33+
from ansys.dpf.core.available_result import Homogeneity
3334
from ansys.dpf.core.common import (
3435
_get_size_of_list,
3536
locations,
@@ -617,18 +618,18 @@ def unit(self):
617618
return self.field_definition.unit
618619

619620
@unit.setter
620-
def unit(self, value):
621+
def unit(self, value: str | tuple[Homogeneity, str]):
621622
"""Change the unit for the field.
622623
623624
A single string is interpreted as a known physical unit with an associated homogeneity.
624625
625-
A tuple of two strings is interpreted as a homogeneity and a unit name.
626-
If the homogeneity is Homogeneity.dimensionless, then the unit string is kept as a name.
627-
Otherwise, the homogeneity is ignored, and the unit string is interpreted as a known physical unit with an associated homogeneity.
626+
For DPF 11.0 (2026 R1) and above: A tuple of two strings is interpreted as a homogeneity and a unit name.
627+
If the homogeneity is :py:attr:`Homogeneity.dimensionless`, then the unit string is kept as a name.
628+
Otherwise, the homogeneity is ignored, and the unit string interpreted as a known physical unit with an associated homogeneity.
628629
629630
Parameters
630631
----------
631-
value : str | tuple(Homogeneity, str)
632+
value:
632633
Units for the field.
633634
634635
Examples
@@ -651,10 +652,14 @@ def unit(self, value):
651652
>>> print(my_field.unit)
652653
'dollars'
653654
655+
Notes
656+
-----
657+
Setting a named dimensionless unit requires DPF 11.0 (2026 R1) or above.
658+
654659
"""
655-
fielddef = self.field_definition
656-
fielddef.unit = value
657-
self.field_definition = fielddef
660+
field_def = self.field_definition
661+
field_def.unit = value
662+
self.field_definition = field_def
658663

659664
@property
660665
def dimensionality(self):

src/ansys/dpf/core/field_definition.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222

2323
"""FieldDefinition."""
2424

25+
from __future__ import annotations
26+
2527
from argparse import ArgumentError
2628
import traceback
2729
import warnings
2830

2931
from ansys.dpf.core import server as server_module
3032
from ansys.dpf.core.available_result import Homogeneity
31-
from ansys.dpf.core.check_version import version_requires
33+
from ansys.dpf.core.check_version import server_meet_version_and_raise, version_requires
3234
from ansys.dpf.core.common import natures, shell_layers
3335
from ansys.dpf.core.dimensionality import Dimensionality
3436
from ansys.dpf.gate import (
@@ -206,14 +208,37 @@ def is_of_quantity_type(self, quantity_type):
206208
return is_of_quantity_type
207209

208210
@unit.setter
209-
def unit(self, value):
211+
def unit(self, value: str | tuple[Homogeneity, str]):
212+
"""Change the unit for the field definition.
213+
214+
A single string is interpreted as a known physical unit with an associated homogeneity.
215+
216+
For DPF 11.0 (2026 R1) and above: A tuple of two strings is interpreted as a homogeneity and a unit name.
217+
If the homogeneity is :py:attr:`Homogeneity.dimensionless`, then the unit string is kept as a name.
218+
Otherwise, the homogeneity is ignored, and the unit string interpreted as a known physical unit with an associated homogeneity.
219+
220+
Parameters
221+
----------
222+
value:
223+
Units for the field.
224+
225+
Notes
226+
-----
227+
Setting a named dimensionless unit requires DPF 11.0 (2026 R1) or above.
228+
229+
"""
210230
# setter with explicit homogeneity: homogeneity is taken into account if it is dimensionless
211231
if (
212232
isinstance(value, tuple)
213233
and len(value) == 2
214234
and isinstance(value[0], Homogeneity)
215235
and isinstance(value[1], str)
216236
):
237+
server_meet_version_and_raise(
238+
required_version="11.0",
239+
server=self._server,
240+
msg="Setting a named dimensionless unit requires DPF 11.0 (2026 R1) or above.",
241+
)
217242
# csfield_definition_set_unit will ignore the homogeneity if it is not dimensionless
218243
self._api.csfield_definition_set_unit(self, value[1], None, value[0].value, 0, 0)
219244
# standard unit setter, using string interpreter

tests/conftest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ def return_ds(server=None):
329329
return return_ds
330330

331331

332+
SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_11_0 = meets_version(
333+
get_server_version(core._global_server()), "11.0"
334+
)
332335
SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_10_0 = meets_version(
333336
get_server_version(core._global_server()), "10.0"
334337
)

tests/entry/conftest.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ def _get_test_files_directory():
7171
"/tmp/test_files"
7272
)
7373

74-
SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_11_0 = meets_version(
75-
get_server_version(core._global_server()), "11.0"
76-
)
7774
SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_8_1 = meets_version(
7875
get_server_version(core._global_server()), "8.1"
7976
)

tests/test_field.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@
3333
from ansys.dpf.core.available_result import Homogeneity
3434
from ansys.dpf.core.check_version import server_meet_version
3535
from ansys.dpf.core.common import locations, shell_layers
36-
from ansys.dpf.gate.errors import DPFServerException
36+
from ansys.dpf.gate.errors import DPFServerException, DpfVersionNotSupported
3737
import conftest
3838
from conftest import (
3939
SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_8_0,
4040
SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_11_0,
41+
raises_for_servers_version_under,
4142
running_docker,
4243
)
4344

@@ -1426,19 +1427,21 @@ def test_deep_copy_big_field_remote(server_type, server_type_remote_process):
14261427
assert np.allclose(out.data, data)
14271428

14281429

1429-
@pytest.mark.skipif(
1430-
not SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_11_0, reason="Available for servers >=11.0"
1431-
)
1432-
def test_set_units():
1430+
def test_set_units(server_type):
14331431
data = np.random.random(100)
14341432
field = dpf.core.field_from_array(data)
14351433
# use string setter with recognized string
14361434
field.unit = "m"
14371435
assert field.unit == "m"
14381436

1439-
# use tuple(Homogeneity, string) setter
1440-
field.unit = (Homogeneity.dimensionless, "sones")
1441-
assert field.unit == "sones"
1437+
if server_meet_version("11.0", server_type):
1438+
# use tuple(Homogeneity, string) setter
1439+
field.unit = (Homogeneity.dimensionless, "sones")
1440+
assert field.unit == "sones"
1441+
else:
1442+
with pytest.raises(DpfVersionNotSupported):
1443+
# use tuple(Homogeneity, string) setter
1444+
field.unit = (Homogeneity.dimensionless, "sones")
14421445

14431446
# use unrecognized string
14441447
with pytest.raises(DPFServerException):

0 commit comments

Comments
 (0)