Skip to content

Commit 9554195

Browse files
gfyoungjorisvandenbossche
authored andcommitted
MAINT: Use __module__ in _DeprecatedModule. (#14181)
Follow-up to gh-14105. Uses the '__module__' method to correctly determine the location of the alternative module to use.
1 parent cebc70c commit 9554195

File tree

2 files changed

+24
-41
lines changed

2 files changed

+24
-41
lines changed

pandas/core/api.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,12 @@
3131
# see gh-14094.
3232
from pandas.util.depr_module import _DeprecatedModule
3333

34-
_alts = ['pandas.tseries.tools', 'pandas.tseries.offsets',
35-
'pandas.tseries.frequencies']
3634
_removals = ['day', 'bday', 'businessDay', 'cday', 'customBusinessDay',
3735
'customBusinessMonthEnd', 'customBusinessMonthBegin',
3836
'monthEnd', 'yearEnd', 'yearBegin', 'bmonthEnd', 'bmonthBegin',
3937
'cbmonthEnd', 'cbmonthBegin', 'bquarterEnd', 'quarterEnd',
4038
'byearEnd', 'week']
41-
datetools = _DeprecatedModule(deprmod='pandas.core.datetools', alts=_alts,
39+
datetools = _DeprecatedModule(deprmod='pandas.core.datetools',
4240
removals=_removals)
4341

4442
from pandas.core.config import (get_option, set_option, reset_option,

pandas/util/depr_module.py

+23-38
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,11 @@ class _DeprecatedModule(object):
1313
Parameters
1414
----------
1515
deprmod : name of module to be deprecated.
16-
alts : alternative modules to be used to access objects or methods
17-
available in module.
1816
removals : objects or methods in module that will no longer be
1917
accessible once module is removed.
2018
"""
21-
def __init__(self, deprmod, alts=None, removals=None):
19+
def __init__(self, deprmod, removals=None):
2220
self.deprmod = deprmod
23-
24-
self.alts = alts
25-
if self.alts is not None:
26-
self.alts = frozenset(self.alts)
27-
2821
self.removals = removals
2922
if self.removals is not None:
3023
self.removals = frozenset(self.removals)
@@ -33,47 +26,39 @@ def __init__(self, deprmod, alts=None, removals=None):
3326
self.self_dir = frozenset(dir(self.__class__))
3427

3528
def __dir__(self):
36-
_dir = object.__dir__(self)
37-
38-
if self.removals is not None:
39-
_dir.extend(list(self.removals))
29+
deprmodule = self._import_deprmod()
30+
return dir(deprmodule)
4031

41-
if self.alts is not None:
42-
for modname in self.alts:
43-
module = importlib.import_module(modname)
44-
_dir.extend(dir(module))
32+
def __repr__(self):
33+
deprmodule = self._import_deprmod()
34+
return repr(deprmodule)
4535

46-
return _dir
36+
__str__ = __repr__
4737

4838
def __getattr__(self, name):
4939
if name in self.self_dir:
5040
return object.__getattribute__(self, name)
5141

52-
if self.removals is not None and name in self.removals:
53-
with warnings.catch_warnings():
54-
warnings.filterwarnings('ignore', category=FutureWarning)
55-
module = importlib.import_module(self.deprmod)
42+
deprmodule = self._import_deprmod()
43+
obj = getattr(deprmodule, name)
5644

45+
if self.removals is not None and name in self.removals:
5746
warnings.warn(
5847
"{deprmod}.{name} is deprecated and will be removed in "
5948
"a future version.".format(deprmod=self.deprmod, name=name),
6049
FutureWarning, stacklevel=2)
50+
else:
51+
# The object is actually located in another module.
52+
warnings.warn(
53+
"{deprmod}.{name} is deprecated. Please use "
54+
"{modname}.{name} instead.".format(
55+
deprmod=self.deprmod, modname=obj.__module__, name=name),
56+
FutureWarning, stacklevel=2)
6157

62-
return object.__getattribute__(module, name)
63-
64-
if self.alts is not None:
65-
for modname in self.alts:
66-
module = importlib.import_module(modname)
67-
68-
if hasattr(module, name):
69-
warnings.warn(
70-
"{deprmod}.{name} is deprecated. Please use "
71-
"{modname}.{name} instead.".format(
72-
deprmod=self.deprmod, modname=modname, name=name),
73-
FutureWarning, stacklevel=2)
74-
75-
return getattr(module, name)
58+
return obj
7659

77-
raise AttributeError("module '{deprmod}' has no attribute "
78-
"'{name}'".format(deprmod=self.deprmod,
79-
name=name))
60+
def _import_deprmod(self):
61+
with warnings.catch_warnings():
62+
warnings.filterwarnings('ignore', category=FutureWarning)
63+
deprmodule = importlib.import_module(self.deprmod)
64+
return deprmodule

0 commit comments

Comments
 (0)