Skip to content

Compat for util.testing import #30973

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 6 commits into from
Jan 13, 2020
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
18 changes: 18 additions & 0 deletions pandas/tests/api/test_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import subprocess
import sys
from typing import List

import pytest

import pandas as pd
from pandas import api, compat
import pandas._testing as tm
Expand Down Expand Up @@ -311,3 +314,18 @@ def test_util_testing_deprecated_direct(self):

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

def test_util_in_top_level(self):
# in a subprocess to avoid import caching issues
out = subprocess.check_output(
[
sys.executable,
"-c",
"import pandas; pandas.util.testing.assert_series_equal",
],
stderr=subprocess.STDOUT,
).decode()
assert "pandas.util.testing is deprecated" in out

with pytest.raises(AttributeError, match="foo"):
pd.util.foo
27 changes: 27 additions & 0 deletions pandas/util/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
from pandas.util._decorators import Appender, Substitution, cache_readonly # noqa

from pandas import compat
from pandas.core.util.hashing import hash_array, hash_pandas_object # noqa

# compatibility for import pandas; pandas.util.testing
Copy link
Contributor

Choose a reason for hiding this comment

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

in the future pls put the issue numbers here with a comment to make removals easier.

when this code is read in the future, it will be very hard for anyone not part of the original conversation to grok what is happening here (easily).


if compat.PY37:

def __getattr__(name):
if name == "testing":
import pandas.util.testing

return pandas.util.testing
else:
raise AttributeError(f"module 'pandas.util' has no attribute '{name}'")


else:

class _testing:
def __getattr__(self, item):
import pandas.util.testing

return getattr(pandas.util.testing, item)

testing = _testing()


del compat