question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unit roundtrips do not always work

See original GitHub issue

Description

Not sure whether this is an issue, however it would probably nice to have it fixed:

Expected behavior

I would expect that any roundtrip for unicode format works. i.e. for all units unit:

format='unicode'
assert u.Unit(unit.to_string(format=format), format=format) == unit

should work. I would expect the same for the other formatters; at least for generic and fits.

Actual behavior

For Angstrom, it works when not giving unicode as format in the u.Unit() constructor, but not when this is given:

>>> u.Unit(u.Angstrom.to_string(format='unicode')) == u.Angstrom
True
>>> u.Unit(u.Angstrom.to_string(format='unicode'), format='unicode') == u.Angstrom
[…]
ValueError: 'Å' did not parse as unicode unit: Can not parse Unicode If this […]

The second exception comes for any unit; unicode seems not be supported as parsing format.

For the solar mass, the first one does not work as well:

>>>  u.Unit(u.solMass.to_string(format='unicode')) == u.solMass
[…]
ValueError: 'M⊙' did not parse as unit: At col 0, M is not a valid unit. Did you […]

I would assume that

  • the “unicode” parse format is identical to the “generic” format (as we usually use unicode in strings)
  • Unicode output is also parseable, as the generic input

System Details

The tests were done with Astropy 4.2

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
olebolecommented, Jun 9, 2021

Finding them is simple:

import astropy.units as u
import inspect

format='unicode'

print(f'|  Type        |  Generic     |  Unicode     | ')
print(f'| ------------ | ------------ | ------------ | ')

units = set(unit for name, unit in inspect.getmembers(u)
            if isinstance(unit, u.Unit)
            and not isinstance(unit, u.core.PrefixUnit))

for unit in units:
    formatted_name = unit.to_string(format=format)
    try:
        res = u.Unit(formatted_name) == unit
    except ValueError:
        res = False
    if not res:
        print(f'| {unit.physical_type:12s} | {unit.to_string():12s} | {formatted_name:12s} | ')

This shows all unique but the prefixed units that raise the error:

Type Generic Unicode
mass M_e Mₑ
power solLum L⊙
length jupiterRad R♃
mass M_p Mₚ
length earthRad R⊕
mass earthMass M⊕
angle arcsec
volume l
mass solMass M⊙
mass jupiterMass M♃
angle hourangle ʰ
energy Ry R∞
angle arcmin
length solRad R⊙

Looks not overwhelming to me. Maybe I will have a look myself, if this is agreed to be useful…

0reactions
mhvkcommented, Jun 9, 2021

OK, I made a fix to the parsing issue, see #11827.

For the parsing with ‘unicode’, it needs a better error message indeed, which I think is easier to deal with as a separate issue, so I raised #11828.

@maxnoe - indeed, you are right, we are now free to try to implement the module __getattr__ idea!

Read more comments on GitHub >

github_iconTop Results From Across the Web

One Way vs Round Trip Ticket - Issues? - Air Travel Forum
A seat is a seat is a seat, right? One of them should incur less cost than two, correct? Maybe not half as...
Read more >
What is round-trip time? | RTT definition - Cloudflare
It's important to keep in mind that round-trip time is an estimate and not a guarantee; the pathway between the two locations can...
Read more >
What is a "fare component" when buying an airline ticket?
A round trip consists of two components; one outbound and one return. The fare for each component is the same. If different fares...
Read more >
Pricing Diagnostics/Fare Construction Theory
In domestic U.S. fare selection, Worldspan always uses Headline fares. This may or may not be the actual direction of travel. If domestic...
Read more >
What is Round Trip Time (RTT) | Behind the Ping | CDN Guide
Round trip time is a key factor for measuring network latency and page load times. Find out what influences RTT and how CDNs...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found