diff --git a/doc/changelog.d/5049.fixed.md b/doc/changelog.d/5049.fixed.md new file mode 100644 index 00000000000..2d5e53358f4 --- /dev/null +++ b/doc/changelog.d/5049.fixed.md @@ -0,0 +1 @@ +_AllowedSurfaceIDs.__call__ silently returning None on malformed surface info diff --git a/src/ansys/fluent/core/field_data_interfaces.py b/src/ansys/fluent/core/field_data_interfaces.py index 3d58d230a68..a9d271bfa96 100644 --- a/src/ansys/fluent/core/field_data_interfaces.py +++ b/src/ansys/fluent/core/field_data_interfaces.py @@ -568,13 +568,17 @@ def valid_name(self, surface_name: str) -> str: class _AllowedSurfaceIDs(_AllowedNames): def __call__(self, respect_data_valid: bool = True) -> List[int]: - try: - return [ - info["surface_id"][0] - for _, info in self._field_info._get_surfaces_info().items() - ] - except (KeyError, IndexError): - pass + surface_info = self._field_info._get_surfaces_info() + surface_ids = [] + for surface_name, info in surface_info.items(): + try: + surface_ids.append(info["surface_id"][0]) + except (IndexError, KeyError): + warnings.warn( + "Case has an invalid surface id. This is a bug in fluent" + ) + return [] + return surface_ids class FieldUnavailable(RuntimeError): diff --git a/tests/test_field_data.py b/tests/test_field_data.py index ee06e46b2a5..badbeb6c8d1 100644 --- a/tests/test_field_data.py +++ b/tests/test_field_data.py @@ -39,7 +39,9 @@ from ansys.fluent.core.exceptions import DisallowedValuesError from ansys.fluent.core.field_data_interfaces import ( FieldUnavailable, + _AllowedSurfaceIDs, _Fields, + _SurfaceIds, ) from ansys.fluent.core.services.field_data import ( CellElementType, @@ -1017,3 +1019,28 @@ def test_field_data_objects_3d_with_location_objects_overall( assert path_lines_data["hot-inlet"].scalar_field_name == "velocity-magnitude" assert list(path_lines_data["cold-inlet"].lines[100]) == [100, 101] + + +def test_allowed_surface_ids_warngs_on_missing_surface_id_key() -> None: + """_AllowedSurfaceIDs.__call__ should raise LookupError when surface_id key is missing.""" + + class _FakeFieldInfo: + def _get_surfaces_info(self): + return {"bad-surface": {"no_surface_id_key": []}} + + allowed = _AllowedSurfaceIDs(field_info=_FakeFieldInfo()) + with pytest.warns(Warning, match="missing surface id"): + allowed() + + +def test_surface_ids_validate_raises_on_bad_surface_info() -> None: + """_SurfaceIds.validate should propagate LookupError when surface info is malformed.""" + + class _FakeFieldInfo: + def _get_surfaces_info(self): + return {"bad-surface": {"surface_id": []}} + + allowed = _AllowedSurfaceIDs(field_info=_FakeFieldInfo()) + validator = _SurfaceIds(allowed) + with pytest.warns(Warning, match="missing surface id"): + validator.validate([1])