Skip to content

Commit

Permalink
MAINT: Use __module__ in _DeprecatedModule.
Browse files Browse the repository at this point in the history
Follow-up to pandas-devgh-14105. Uses the '__module__' method
to correctly determine the location of the alternative
module to use.
  • Loading branch information
gfyoung committed Sep 9, 2016
1 parent 02df7b6 commit 2e581ab
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 41 deletions.
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

0 comments on commit 2e581ab

Please sign in to comment.