Skip to content

Commit

Permalink
give nicer deprecation / message on infer_dtype moving
Browse files Browse the repository at this point in the history
  • Loading branch information
jreback committed Apr 3, 2017
1 parent ab4525b commit e5fbdc8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
3 changes: 2 additions & 1 deletion pandas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@

json = _DeprecatedModule(deprmod='pandas.json', deprmodto='pandas.io.json.libjson')
parser = _DeprecatedModule(deprmod='pandas.parser', deprmodto='pandas.io.libparsers')
lib = _DeprecatedModule(deprmod='pandas.lib', deprmodto='pandas._libs.lib')
lib = _DeprecatedModule(deprmod='pandas.lib', deprmodto='pandas._libs.lib',
moved={'infer_dtype': 'pandas.api.lib.infer_dtype'})
tslib = _DeprecatedModule(deprmod='pandas.tslib', deprmodto='pandas._libs.tslib')

# use the closest tagged version if possible
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class TestLib(tm.TestCase):

def test_deprecation_access_func(self):
with catch_warnings(record=True):
pd.lib.infer_dtype
pd.lib.infer_dtype('foo')


class TestTSLib(tm.TestCase):
Expand Down
11 changes: 5 additions & 6 deletions pandas/tests/api/test_lib.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# -*- coding: utf-8 -*-

import pytest
from warnings import catch_warnings
import pandas # noqa


@pytest.mark.parametrize('f', ['infer_dtype'])
def test_importable(f):
from pandas.api import lib
e = getattr(lib, f)
assert e is not None
def test_moved_infer_dtype():
with catch_warnings(record=True):
e = pandas.lib.infer_dtype('foo')
assert e is not None
15 changes: 14 additions & 1 deletion pandas/util/depr_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ class _DeprecatedModule(object):
be used when needed.
removals : objects or methods in module that will no longer be
accessible once module is removed.
moved : dict, optional
dictionary of function name -> new location for moved
objects
"""

def __init__(self, deprmod, deprmodto=None, removals=None):
def __init__(self, deprmod, deprmodto=None, removals=None,
moved=None):
self.deprmod = deprmod
self.deprmodto = deprmodto
self.removals = removals
if self.removals is not None:
self.removals = frozenset(self.removals)
self.moved = moved

# For introspection purposes.
self.self_dir = frozenset(dir(self.__class__))
Expand Down Expand Up @@ -60,6 +65,14 @@ def __getattr__(self, name):
"{deprmod}.{name} is deprecated and will be removed in "
"a future version.".format(deprmod=self.deprmod, name=name),
FutureWarning, stacklevel=2)
elif self.moved is not None and name in self.moved:
warnings.warn(
"{deprmod} is deprecated and will be removed in "
"a future version.\nYou can access {name} in {moved}".format(
deprmod=self.deprmod,
name=name,
moved=self.moved[name]),
FutureWarning, stacklevel=2)
else:
deprmodto = self.deprmodto
if deprmodto is None:
Expand Down

0 comments on commit e5fbdc8

Please sign in to comment.