Skip to content

Commit 608f177

Browse files
committed
Compat for util.testing import
Closes pandas-dev#30869
1 parent dd6e31a commit 608f177

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

pandas/tests/api/test_api.py

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import sys
22
from typing import List
33

4+
import pytest
5+
46
import pandas as pd
57
from pandas import api, compat
68
import pandas._testing as tm
@@ -311,3 +313,16 @@ def test_util_testing_deprecated_direct(self):
311313

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

pandas/util/__init__.py

+25
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
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 # noqa: F401
13+
else:
14+
raise AttributeError(f"module 'pandas.util' has no attribute '{name}'")
15+
16+
17+
else:
18+
19+
class _testing:
20+
def __getattr__(self, item):
21+
import pandas.util.testing
22+
23+
return getattr(pandas.util.testing, item)
24+
25+
testing = _testing()
26+
27+
28+
del compat

0 commit comments

Comments
 (0)