Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor exception handling #168

Merged
merged 2 commits into from
Aug 2, 2023
Merged

Conversation

andlaus
Copy link
Collaborator

@andlaus andlaus commented Jul 31, 2023

These changes make it possible to continue the program execution if a deviation from the ODX specification was detected or a hard-coded assumption was violated. (If any of this happens, the behavior after that point is undefined.) The new mechanism consists of the following parts:

  • The odxtools.exceptions.strict_mode variable indicates whether program execution should be attempted to be continued if a violation is detected or not. The default is to not continue. Switching to non-strict mode can be achieved using
    import odxtools
    
    odxtools.exceptions.strict_mode = False
  • The odxraise() function raises an exception in strict mode and does nothing in non-strict mode. For type checkers, it looks like it always raises an exception.
  • The odxassert() function is very similar to the build-in assert statement, except that if the asserted condition is not met and if odxtools is in non-strict mode, program execution will continue. Unfortunately, this does not work well with type checkers as these do not consider the internal behavior of functions, so assert statements which's purpose is to narrow the type of a given object for the benefit of type checkers must be replaced by if...odxraise() constructs. E.g.
    # prevent mypy from complaining
    assert isinstance(x, int)
    becomes
    # prevent mypy from complaining
    if not isinstance(x, int): odxraise()
  • In strict mode, the odxrequire() function raises an exception if its argument is None, in all other cases it passes through its argument without modifications. This allows to conveniently ensure that a given object was specified whilst convincing type checkers that it is not None. E.g., ensuring that a SHORT-NAME exists for an ElementTree node can be achieved using
    short_name = odxrequire(et_element.findtext("SHORT-NAME"))

Andreas Lauser <[email protected]>, on behalf of MBition GmbH.
Provider Information

@andlaus andlaus requested a review from kayoub5 July 31, 2023 08:13
README.md Show resolved Hide resolved
odxtools/utils.py Outdated Show resolved Hide resolved
examples/somersaultecu.py Outdated Show resolved Hide resolved
odxtools/nameditemlist.py Outdated Show resolved Hide resolved
odxtools/nameditemlist.py Outdated Show resolved Hide resolved
@andlaus andlaus force-pushed the odxassert branch 3 times, most recently from 6a500ea to 79d7d9a Compare July 31, 2023 13:29
odxtools/exceptions.py Outdated Show resolved Hide resolved
andlaus and others added 2 commits August 1, 2023 15:00
This commit makes it possible to continue the program execution if a
deviation from the ODX specification was detected or a hard-coded
assumption was violated. (If this is happens, the behavior is
undefined.) The new mechanism consists of the following parts:

- The `odxtools.exceptions.strict_mode` variable indicates whether
  program execution should be attempted to be continued if a violation
  is detected or not. The default is to not continue. Switching to
  non-strict mode can be achieved using
  ```python
  import odxtools

  odxtools.exceptions.strict_mode = False
  ```
- The `odxraise()` function raises an exception in strict mode and
  does nothing in non-strict mode.
- The `odxassert()` function is very similar to the build-in `assert`
  statement, except that if the asserted condition is not met and if
  odxtools is in non-strict mode, program execution will
  continue. Unfortunately, this does not work well with type checkers
  as these do not consider the internal behaviour of functions, so
  `assert` statements which's purpose is to instruct type checkers
  that an object is of a given type must be replaced by
  `if...odxraise()` constructs. E.g.
  ```python
  # prevent mypy from complaining
  assert isinstance(x, int)
  ```
  becomes
  ```python
  # prevent mypy from complaining
  if not isinstance(x, int): odxraise()
  ```

- In strict mode, the `odxrequire()` function raises an exeption if
  its argument is `None`, in all other cases it passes through its
  argument without modifications. This allows to conveniently ensure
  that a given object was specified whilst convincing type checkers
  that it is not `None`. E.g., ensuring that a `SHORT-NAME` exists for
  an ElementTree node can be achieved using
  ```python
  short_name = odxrequire(et_element.findtext("SHORT-NAME")
  ```

Signed-off-by: Andreas Lauser <[email protected]>
Signed-off-by: Christian Hackenbeck <[email protected]>
thanks!

Signed-off-by: Andreas Lauser <[email protected]>
Signed-off-by: Christian Hackenbeck <[email protected]>
@andlaus andlaus merged commit 7d47f96 into mercedes-benz:main Aug 2, 2023
6 checks passed
@andlaus andlaus mentioned this pull request Aug 2, 2023
@andlaus andlaus deleted the odxassert branch December 7, 2023 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants