Skip to content

SkyCoord subclass property masks underlying AttributeError #116

@rowan-stein

Description

@rowan-stein

User request

Subclassed SkyCoord gives misleading attribute access message. When a subclass property attempts to access a non-existent attribute, the error message claims the property itself does not exist instead of pointing to the missing inner attribute.

Reproducer:

import astropy.coordinates as coord

class custom_coord(coord.SkyCoord):
    @property
    def prop(self):
        return self.random_attr

c = custom_coord('00h42m30s', '+41d12m00s', frame='icrs')
c.prop

Observed error (example):

AttributeError: 'custom_coord' object has no attribute 'prop'

Researcher specification

Summary:

  • SkyCoord.getattr currently raises a standardized AttributeError for unknown attributes. When a subclass-defined descriptor/property exists but its getter raises AttributeError, Python falls back to getattr, which then masks the original exception and reports the property name as missing.

Fix:

  • In SkyCoord.getattr, first perform a static attribute existence check using inspect.getattr_static(self, attr). If the attribute exists on the class/object (descriptor/property/method), delegate to object.getattribute(self, attr) so any AttributeError from the getter is propagated unchanged. Otherwise, proceed with the existing SkyCoord dynamic behavior (frame aliases, frame attributes, underlying frame delegation, and standardized AttributeError for truly missing attributes).

Tests:

  • Add tests under astropy/coordinates/tests/ that:
    1. Verify that a SkyCoord subclass property whose getter accesses a missing attribute raises an AttributeError mentioning the missing inner attribute (e.g., 'random_attr'), not the property name ('prop').
    2. Confirm transform alias access (e.g., c.icrs) remains unaffected.
    3. Confirm frame attribute delegation (e.g., c.obstime) remains unaffected.

Compatibility:

  • No API changes; fix narrows behavior to preserve the original exceptions from subclass-defined descriptors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions