Skip to content

DEPR/REGR: Fix pandas.util.testing deprecation #30745

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 4 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 14 additions & 7 deletions pandas/tests/api/test_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from typing import List

import pandas as pd
Expand Down Expand Up @@ -288,14 +289,20 @@ def test_testing(self):
self.check(testing, self.funcs)

def test_util_testing_deprecated(self):
s = pd.Series([], dtype="object")
with tm.assert_produces_warning(FutureWarning) as m:
import pandas.util.testing as tm2
# avoid cache state affecting the test
sys.modules.pop("pandas.util.testing", None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in theory this should be in a context manager (but nbd)


tm2.assert_series_equal(s, s)
with tm.assert_produces_warning(FutureWarning) as m:
import pandas.util.testing # noqa: F401

assert "pandas.testing.assert_series_equal" in str(m[0].message)
assert "pandas.util.testing is deprecated" in str(m[0].message)
assert "pandas.testing instead" in str(m[0].message)

def test_util_testing_deprecated_direct(self):
# avoid cache state affecting the test
sys.modules.pop("pandas.util.testing", None)
with tm.assert_produces_warning(FutureWarning) as m:
tm2.DataFrame
assert "removed" in str(m[0].message)
from pandas.util.testing import assert_series_equal # noqa: F401

assert "pandas.util.testing is deprecated" in str(m[0].message)
assert "pandas.testing instead" in str(m[0].message)
62 changes: 59 additions & 3 deletions pandas/util/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,60 @@
from pandas.util._decorators import Appender, Substitution, cache_readonly # noqa
import importlib as _importlib
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

umm why don't you actually just leave pandas.util.testing as a module itself? (and just re-import what you need). This is how we did all of the original deprecations.

from importlib.abc import Loader as _Loader, MetaPathFinder
from importlib.machinery import ModuleSpec as _ModuleSpec
import sys as _sys
import warnings as _warnings

from pandas.core.util.hashing import hash_array, hash_pandas_object # noqa
from pandas.util.testing import testing # noqa: F401
from pandas.util._decorators import Appender, Substitution, cache_readonly

from pandas.core.util.hashing import hash_array, hash_pandas_object

# Custom import hook for the deprecated pandas.util.testing module.
# The custom Finder only runs when python's standard import fail,
# since we're adding to the end of sys.meta_path. In TestingLoader.create_module
# we simply warn and then return the real testing module, pandas._testing.
# This differs from _DeprecatedModule by using the import system, which
# allows it to work with both `import foo; foo.bar` and `from foo import bar`.
# But because Python caches imports, the warning appears only once.


class _TestingFinder(MetaPathFinder):
@classmethod
def find_spec(cls, fullname, path=None, target=None):

name_parts = fullname.split(".")
if name_parts[:3] != ["pandas", "util", "testing"] or len(name_parts) > 3:
return None
else:
# return ModuleSpec(fullname, DataPackageImporter())
return _ModuleSpec(fullname, _TestingLoader())


class _TestingLoader(_Loader):
@classmethod
def create_module(cls, spec):
module = _importlib.import_module("pandas._testing")
_warnings.warn(
(
"pandas.util.testing is deprecated. Use the "
"public methods from pandas.testing instead."
),
FutureWarning,
stacklevel=2,
)
return module

@classmethod
def exec_module(cls, module):
pass


_sys.meta_path.append(_TestingFinder())


__all__ = [
"Appender",
"Substitution",
"cache_readonly",
"hash_array",
"hash_pandas_object",
]
144 changes: 0 additions & 144 deletions pandas/util/testing/__init__.py

This file was deleted.