Skip to content

Commit 2bf0c9f

Browse files
Compat for util.testing import (#30973)
* Compat for util.testing import Closes #30869
1 parent 6928327 commit 2bf0c9f

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

pandas/tests/api/test_api.py

+18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import subprocess
12
import sys
23
from typing import List
34

5+
import pytest
6+
47
import pandas as pd
58
from pandas import api, compat
69
import pandas._testing as tm
@@ -311,3 +314,18 @@ def test_util_testing_deprecated_direct(self):
311314

312315
assert "pandas.util.testing is deprecated" in str(m[0].message)
313316
assert "pandas.testing instead" in str(m[0].message)
317+
318+
def test_util_in_top_level(self):
319+
# in a subprocess to avoid import caching issues
320+
out = subprocess.check_output(
321+
[
322+
sys.executable,
323+
"-c",
324+
"import pandas; pandas.util.testing.assert_series_equal",
325+
],
326+
stderr=subprocess.STDOUT,
327+
).decode()
328+
assert "pandas.util.testing is deprecated" in out
329+
330+
with pytest.raises(AttributeError, match="foo"):
331+
pd.util.foo

pandas/util/__init__.py

+27
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
11
from pandas.util._decorators import Appender, Substitution, cache_readonly # noqa
22

3+
from pandas import compat
34
from pandas.core.util.hashing import hash_array, hash_pandas_object # noqa
5+
6+
# compatibility for import pandas; pandas.util.testing
7+
8+
if compat.PY37:
9+
10+
def __getattr__(name):
11+
if name == "testing":
12+
import pandas.util.testing
13+
14+
return pandas.util.testing
15+
else:
16+
raise AttributeError(f"module 'pandas.util' has no attribute '{name}'")
17+
18+
19+
else:
20+
21+
class _testing:
22+
def __getattr__(self, item):
23+
import pandas.util.testing
24+
25+
return getattr(pandas.util.testing, item)
26+
27+
testing = _testing()
28+
29+
30+
del compat

0 commit comments

Comments
 (0)