Skip to content

ENH: Improve Python errors of mismatched types

Summary

This MR addresses and closes #2175 (closed): When using Python API for app.SetParameterValue() with mismatching parameter types, the reported error is cryptic and doesn't help Python-developers to fix the function call. Scientific end-users would be even more troubled by such error messages.

Rationale

When we pass data of the wrong type or even None to otb.Application.SetParameterValue(), we have no way to know what we did wrong.

In the stack trace we see messages like:

   File "/path/to/OTB-7.2.0-Linux64/lib/python/otbApplication.py", line 2920, in SetParameterString
    val = _otbApplication.Application_SetParameterString(self, parameter, value, hasUserValueFlag)
TypeError: in method 'Application_SetParameterString', argument 3 of type 'std::string'

But, which parameter is it? What is its value?

Instead, we could have

  File "/home/luc/dev/tests/OTB-7.2.0-Linux64/lib/python/otbApplication.py", line 3239, in SetParameterValue
    raise TypeError('Expected a string for %s, got %s for %s' % (paramKey, type(value), value))
TypeError: Expected a string for elev.geoid, got <class 'NoneType'> for None

Implementation Details

An extra test on the real parameter types has been added for most expected types according to the actual parameters already identified.

Warning: However, this may disable implicit conversions that were previously accepted. We will need to analyse whether things like myapp.SetParameterValue('a_string_parameter', 42) shall be rejected to force the use of myapp.SetParameterValue('a_string_parameter', '42') instead (note the extra quotes around the literal constant number).

Tests

No extra test has been written. All preexisting tests shall continue to work.

Copyright

The copyright owner is CSGroup France and has signed the ORFEO ToolBox Contributor License Agreement.


Check before merging:

  • All discussions are resolved
  • At least 2 👍 votes from core developers, no 👎 vote.
  • The feature branch is (reasonably) up-to-date with the base branch
  • Dashboard is green
  • Copyright owner has signed the ORFEO ToolBox Contributor License Agreement
  • Optionally, run git diff develop... -U0 --no-color | clang-format-diff.py -p1 -i on latest changes and commit
Edited by Luc Hermitte

Merge request reports

Loading