astropy: Can't import Representation class twice (from different packages/telescopes)

I try to import two functions from two packages of two telescopes (CTA / FACT). Both define a PlanarRepresentation class internally.

Error:

    438         if repr_name in REPRESENTATION_CLASSES:
    439             raise ValueError("Representation class {0} already defined"
--> 440                              .format(repr_name))
    441 
    442         REPRESENTATION_CLASSES[repr_name] = cls

ValueError: Representation class planar already defined

MWE without the packages, but same Error, basically just redefining the Representation class:

from astropy.coordinates import BaseRepresentation

class PlanarRepresentation(BaseRepresentation): 
     attr_classes = ['a', 'b'] 
     def __init__(self, x, y, copy=True, **kwargs): 
         pass

class PlanarRepresentation(BaseRepresentation): 
     attr_classes = ['a', 'b'] 
     def __init__(self, x, y, copy=True, **kwargs): 
         pass

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 28 (23 by maintainers)

Commits related to this issue

Most upvoted comments

The use case is arguably mostly laziness: if you want to initialize or convert, you don’t have to import the relevant class, but just give its name.

So, indeed, changing the name of your class would resolve it, though I think that longer term it still seems reasonable to move the class up to astropy.

Could you explain why or when this would be useful?

Well, it certainly is not something we can change any more; it is even in the Getting Started section on coordinates; http://docs.astropy.org/en/latest/coordinates/index.html#representation

p.s. @noahhdf - thanks for raising the issues!

delete the entry in astropy.coordinates.representation.REPRESENTATION_CLASSES, and import the next package

thanks @mhvk, this did the trick for me

1  from astropy.coordinates.representation import REPRESENTATION_CLASSES
2  from fact.analysis.source import calc_off_position
3  REPRESENTATION_CLASSES.pop("planar")
4  from ctapipe.coordinates import CameraFrame

Theoretically, you can let the latter silently (or with warning) overwrite the former. But I am not sure if that is more dangerous than it’s worth.