Skip to content

Cannot use negative distance in spherical representation despite error message #11104

Closed

Description

@astrojuanlu
>>> from astropy.coordinates import SphericalRepresentation
>>> from astropy import units as u
>>> SphericalRepresentation(10 * u.deg, 45 * u.deg, 10 * u.m)
<SphericalRepresentation (lon, lat, distance) in (deg, deg, m)
    (10., 45., 10.)>
>>> SphericalRepresentation(10 * u.deg, 45 * u.deg, -10 * u.m)
Traceback (most recent call last):
  File "/home/juanlu/Personal/Astropy/astropy/astropy/coordinates/representation.py", line 1840, in __init__
    self._distance = Distance(self._distance, copy=False)
  File "/home/juanlu/Personal/Astropy/astropy/astropy/coordinates/distances.py", line 187, in __new__
    raise ValueError("Distance must be >= 0.  Use the argument "
ValueError: Distance must be >= 0.  Use the argument 'allow_negative=True' to allow negative values.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/juanlu/Personal/Astropy/astropy/astropy/coordinates/representation.py", line 1843, in __init__
    raise ValueError("Distance must be >= 0. To allow negative "
ValueError: Distance must be >= 0. To allow negative distance values, you must explicitly pass in a `Distance` object with the the argument 'allow_negative=True'.
>>> from astropy.coordinates import Distance
>>> SphericalRepresentation(10 * u.deg, 45 * u.deg, Distance(-10 * u.m, allow_negative=True))
Traceback (most recent call last):
  File "/home/juanlu/Personal/Astropy/astropy/astropy/coordinates/representation.py", line 1840, in __init__
    self._distance = Distance(self._distance, copy=False)
  File "/home/juanlu/Personal/Astropy/astropy/astropy/coordinates/distances.py", line 187, in __new__
    raise ValueError("Distance must be >= 0.  Use the argument "
ValueError: Distance must be >= 0.  Use the argument 'allow_negative=True' to allow negative values.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/juanlu/Personal/Astropy/astropy/astropy/coordinates/representation.py", line 1843, in __init__
    raise ValueError("Distance must be >= 0. To allow negative "
ValueError: Distance must be >= 0. To allow negative distance values, you must explicitly pass in a `Distance` object with the the argument 'allow_negative=True'.

Activity

  1. mhvk commented on Dec 1, 2020

    @mhvk
    Contributor

    I actually wonder if we shouldn't just get rid of the whole check... Why is length special? I can see it for frames or sky coordinates but not for representations. See #11114.

    But probably best not to let the perfect stand in the way of the good enough - a direct fix is in #11113.

  2. adrn commented on Dec 1, 2020

    @adrn
    Member

    Just a note to say that this looks like a duplicate of #8091?

  3. mhvk commented on Dec 1, 2020

    @mhvk
    Contributor

    Indeed, and I see my comments are quite similar to what I'm now suggesting in #11114... I'll note in #11113 that it fixes #8091 as well...

  4. astrojuanlu commented on Dec 1, 2020

    @astrojuanlu
    MemberAuthor

    Oops, didn't look for similar issues before opening this one. Closing as duplicate, thanks folks!

  5. added a commit that references this issue on Dec 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions