astropy: Creating new dB units fail with `def_unit`

In your documentation, you demonstrate that a new Unit definition can be made as such: >>> kmph = u.def_unit('kmph', u.km / u.h)

So, I tried something similar: In [2]: a = units.def_unit('dBW', units.dB(units.W))

However, instead of making a new units, I received this error instead:

ValueError                                Traceback (most recent call last)
<ipython-input-28-90e4cc6e9112> in <module>()
----> 1 a = units.def_unit('dBW', units.dB(units.W))

***/python2.7/site-packages/astropy/units/core.pyc in def_unit(s, represents, doc,
format, prefixes, exclude_prefixes, namespace)
   2290     if represents is not None:
   2291         result = Unit(s, represents, namespace=namespace, doc=doc,
-> 2292                       format=format)
   2293     else:
   2294         result = IrreducibleUnit(

***/python2.7/site-packages/astropy/units/core.pyc in __call__(self, s, represents,
 format, namespace, doc, parse_strict)
   1834                            .format(s, format_clause, six.text_type(e)))
   1835                     if parse_strict == 'raise':
-> 1836                         raise ValueError(msg)
   1837                     elif parse_strict == 'warn':
   1838                         warnings.warn(msg, UnitsWarning)

ValueError: 'dBW' did not parse as unit: At col 0, dBW is not a valid unit. Did you mean dB, dW or db?

This is probably a bug that should be fixed.

Note

The fix for this issue also have to remove the known issue entry added in:

About this issue

  • Original URL
  • State: open
  • Created 10 years ago
  • Comments: 17 (15 by maintainers)

Commits related to this issue

Most upvoted comments

I wanted to check when the change was introduced and bisected it down to 73232e8b62887ce9cb7139a1a4edd64d10568fac from #17399 (unreleased), so we have the option of reverting that PR before we decide to call it a known issue. Ping @eerovaher

Actually, it indeed doesn’t give a failure any more, but it also doesn’t produce the right unit:

a = u.def_unit('dBW', u.dB(u.W))
type(a)
# astropy.units.core.Unit
b = u.Unit("dB(W)")
type(b)
# astropy.units.function.logarithmic.DecibelUnit

Also,

1*a
TypeError: unsupported operand type(s) for *: 'int' and 'Unit'
1*b
<Decibel 1. dB(W)>

So, in a way it is worse than before…

Yes, and yes. Having a test for a known issue feels messy in principle but practically this appears to be needed.

in a way it is worse than before

Uh oh… should we at least document this in https://docs.astropy.org/en/latest/known_issues.html until a solution is found?