Skip to content
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

Chained exception for invalid unit string? #7710

Open
cdeil opened this issue Aug 6, 2018 · 1 comment
Open

Chained exception for invalid unit string? #7710

cdeil opened this issue Aug 6, 2018 · 1 comment

Comments

@cdeil
Copy link
Member

cdeil commented Aug 6, 2018

I was surprised how complex the exception is when trying to create a unit for an invalid string.

Would it be possible / make sense to simplify this and present a non-chained exception to the user in that case?

This is with Astropy 3.0.3 on Python 3.6, I did not check other versions.

>>> import astropy.units as u
>>> u.Unit('spam')
Traceback (most recent call last):
  File "/Users/deil/software/anaconda3/envs/gammapy-dev/lib/python3.6/site-packages/astropy/units/format/generic.py", line 482, in _do_parse
    return cls._parse_unit(s, detailed_exception=False)
  File "/Users/deil/software/anaconda3/envs/gammapy-dev/lib/python3.6/site-packages/astropy/units/format/generic.py", line 462, in _parse_unit
    raise ValueError()
ValueError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/deil/software/anaconda3/envs/gammapy-dev/lib/python3.6/site-packages/astropy/units/format/generic.py", line 443, in _get_unit
    return cls._parse_unit(t.value)
  File "/Users/deil/software/anaconda3/envs/gammapy-dev/lib/python3.6/site-packages/astropy/units/format/generic.py", line 460, in _parse_unit
    s, did_you_mean(s, registry)))
ValueError: spam is not a valid unit. 

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/deil/software/anaconda3/envs/gammapy-dev/lib/python3.6/site-packages/astropy/units/core.py", line 1784, in __call__
    return f.parse(s)
  File "/Users/deil/software/anaconda3/envs/gammapy-dev/lib/python3.6/site-packages/astropy/units/format/generic.py", line 469, in parse
    result = cls._do_parse(s, debug=debug)
  File "/Users/deil/software/anaconda3/envs/gammapy-dev/lib/python3.6/site-packages/astropy/units/format/generic.py", line 485, in _do_parse
    return cls._parser.parse(s, lexer=cls._lexer, debug=debug)
  File "/Users/deil/software/anaconda3/envs/gammapy-dev/lib/python3.6/site-packages/astropy/extern/ply/yacc.py", line 331, in parse
    return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
  File "/Users/deil/software/anaconda3/envs/gammapy-dev/lib/python3.6/site-packages/astropy/extern/ply/yacc.py", line 1061, in parseopt_notrack
    lookahead = get_token()     # Get the next token
  File "/Users/deil/software/anaconda3/envs/gammapy-dev/lib/python3.6/site-packages/astropy/extern/ply/lex.py", line 350, in token
    newtok = func(tok)
  File "/Users/deil/software/anaconda3/envs/gammapy-dev/lib/python3.6/site-packages/astropy/units/format/generic.py", line 151, in t_UNIT
    t.value = cls._get_unit(t)
  File "/Users/deil/software/anaconda3/envs/gammapy-dev/lib/python3.6/site-packages/astropy/units/format/generic.py", line 447, in _get_unit
    t.lexpos, str(e)))
ValueError: At col 0, spam is not a valid unit. 

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/deil/software/anaconda3/envs/gammapy-dev/lib/python3.6/site-packages/astropy/units/core.py", line 1798, in __call__
    raise ValueError(msg)
ValueError: 'spam' did not parse as unit: At col 0, spam is not a valid unit. 
@bsipocz bsipocz added the units label Aug 6, 2018
@pllim pllim added the question label Aug 6, 2018
@pllim
Copy link
Member

pllim commented Aug 6, 2018

Chaining is still seen in 3.1.dev22297. cc @mhvk

MHvK: Still there in 2021. Should really fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants