-
-
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
VOUnit to issue VOWarning rather than generic UnitsWarning #13017
Comments
What would be a desired behavior here? |
To use
|
I am not sure if it will be a drop-in replacement in terms of the warning behavior. As you can see here: astropy/astropy/io/votable/exceptions.py Lines 1040 to 1052 in 6c15aa6
It is really customized towards displaying a particular XML format that goes beyond VO unit specification. If you pass it in as warning class, you will see some things that only makes sense if you also pass in things very specific to VO table parsing, like its configuration and position that the unit appears in the table. Is that what you really want? >>> import warnings
>>> from astropy.io.votable.exceptions import W50
>>> warnings.warn('foo', W50)
WARNING: W50: ?:?:?: W50: Invalid unit string 'foo' [warnings] When you use W50 in the actual XML validation, it displays something like this:
|
Then what about making this a new UnitsWarning, that is a subclass of W50? That would make the suppressing happy and can come with whatever custom text that makes sense for the units module. |
Looking at the inheritance tree of these two warnings,
|
I think both concerns — that incorrect VOUnits should warn with W50 and that we have a lot of warning classes — are reasonable. In this case Instead of subclassing |
I don't see any issues with having multiple inheritances for W50, but @tomdonaldson needs to comment on that from the VO side. |
I hope we are not over-engineering this...
Theoretically possible but it is starting to hurt my head since it already mixing in two different classes to begin with.
I cannot think of a simple way to do this. I think at the very least you will have to rewrite its constructor to handle this logic that is currently defined up in astropy/astropy/io/votable/exceptions.py Line 217 in 6c15aa6
|
If we look at the code that parses the VOTable then we see that it should already properly replace any astropy/astropy/io/votable/tree.py Lines 1390 to 1404 in acdefa2
The fact that we still get an UnitsWarning despite parse_strict='silent' means that there is a bug that would not be fixed by changing the warnings hierarchy, so that does not sound like a solution we should pursue. Indeed, the underlying bug can be easily confirmed if we try out different unit formats. With e.g. format='cds' everything works as expected:
>>> from astropy import units as u
>>> u.Unit('e-/s', format='cds', parse_strict='raise')
...
ValueError: 'e-/s' did not parse as cds unit: Syntax error If this is meant to be a custom unit...
>>> u.Unit('e-/s', format='cds', parse_strict='warn')
WARNING: UnitsWarning: 'e-/s' did not parse as cds unit: Syntax error If this is meant to be a custom unit...
UnrecognizedUnit(e-/s)
>>> u.Unit('e-/s', format='cds', parse_strict='silent')
UnrecognizedUnit(e-/s) But now with >>> u.Unit('e-/s', format='vounit', parse_strict='raise')
WARNING: UnitsWarning: Unit 'e' not supported by the VOUnit standard. [astropy.units.format.vounit]
...
ValueError: 'e-/s' did not parse as vounit unit: Invalid character at col 1 If this is meant to be a custom unit...
>>> u.Unit('e-/s', format='vounit', parse_strict='warn')
WARNING: UnitsWarning: Unit 'e' not supported by the VOUnit standard. [astropy.units.format.vounit]
WARNING: UnitsWarning: 'e-/s' did not parse as vounit unit: Invalid character at col 1 If this is meant to be a custom unit...
UnrecognizedUnit(e-/s)
>>> u.Unit('e-/s', format='vounit', parse_strict='silent')
WARNING: UnitsWarning: Unit 'e' not supported by the VOUnit standard. [astropy.units.format.vounit]
UnrecognizedUnit(e-/s) We can see that using |
Thanks for the sleuthing @eerovaher! |
@eerovaher , thanks for illustrating the bug nicely. Is anyone pursuing a fix? Not sure when I can get to this but I also don't want to duplicate work if you already know how to fix it. 😉 |
I accidentally came across a small test case that might narrow down the source of the warning. The >>> from astropy import units as u
>>> u.Unit('e-/s', format='vounit', parse_strict='foobar')
WARNING: UnitsWarning: Unit 'e' not supported by the VOUnit standard. [astropy.units.format.vounit]
Traceback (most recent call last):
File "C:\dev\astropy\astropy\units\format\generic.py", line 612, in _do_parse
return cls._parse_unit(s, detailed_exception=False)
File "C:\dev\astropy\astropy\units\format\vounit.py", line 124, in _parse_unit
raise ValueError()
ValueError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\dev\astropy\astropy\units\core.py", line 2019, in __call__
return f.parse(s)
File "C:\dev\astropy\astropy\units\format\vounit.py", line 94, in parse
result = cls._do_parse(s, debug=debug)
File "C:\dev\astropy\astropy\units\format\generic.py", line 615, in _do_parse
return cls._parser.parse(s, lexer=cls._lexer, debug=debug)
File "C:\dev\astropy\astropy\utils\parsing.py", line 115, in parse
return self.parser.parse(*args, **kwargs)
File "C:\dev\astropy\astropy\extern\ply\yacc.py", line 333, in parse
return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
File "C:\dev\astropy\astropy\extern\ply\yacc.py", line 1063, in parseopt_notrack
lookahead = get_token() # Get the next token
File "C:\dev\astropy\astropy\extern\ply\lex.py", line 386, in token
newtok = self.lexerrorf(tok)
File "C:\dev\astropy\astropy\units\format\generic.py", line 159, in t_error
raise ValueError(
ValueError: Invalid character at col 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\dev\astropy\astropy\units\core.py", line 2045, in __call__
raise ValueError("'parse_strict' must be 'warn', "
ValueError: 'parse_strict' must be 'warn', 'raise' or 'silent' |
The fix does not seem to be trivial, I don't have a working solution. |
I have proposed a fix at #13042 but it comes with some behavior change. I dunno if it is good or bad. Please have a look and discuss. Thanks! |
Turns out the bug we uncovered here was reported already years ago: #6302 |
astropy.io.votable.exceptions.W50
is defined to be the warning for units that do not adhere to the VO standards: https://www.ivoa.net/documents/VOUnitsyet, astropy.units.format.vounit.VOUnit issues a generic
UnitsWarning
for the exact same reason.This is somewhat unexpected in practice, and while in the example below we could ignore both, I don't think that's the right approach:
astropy/astroquery#2352
The text was updated successfully, but these errors were encountered: