Skip to content

Potential data state from _AllowedSurfaceIDs.__call__ returning None #5043

@Gobot1234

Description

@Gobot1234

🔍 Before submitting the issue

  • I have searched among the existing issues
  • I am using a Python virtual environment

🐞 Description of the bug

Currently this _AllowedSurfaceIDs.call silently throws away KeyError and IndexErrors

class _AllowedSurfaceIDs(_AllowedNames):
    def __call__(self, respect_data_valid: bool = True) -> list[int]:
        surface_info = self._field_info._get_surfaces_info()
        try:
            return [
                info["surface_id"][0]
                for name_, info in surface_info.items()
            ]
        except (KeyError, IndexError):
            pass

Would be a significant improvement to use something like this (though probably without the walrus)

class _AllowedSurfaceIDs(_AllowedNames):
    def __call__(self, respect_data_valid: bool = True) -> list[int]:
        surface_info = self._field_info._get_surfaces_info()
        try:
            return [
                info["surface_id"][0]
                for name_, info in surface_info.items()
                if (name := name_)
            ]
        except KeyError:
            raise LookupError("Failed to retrieve valid surface_id key.", name)
        except IndexError:
            raise LookupError("Failed to retrieve valid surface_id index.", name)

📝 Steps to reproduce

from types import SimpleNamespace

from ansys.api.fluent.v0 import field_data_pb2 as FieldDataProtoModule
from ansys.fluent.core.field_data_interfaces import _AllowedSurfaceIDs, _SurfaceIds
from ansys.fluent.core.services.field_data import _FieldInfo


response = FieldDataProtoModule.GetSurfacesInfoResponse()
surface = response.surfaceInfo.add()
surface.surfaceName = "bad-surface"
# Intentionally do not append to surface.surfaceId so _AllowedSurfaceIDs hits IndexError.

service = SimpleNamespace(get_surfaces_info=lambda request: response)
field_info = _FieldInfo(service=service, is_data_valid=lambda: True)
allowed = _AllowedSurfaceIDs(field_info=field_info)
print("allowed_call_result", allowed())

validator = _SurfaceIds(allowed)
validator.validate([1])
Traceback (most recent call last):
  File "/home/jhilton-/pyfluent/repros/type_issues/repro_field_data_surface_ids_none.py", line 24, in <module>
    validator.validate([1])
  File "/home/jhilton-/pyfluent/src/ansys/fluent/core/field_data_interfaces.py", line 362, in validate
    if surf not in self._allowed_surface_ids():
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: argument of type 'NoneType' is not iterable

💻 Which operating system are you using?

Windows

📀 Which ANSYS version are you using?

27.1

🐍 Which Python version are you using?

3.13

📦 Installed packages

N/A

Metadata

Metadata

Labels

bugIssue, problem or error in PyFluent

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions