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
- Allow representations with same name, fixes #8509 Fallback to `__qualname__` to avoid naming conflicts in case there are multiple representations with the same name. — committed to maxnoe/astropy by maxnoe 5 years ago
- Allow representations with same name, fixes #8509 Fallback to `__qualname__` to avoid naming conflicts in case there are multiple representations with the same name. — committed to maxnoe/astropy by maxnoe 5 years ago
- Allow representations with same name, fixes #8509 Fallback to `__qualname__` to avoid naming conflicts in case there are multiple representations with the same name. — committed to maxnoe/astropy by maxnoe 5 years ago
- Merge pull request #8561 from MaxNoe/fix_representation_name_conflict Allow representations with same name, fixes #8509 — committed to astropy/astropy by bsipocz 5 years ago
- Allow representations with same name, fixes #8509 Fallback to `__qualname__` to avoid naming conflicts in case there are multiple representations with the same name. — committed to sadielbartholomew/astropy by maxnoe 5 years ago
- Allow representations with same name, fixes #8509 Fallback to `__qualname__` to avoid naming conflicts in case there are multiple representations with the same name. — committed to maxnoe/astropy by maxnoe 5 years ago
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.
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!
thanks @mhvk, this did the trick for me
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.