ansys / pyfluent

Pythonic interface to Ansys Fluent
https://fluent.docs.pyansys.com
MIT License
238 stars 41 forks source link

Require Pythonic error message for wrong data type #1003

Open seanpearsonuk opened 1 year ago

seanpearsonuk commented 1 year ago

E.g.,

>>> s.solution.report_definitions.surface["r1"](report_type="surface-areaavg",field="pressure",average_over=1, per_surface=False,surface_names="wall")

Error: CAR: invalid argument [1]: wrong type [not a pair]
Error Object: "wall"

Requires e.g., a TypeError such as

TypeError: surface_names must be a str list, not a str

Next I change the value of the bool per_surface to an integer, and this is accepted and reflected in the new state:

>>> s.solution.report_definitions.surface["r1"](report_type="surface-areaavg",field="pressure",average_over=1, per_surface=1,surface_names=["wall"])
>>> s.solution.report_definitions.surface["r1"]()
{'report_type': 'surface-areaavg', 'field': 'pressure', 'average_over': 1, 'per_surface': 1, 'surface_names': ['wall']}

I change the integer, average_over, to a boolean and this causes another low-level error:

>>> s.solution.report_definitions.surface["r1"](report_type="surface-areaavg",field="pressure",average_over=True, per_surface=False,surface_names=["wall"])
Error: < (less-than): invalid argument [2]: wrong type [not a number]
Error Object: #t
seanpearsonuk commented 1 year ago

Fluent user story 750088

seanpearsonuk commented 9 months ago

We can do some work to mitigate this in flobject.py.

Latest:

>>> surf = solver.solution.report_definitions.surface
>>> rd1 = surf.create("rd1")
>>> rd1.surface_names = "wall"
>>> rd1.surface_names()
['wall']
>>> rd1.surface_names = 24

Error: wta(1st) to symbol->string
Error Object: 24
Traceback (most recent call last):
  File "C:\ANSYSDev\devZ3\vNNN\fluent\fluent24.1\cortex\pylib\flapi\flobject.py", line 546, in __setattr__
    return attr.set_state(value)
  File "C:\ANSYSDev\devZ3\vNNN\fluent\fluent24.1\cortex\pylib\flapi\flobject.py", line 271, in set_state
    return self.flproxy.set_var(self.path, self.to_scheme_keys(state))
  File "C:\ANSYSDev\devZ3\vNNN\fluent\fluent24.1\cortex\pylib\flapi\flproxy.py", line 5, in set_var
    return scheme.fl_set_var(path, value)
  File "C:\ANSYSDev\devZ3\vNNN\fluent\fluent24.1\cortex\pylib\flapi\scheme.py", line 21, in newf
    return f(*args, **kwds)
  File "C:\ANSYSDev\devZ3\vNNN\fluent\fluent24.1\cortex\pylib\flapi\scheme.py", line 98, in fl_set_var
    return _fl_set_var(path, value)
RuntimeError: wta(1st) to symbol->string
Error Object: 24

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\ANSYSDev\devZ3\vNNN\fluent\fluent24.1\cortex\pylib\flapi\flobject.py", line 550, in __setattr__
    raise allowed_values_error(name, value, allowed) from ex
  File "C:\ANSYSDev\devZ3\vNNN\fluent\fluent24.1\cortex\pylib\flapi\error_message.py", line 27, in allowed_values_error
    return ValueError(allowed_name_error_message(context, trial_name, allowed_values))
  File "C:\ANSYSDev\devZ3\vNNN\fluent\fluent24.1\cortex\pylib\flapi\error_message.py", line 18, in allowed_name_error_message
    matches = closest_allowed_names(trial_name, allowed_values)
  File "C:\ANSYSDev\devZ3\vNNN\fluent\fluent24.1\cortex\pylib\flapi\error_message.py", line 9, in closest_allowed_names
    return f(cutoff=0.6, n=5) or f(cutoff=0.3, n=1)
  File "C:\ANSYSDev\devZ3\vNNN\commonfiles\CPython\3_10\winx64\Release\python\lib\difflib.py", line 701, in get_close_matches
    s.set_seq2(word)
  File "C:\ANSYSDev\devZ3\vNNN\commonfiles\CPython\3_10\winx64\Release\python\lib\difflib.py", line 248, in set_seq2
    self.__chain_b()
  File "C:\ANSYSDev\devZ3\vNNN\commonfiles\CPython\3_10\winx64\Release\python\lib\difflib.py", line 280, in __chain_b
    for i, elt in enumerate(b):
TypeError: 'int' object is not iterable

So, this is an improvement. This means that we can catch TypeErrors in flobject.py, do the type checking in the catch block and then escalate to an appropriate, meaningful exception.

OTOH, we are accepting string for string list. Is this intentional.

seanpearsonuk commented 9 months ago

@h-krishnan, @gyeole was it an intentional change that this is now accepted:

>>> rd1.surface_names = "wall"
>>> rd1.surface_names()
['wall']