Skip to content

Commit

Permalink
deprecate override__dir__
Browse files Browse the repository at this point in the history
Signed-off-by: Nathaniel Starkman (@nstarman) <nstarkman@protonmail.com>
  • Loading branch information
nstarman committed Sep 10, 2022
1 parent 8f3f54b commit 3528641
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 30 deletions.
9 changes: 4 additions & 5 deletions astropy/coordinates/baseframe.py
Expand Up @@ -15,7 +15,6 @@
import numpy as np

# Project
from astropy.utils.compat.misc import override__dir__
from astropy.utils.decorators import lazyproperty, format_doc
from astropy.utils.exceptions import AstropyWarning, AstropyDeprecationWarning
from astropy import units as u
Expand Down Expand Up @@ -1575,18 +1574,18 @@ def __setitem__(self, item, value):

self.cache.clear()

@override__dir__
def __dir__(self):
def __dir__(self) -> list[str]:
"""
Override the builtin `dir` behavior to include representation
names.
TODO: dynamic representation transforms (i.e. include cylindrical et al.).
"""
dir_values = set(self.representation_component_names)
dir_values = set(super().__dir__())
dir_values |= set(self.representation_component_names)
dir_values |= set(self.get_representation_component_names('s'))

return dir_values
return sorted(dir_values)

def __getattr__(self, attr):
"""
Expand Down
8 changes: 3 additions & 5 deletions astropy/coordinates/sky_coordinate.py
Expand Up @@ -6,7 +6,6 @@
import numpy as np
import erfa

from astropy.utils.compat.misc import override__dir__
from astropy import units as u
from astropy.constants import c as speed_of_light
from astropy.utils.data_info import MixinInfo
Expand Down Expand Up @@ -911,16 +910,15 @@ def __delattr__(self, attr):
# Otherwise, do the standard Python attribute setting
super().__delattr__(attr)

@override__dir__
def __dir__(self):
def __dir__(self) -> list[str]:
"""
Override the builtin `dir` behavior to include:
- Transforms available by aliases
- Attribute / methods of the underlying self.frame object
"""
dir_values = set(super().__dir__())

# determine the aliases that this can be transformed to.
dir_values = set()
for name in frame_transform_graph.get_names():
frame_cls = frame_transform_graph.lookup_name(name)
if self.frame.is_transformable_to(frame_cls):
Expand All @@ -932,7 +930,7 @@ def __dir__(self):
# Add all possible frame attributes
dir_values.update(frame_transform_graph.frame_attributes.keys())

return dir_values
return sorted(dir_values)

def __repr__(self):
clsnm = self.__class__.__name__
Expand Down
9 changes: 3 additions & 6 deletions astropy/time/core.py
Expand Up @@ -21,7 +21,6 @@
from astropy import units as u, constants as const
from astropy.units import UnitConversionError
from astropy.utils import ShapedLikeNDArray
from astropy.utils.compat.misc import override__dir__
from astropy.utils.data_info import MixinInfo, data_info_factory
from astropy.utils.exceptions import AstropyDeprecationWarning, AstropyWarning
from .utils import day_frac
Expand Down Expand Up @@ -1401,11 +1400,9 @@ def __getattr__(self, attr):
# Should raise AttributeError
return self.__getattribute__(attr)

@override__dir__
def __dir__(self):
result = set(self.SCALES)
result.update(self.FORMATS)
return result
def __dir__(self) -> list[str]:
dir_values = set(super().__dir__()) | set(self.SCALES) | set(self.FORMATS)
return sorted(dir_values)

def _match_shape(self, val):
"""
Expand Down
16 changes: 7 additions & 9 deletions astropy/units/quantity.py
Expand Up @@ -18,7 +18,6 @@
# LOCAL
from astropy import config as _config
from astropy.utils.compat import NUMPY_LT_1_20, NUMPY_LT_1_22
from astropy.utils.compat.misc import override__dir__
from astropy.utils.data_info import ParentDtypeInfo
from astropy.utils.exceptions import AstropyDeprecationWarning, AstropyWarning
from astropy.utils.misc import isiterable
Expand Down Expand Up @@ -1004,21 +1003,20 @@ def isscalar(self):
# Quantity, such as `astropy.coordinates.Angle`.
_include_easy_conversion_members = False

@override__dir__
def __dir__(self):
def __dir__(self) -> list[str]:
"""
Quantities are able to directly convert to other units that
have the same physical type. This function is implemented in
order to make autocompletion still work correctly in IPython.
"""
if not self._include_easy_conversion_members:
return []
extra_members = set()
return super().__dir__()

dir_values = set(super().__dir__())
equivalencies = Unit._normalize_equivalencies(self.equivalencies)
for equivalent in self.unit._get_units_with_same_physical_type(
equivalencies):
extra_members.update(equivalent.names)
return extra_members
for equivalent in self.unit._get_units_with_same_physical_type(equivalencies):
dir_values.update(equivalent.names)
return sorted(dir_values)

def __getattr__(self, attr):
"""
Expand Down
29 changes: 24 additions & 5 deletions astropy/utils/compat/misc.py
Expand Up @@ -9,6 +9,7 @@
import sys
import functools

from astropy.utils.decorators import deprecated


__all__ = ['override__dir__', 'possible_filename']
Expand All @@ -34,13 +35,19 @@ def possible_filename(filename):
return False


@deprecated(
since="v5.2",
message="http://bugs.python.org/issue12166 is resolved. See docstring for alternatives."
)
def override__dir__(f):
"""
When overriding a __dir__ method on an object, you often want to
include the "standard" members on the object as well. This
decorator takes care of that automatically, and all the wrapped
function needs to do is return a list of the "special" members
that wouldn't be found by the normal Python means.
When overriding a __dir__ method on an object, you often want to include the
"standard" members on the object as well. This decorator takes care of that
automatically, and all the wrapped function needs to do is return a list of
the "special" members that wouldn't be found by the normal Python means.
.. deprecated:: v5.2
Use ``sorted(super().__dir__() + ...)`` instead.
Example
-------
Expand All @@ -50,6 +57,18 @@ def override__dir__(f):
@override__dir__
def __dir__(self):
return ['special_method1', 'special_method2']
Notes
-----
This function was introduced because of http://bugs.python.org/issue12166,
which has since been resolved by
http://hg.python.org/cpython/rev/8f403199f999. Now, the best way to
customize ``__dir__`` is to use ``super``.
::
def __dir__(self):
added = ['special_method1', 'special_method2'] return
sorted(super().__dir__() + added)
"""
# http://bugs.python.org/issue12166

Expand Down
4 changes: 4 additions & 0 deletions docs/changes/utils/13636.api.rst
@@ -0,0 +1,4 @@
``astropy.utils.misc.suppress`` is removed, use ``contextlib.suppress`` instead.
``astropy.utils.namedtuple_asdict`` is removed, use method ``._asdict`` on a
named-tuple. ``override__dir__`` is deprecated and will be removed in a future
version, see the docstring for the better alternative.

0 comments on commit 3528641

Please sign in to comment.