Skip to content

Commit 7b6d0b0

Browse files
committed
REF: Change how pandas.core.common shim works
I have a PR incoming that would introduce a circular dependency between pandas.core.arrays.categorical, pandas.api, and pandas.core.common. This change will allow that PR to avoid hacky workarounds, since the real problem is pandas.core importing pandas.api.types. xref pandas-dev#13990
1 parent ca2d261 commit 7b6d0b0

File tree

2 files changed

+52
-43
lines changed

2 files changed

+52
-43
lines changed

pandas/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
from pandas.io.api import *
5959
from pandas.util._tester import test
6060
import pandas.testing
61+
from pandas.core.common import _add_deprecated
62+
_add_deprecated()
63+
del _add_deprecated
6164

6265
# extension module deprecations
6366
from pandas.util._depr_module import _DeprecatedModule

pandas/core/common.py

+49-43
Original file line numberDiff line numberDiff line change
@@ -19,56 +19,61 @@
1919
from pandas.core.dtypes.common import _NS_DTYPE
2020
from pandas.core.dtypes.inference import _iterable_not_string
2121
from pandas.core.dtypes.missing import isna, isnull, notnull # noqa
22-
from pandas.api import types
2322
from pandas.core.dtypes import common
2423
from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
2524

2625
# compat
2726
from pandas.errors import ( # noqa
2827
PerformanceWarning, UnsupportedFunctionCall, UnsortedIndexError)
2928

30-
# back-compat of public API
31-
# deprecate these functions
32-
m = sys.modules['pandas.core.common']
33-
for t in [t for t in dir(types) if not t.startswith('_')]:
34-
35-
def outer(t=t):
36-
37-
def wrapper(*args, **kwargs):
38-
warnings.warn("pandas.core.common.{t} is deprecated. "
39-
"import from the public API: "
40-
"pandas.api.types.{t} instead".format(t=t),
41-
DeprecationWarning, stacklevel=3)
42-
return getattr(types, t)(*args, **kwargs)
43-
return wrapper
44-
45-
setattr(m, t, outer(t))
46-
47-
# back-compat for non-public functions
48-
# deprecate these functions
49-
for t in ['is_datetime_arraylike',
50-
'is_datetime_or_timedelta_dtype',
51-
'is_datetimelike',
52-
'is_datetimelike_v_numeric',
53-
'is_datetimelike_v_object',
54-
'is_datetimetz',
55-
'is_int_or_datetime_dtype',
56-
'is_period_arraylike',
57-
'is_string_like',
58-
'is_string_like_dtype']:
59-
60-
def outer(t=t):
61-
62-
def wrapper(*args, **kwargs):
63-
warnings.warn("pandas.core.common.{t} is deprecated. "
64-
"These are not longer public API functions, "
65-
"but can be imported from "
66-
"pandas.api.types.{t} instead".format(t=t),
67-
DeprecationWarning, stacklevel=3)
68-
return getattr(common, t)(*args, **kwargs)
69-
return wrapper
70-
71-
setattr(m, t, outer(t))
29+
30+
def _add_deprecated():
31+
# back-compat of public API
32+
# deprecate these functions
33+
# This is called at the end of pandas.__init__, after all the imports
34+
# have already been resolved, to avoid any circular imports.
35+
from pandas.api import types
36+
37+
m = sys.modules['pandas.core.common']
38+
for t in [t for t in dir(types) if not t.startswith('_')]:
39+
40+
def outer(t=t):
41+
42+
def wrapper(*args, **kwargs):
43+
warnings.warn("pandas.core.common.{t} is deprecated. "
44+
"import from the public API: "
45+
"pandas.api.types.{t} instead".format(t=t),
46+
DeprecationWarning, stacklevel=3)
47+
return getattr(types, t)(*args, **kwargs)
48+
return wrapper
49+
50+
setattr(m, t, outer(t))
51+
52+
# back-compat for non-public functions
53+
# deprecate these functions
54+
for t in ['is_datetime_arraylike',
55+
'is_datetime_or_timedelta_dtype',
56+
'is_datetimelike',
57+
'is_datetimelike_v_numeric',
58+
'is_datetimelike_v_object',
59+
'is_datetimetz',
60+
'is_int_or_datetime_dtype',
61+
'is_period_arraylike',
62+
'is_string_like',
63+
'is_string_like_dtype']:
64+
65+
def outer(t=t):
66+
67+
def wrapper(*args, **kwargs):
68+
warnings.warn("pandas.core.common.{t} is deprecated. "
69+
"These are not longer public API functions, "
70+
"but can be imported from "
71+
"pandas.api.types.{t} instead".format(t=t),
72+
DeprecationWarning, stacklevel=3)
73+
return getattr(common, t)(*args, **kwargs)
74+
return wrapper
75+
76+
setattr(m, t, outer(t))
7277

7378

7479
# deprecate array_equivalent
@@ -646,6 +651,7 @@ def _random_state(state=None):
646651
-------
647652
np.random.RandomState
648653
"""
654+
from pandas.api import types
649655

650656
if types.is_integer(state):
651657
return np.random.RandomState(state)

0 commit comments

Comments
 (0)