You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 8b124d3
Browse filesBrowse the repository at this point in the historyBrowse files
Implement mechanism for more flexible unit parsing
`Unit()` accepts the `parse_strict` argument, which is supposed to let
users choose if unit parsing problems cause errors, warnings or are
suppressed silently. This has so far been done by letting unit parsers
raise errors, which are handled based on the `parse_strict` value.
However, raising an error forces parsers to stop early, meaning that
every problem becomes a critical error and `Unit` has no choice but to
return an `UnrecognizedUnit`. Therefore `parse_strict="raise"` cannot be
very strict because otherwise `parse_strict="warn"` and `"silent"` can
cause problems for users who might be e.g. reading in files they have
little control over.
An improvement is to let the unit parsers emit warnings for non-critical
problems and to let the machinery in `Unit` handle those warnings based
on the `parse_strict` value. Warnings do not force parsers to stop
early, so it will be possible to make the `parse_strict="raise"` option
stricter without forcing `UnrecognizedUnit` instances with `"warn"` or
`"silent"`. Unit parsers still have the option of immediately raising an
error in case of serious problems.
The new implementation places the warning handling in `_UnitMetaClass`
so it is inherited by `Unit`. The other possible implementation without
code duplication would be to place it in `Generic._do_parse()`. However,
that would require passing the `parse_strict` value through the unit
formatter `parse()` methods, which would be a backwards-incompatible
change.
A subtle (and fortunately minor) change is that if a string can be
parsed without problems, but the `parse_strict` value is invalid, then
the updated code raises an error. This is because the new code uses the
`parse_strict` value to set up appropriate warning filters, which must
be done before calling the unit parsers.
0 commit comments