Skip to content

MAINT: Use __module__ in _DeprecatedModule. #14181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions pandas/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@
# see gh-14094.
from pandas.util.depr_module import _DeprecatedModule

_alts = ['pandas.tseries.tools', 'pandas.tseries.offsets',
'pandas.tseries.frequencies']
_removals = ['day', 'bday', 'businessDay', 'cday', 'customBusinessDay',
'customBusinessMonthEnd', 'customBusinessMonthBegin',
'monthEnd', 'yearEnd', 'yearBegin', 'bmonthEnd', 'bmonthBegin',
'cbmonthEnd', 'cbmonthBegin', 'bquarterEnd', 'quarterEnd',
'byearEnd', 'week']
datetools = _DeprecatedModule(deprmod='pandas.core.datetools', alts=_alts,
datetools = _DeprecatedModule(deprmod='pandas.core.datetools',
removals=_removals)

from pandas.core.config import (get_option, set_option, reset_option,
Expand Down
61 changes: 23 additions & 38 deletions pandas/util/depr_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,11 @@ class _DeprecatedModule(object):
Parameters
----------
deprmod : name of module to be deprecated.
alts : alternative modules to be used to access objects or methods
available in module.
removals : objects or methods in module that will no longer be
accessible once module is removed.
"""
def __init__(self, deprmod, alts=None, removals=None):
def __init__(self, deprmod, removals=None):
self.deprmod = deprmod

self.alts = alts
if self.alts is not None:
self.alts = frozenset(self.alts)

self.removals = removals
if self.removals is not None:
self.removals = frozenset(self.removals)
Expand All @@ -33,47 +26,39 @@ def __init__(self, deprmod, alts=None, removals=None):
self.self_dir = frozenset(dir(self.__class__))

def __dir__(self):
_dir = object.__dir__(self)

if self.removals is not None:
_dir.extend(list(self.removals))
deprmodule = self._import_deprmod()
return dir(deprmodule)

if self.alts is not None:
for modname in self.alts:
module = importlib.import_module(modname)
_dir.extend(dir(module))
def __repr__(self):
deprmodule = self._import_deprmod()
return repr(deprmodule)

return _dir
__str__ = __repr__

def __getattr__(self, name):
if name in self.self_dir:
return object.__getattribute__(self, name)

if self.removals is not None and name in self.removals:
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=FutureWarning)
module = importlib.import_module(self.deprmod)
deprmodule = self._import_deprmod()
obj = getattr(deprmodule, name)

if self.removals is not None and name in self.removals:
warnings.warn(
"{deprmod}.{name} is deprecated and will be removed in "
"a future version.".format(deprmod=self.deprmod, name=name),
FutureWarning, stacklevel=2)
else:
# The object is actually located in another module.
warnings.warn(
"{deprmod}.{name} is deprecated. Please use "
"{modname}.{name} instead.".format(
deprmod=self.deprmod, modname=obj.__module__, name=name),
FutureWarning, stacklevel=2)

return object.__getattribute__(module, name)

if self.alts is not None:
for modname in self.alts:
module = importlib.import_module(modname)

if hasattr(module, name):
warnings.warn(
"{deprmod}.{name} is deprecated. Please use "
"{modname}.{name} instead.".format(
deprmod=self.deprmod, modname=modname, name=name),
FutureWarning, stacklevel=2)

return getattr(module, name)
return obj

raise AttributeError("module '{deprmod}' has no attribute "
"'{name}'".format(deprmod=self.deprmod,
name=name))
def _import_deprmod(self):
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=FutureWarning)
deprmodule = importlib.import_module(self.deprmod)
return deprmodule