-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Unit parser only issues warning rather than throwing an exception for completely invalid string when using "vounit" format #16199
Comments
Welcome to Astropy 👋 and thank you for your first issue! A project member will respond to you as soon as possible; in the meantime, please double-check the guidelines for submitting issues and make sure you've provided the requested details. GitHub issues in the Astropy repository are used to track bug reports and feature requests; If your issue poses a question about how to use Astropy, please instead raise your question in the Astropy Discourse user forum and close this issue. If you feel that this issue has not been responded to in a timely manner, please send a message directly to the development mailing list. If the issue is urgent or sensitive in nature (e.g., a security vulnerability) please send an e-mail directly to the private e-mail feedback@astropy.org. |
Still a problem in astropy v6.1.dev . Looks like this call went here: astropy/astropy/units/format/generic.py Line 588 in 92a96e1
That invokes astropy/astropy/utils/parsing.py Line 107 in 92a96e1
This line calls an external library ( astropy/astropy/utils/parsing.py Line 120 in 92a96e1
You can get this same problematic behavior with this call: from astropy.units.format import vounit
vounit.VOUnit._parser.parser.parse("bad") This very low-level parse function does not understand
If the astropy/astropy/units/format/generic.py Line 586 in 92a96e1
I think it would have properly respected vounit.VOUnit._parse_unit("bad") # ValueError Thanks for the report! |
That said, this throws an exception even though theoretically it went though similar logic route. 🤯 from astropy.units.format.cds import CDS
CDS._parser.parser.parse("bad") |
FWIW if I comment out this line, it would respect --- a/astropy/units/format/vounit.py
+++ b/astropy/units/format/vounit.py
@@ -111,7 +111,7 @@ class VOUnit(generic.Generic):
core.UnitsWarning,
)
- return cls._def_custom_unit(t.value)
+ #return cls._def_custom_unit(t.value)
raise |
There's clearly tension between
The former is of course of interest to way more users of Astropy. But I think it's important for data publishers (@JeremyMcCormick and I are with the Rubin Observatory) to have the tools to do this properly. Our intent is to validate our published units specifically against VOUnit. From what I understand, the |
I don't recall in detail, but recall the vounit specification has a very liberal idea about custom units. I do agree it would be nice to allow checking things without having to turn warnings into errors, and |
I think this is somewhat up my alley so if no one else is already working on a fix I'm happy to give it a go ! |
From what I can tell this is a duplicate of #6302 and #13017. #13042 was an attempt to fix the problem. I am not very familiar with the VOUnit standard, but from what I remember from participating in the discussions the last time this was brought up is that apparently |
I totally forgot I tried to fix it before, and from a different angle... 😆 @tomdonaldson , your input would be appreciated here. Thanks! |
@eerovaher I think you are in essence referring to the following text from the standard?
(I.e., there's nothing special about Let's be clear what this says: "an implementation ... is not required to reject [an unrecognized] unit string". But it is not forbidden from doing so. This goes to my previous comment. If we expect Astropy to be used by people who are creating data, not just reading existing data in a permissive, annoyance-reducing way, it absolutely should have a way to validate units for being recognized VOUnit strings (the issue in this ticket) as well as a simple way to verify that in-memory The more we support writing valid VOUnit strings, the less, over time, the leniencies at read time may be needed. |
Thanks, @eerovaher, for finding those older issues - I knew there was something but didn't find them quickly enough! And thanks, @gpdf, for the good summary. Looking at the older issues, there seemed a general consensus on the outcome I'd love to see a new PR, though clearly the travails of #13042 shows that it is not as trivial as one might think. |
I would like to point out if it is not clear from the discussion that the unit validation in
When using I would really like to be able to get proper exceptions thrown here, because we are using a programmatic framework based on Pydantic where errors with unit specifications should percolate up as validation errors. Additional minor issue: Warnings messages are being printed twice. |
@neutrinoceros et al. Any progress on this or plans to include a fix in an upcoming release? We would very much like to be able to validate against the IVOA units specification and get proper exceptions when errors occur rather than just warning messages. |
It's definitely non trivial to fix, and there were already multiple failed attempts, so I don't want to make any promises here. However, I am still up for it if others think it's worth prioritizing. |
Description
The following warning prints when I define a unit in "vounit" format with a non-sensical string:
This should be throwing an exception, because "bad" is a completely invalid string for a units definition.
Expected behavior
The parser for "vounit" should be throwing an exception in this case and not just issuing a warning when
parse_strict="raise"
is set. Otherwise, it cannot be used programmatically to check VOUnit and raise errors when invalid ones are used.The regular expression matching used here represents undesirable behavior to me. The parser should not be doing fuzzy matching on strings which do not represent valid units and then issuing warnings instead of exceptions when it finds a string that kind of appears similar.
How to Reproduce
Versions
The text was updated successfully, but these errors were encountered: