astropy: AttributeError: Longitude instance has no attribute 'radian' (but only from pytest 🤷 ) with cds.enabled()
Description
I have a piece of code that crashes unexpectedly during a pytest session and works fine in a separate IPython session,
swgo_plot/plots/sky.py:447: in plot_sky_areas
lon[k] = sky_coords[k].geocentrictrueecliptic.lon.deg
../../mambaforge/envs/swgo-plot/lib/python3.9/site-packages/astropy/coordinates/sky_coordinate.py:893: in __getattr__
return self.transform_to(attr)
../../mambaforge/envs/swgo-plot/lib/python3.9/site-packages/astropy/coordinates/sky_coordinate.py:702: in transform_to
new_coord = trans(self.frame, generic_frame)
../../mambaforge/envs/swgo-plot/lib/python3.9/site-packages/astropy/coordinates/transformations.py:1579: in __call__
curr_coord = t(curr_coord, curr_toframe)
../../mambaforge/envs/swgo-plot/lib/python3.9/site-packages/astropy/coordinates/transformations.py:1145: in __call__
return supcall(fromcoord, toframe)
../../mambaforge/envs/swgo-plot/lib/python3.9/site-packages/astropy/coordinates/builtin_frames/icrs_cirs_transforms.py:124: in icrs_to_gcrs
gcrs_ra, gcrs_dec = atciqz(srepr.without_differentials(), astrom)
../../mambaforge/envs/swgo-plot/lib/python3.9/site-packages/astropy/coordinates/builtin_frames/utils.py:297: in atciqz
pco = erfa.s2c(srepr.lon.radian, srepr.lat.radian)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <Longitude 175. deg>, attr = 'radian'
def __getattr__(self, attr):
"""
Quantities are able to directly convert to other units that
have the same physical type.
"""
if not self._include_easy_conversion_members:
raise AttributeError(
f"'{self.__class__.__name__}' object has no '{attr}' member"
)
def get_virtual_unit_attribute():
registry = get_current_unit_registry().registry
to_unit = registry.get(attr, None)
if to_unit is None:
return None
try:
return self.unit.to(
to_unit, self.value, equivalencies=self.equivalencies
)
except UnitsError:
return None
value = get_virtual_unit_attribute()
if value is None:
> raise AttributeError(
f"{self.__class__.__name__} instance has no attribute '{attr}'"
)
E AttributeError: Longitude instance has no attribute 'radian'
../../mambaforge/envs/swgo-plot/lib/python3.9/site-packages/astropy/units/quantity.py:1102: AttributeError
Expected behavior
It should work the same in any environment if the package version is the same
How to Reproduce
From a pdb session triggered by pytest I get that the triggering line is the following
-> lon[k] = sky_coords[k].geocentrictrueecliptic.lon.deg
(Pdb) k
0
(Pdb) sky_coords[k]
<SkyCoord (ICRS): (ra, dec) in deg
(175., -0.5)>
indeed if (still inside pytest/pdb) I try to perform the transformation I get,
(Pdb) sky_coords[k].geocentrictrueecliptic *** AttributeError: Longitude instance has no attribute 'radian'
To be sure I try to replicate the issue from scratch in a separate IPython session,
In [1]: from astropy import units as u
...: from astropy.coordinates import SkyCoord
...: c = SkyCoord(ra=10.625*u.degree, dec=41.2*u.degree, frame='icrs')
In [2]: c.geocentrictrueecliptic
Out[2]:
<SkyCoord (GeocentricTrueEcliptic: equinox=J2000.000, obstime=J2000.000): (lon, lat, distance) in (deg, deg, )
(27.76134106, 33.31188749, 1.)>
In [3]: c.geocentrictrueecliptic.lon.deg
Out[3]: 27.761341057229142
Versions
macOS-11.7.4-x86_64-i386-64bit Python 3.9.13 | packaged by conda-forge | (main, May 27 2022, 17:01:00) [Clang 13.0.1 ] astropy 5.2.1 Numpy 1.23.4 pyerfa 2.0.0.1 Scipy 1.9.3 Matplotlib 3.6.2 pytest 7.2.0
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 34 (17 by maintainers)
Commits related to this issue
- Enable CDS units in the class constructor See https://github.com/astropy/astropy/issues/14580 — committed to cta-observatory/pycorsikaio by deleted user a year ago
- Always convert longitudes and latitudes to rad see https://github.com/astropy/astropy/issues/14580 — committed to HealthyPear/astropy by deleted user a year ago
I’d vote for using the more limited API (probably faster) of
lon.to_value(u.rad)
, which should always work too, and doesn’t rely on the fact thatAngle
tries to interpret unknown attributes as units.@HealthyPear , thanks for the update. The only way to find out is to open a draft PR with your changes and see if CI gets green. 😉 Thanks!
For a moment it worked, though, I swear! 🤣
Indeed, a fresh env doesn’t give me this problem…now I need to understand where is the difference…