-
Notifications
You must be signed in to change notification settings - Fork 76
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
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
kayoub5
reviewed
Jul 31, 2023
kayoub5
reviewed
Jul 31, 2023
kayoub5
reviewed
Jul 31, 2023
kayoub5
reviewed
Jul 31, 2023
kayoub5
reviewed
Jul 31, 2023
kayoub5
reviewed
Jul 31, 2023
kayoub5
reviewed
Jul 31, 2023
kayoub5
reviewed
Jul 31, 2023
kayoub5
reviewed
Jul 31, 2023
kayoub5
reviewed
Jul 31, 2023
kayoub5
reviewed
Jul 31, 2023
andlaus
force-pushed
the
odxassert
branch
3 times, most recently
from
July 31, 2023 13:29
6a500ea
to
79d7d9a
Compare
kayoub5
reviewed
Jul 31, 2023
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]>
Merged
This was referenced Sep 5, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
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 usingodxraise()
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.odxassert()
function is very similar to the build-inassert
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, soassert
statements which's purpose is to narrow the type of a given object for the benefit of type checkers must be replaced byif...odxraise()
constructs. E.g.odxrequire()
function raises an exception if its argument isNone
, 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 notNone
. E.g., ensuring that aSHORT-NAME
exists for anElementTree
node can be achieved usingAndreas Lauser <[email protected]>, on behalf of MBition GmbH.
Provider Information