-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Make parsing deprecated units obey parse_strict
#18536
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
import numpy as np | ||
|
||
from astropy.units.core import CompositeUnit, NamedUnit, Unit, get_current_unit_registry | ||
from astropy.units.errors import UnitsWarning | ||
from astropy.units.errors import UnitParserWarning, UnitsWarning | ||
from astropy.units.utils import maybe_simple_fraction | ||
from astropy.utils.misc import did_you_mean | ||
|
||
|
@@ -253,14 +253,28 @@ def _did_you_mean_units(cls, unit: str) -> str: | |
return did_you_mean(unit, cls._units, fix=cls._fix_deprecated) | ||
|
||
@classmethod | ||
def _validate_unit(cls, unit: str, detailed_exception: bool = True) -> UnitBase: | ||
def _validate_unit( | ||
cls, | ||
unit: str, | ||
detailed_exception: bool = True, | ||
deprecations: Literal["silent", "warn", "raise"] = "warn", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I expect the new parameter will be helpful for #18304. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may be, and perhaps the best way forward would be to try to solve #18304 first (which can be done for 7.2). Though one of the options there has to be "convert". |
||
) -> UnitBase: | ||
if unit in cls._deprecated_units: | ||
if deprecations == "warn": | ||
warnings.warn(cls._deprecated_unit_message(unit), UnitParserWarning) | ||
elif deprecations == "raise": | ||
raise ValueError() | ||
try: | ||
return cls._units[unit] | ||
except KeyError: | ||
if detailed_exception: | ||
raise ValueError(cls._invalid_unit_error_message(unit)) from None | ||
raise ValueError() from None | ||
|
||
@classmethod | ||
def _deprecated_unit_message(cls, unit: str) -> str: | ||
return f"The unit '{unit}' has been deprecated in the {cls.__name__} standard." | ||
|
||
@classmethod | ||
def _invalid_unit_error_message(cls, unit: str) -> str: | ||
return ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Parsing deprecated units (e.g. ``u.Unit("erg", format="vounit", | ||
parse_strict=...)``) now obeys the ``parse_strict`` argument, which has so far | ||
been ignored in that context. | ||
Because by default ``parse_strict="raise"`` then parsing deprecated units can | ||
now cause an error where it previously succeeded with a warning, but the | ||
previous behaviour (for deprecated units) can be ensured in a | ||
backwards-compatible way by setting ``parse_strict="warn"`` explicitly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not obvious to me why a test about writing a VOTable should assert that parsing the string
"Crab"
with the"ogip"
format emits a warning, but that is what the test does.