astropy: Unit parser only issues warning rather than throwing an exception for completely invalid string when using "vounit" format

Description

The following warning prints when I define a unit in “vounit” format with a non-sensical string:

WARNING: UnitsWarning: Unit 'bad' not supported by the VOUnit standard. Did you mean Ba (deprecated), aD, ad or g.cm**-1.s**-2? [astropy.units.format.vounit]

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

from astropy.units import Unit
Unit("bad", format="vounit", parse_strict="raise")  # This should raise an exception but does not.

Versions

In [20]: astropy.__version__
Out[20]: '5.3.4'

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (13 by maintainers)

Most upvoted comments

There’s clearly tension between

  • flexibly dealing with units that users find in datasets in the wild (as well as avoiding avalanches of nuisance warning messages); and
  • assisting data publishers in rigorously verifying that their published data follows applicable standards.

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 parse_strict option is the tool we should be using to do this.

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.

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 "bad" is a valid unit as far as VOUnit is concerned. You can look up the details and references in the issues and pull request I mentioned.

Still a problem in astropy v6.1.dev . Looks like this call went here:

https://github.com/astropy/astropy/blob/92a96e111bd80f3d53d08f51049eefef69af287a/astropy/units/format/generic.py#L588

That invokes parse in here:

https://github.com/astropy/astropy/blob/92a96e111bd80f3d53d08f51049eefef69af287a/astropy/utils/parsing.py#L107

This line calls an external library (astropy.extern.ply.yacc.LRParser) that somehow bypasses Unit error handling:

https://github.com/astropy/astropy/blob/92a96e111bd80f3d53d08f51049eefef69af287a/astropy/utils/parsing.py#L120

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 parse_strict:

>>> vounit.VOUnit._parser.parser.parse?
Signature:
vounit.VOUnit._parser.parser.parse(
    input=None,
    lexer=None,
    debug=False,
    tracking=False,
    tokenfunc=None,
)
Docstring: <no docstring>
File:      astropy/extern/ply/yacc.py
Type:      method

If the except is not catching ValueError here:

https://github.com/astropy/astropy/blob/92a96e111bd80f3d53d08f51049eefef69af287a/astropy/units/format/generic.py#L586

I think it would have properly respected parse_strict. So there is a bug but not clear to me what is a proper way to fix this.

vounit.VOUnit._parse_unit("bad")  # ValueError

Thanks for the report!