Skip to content

Commit 8d9689f

Browse files
committed
COMPAT: Catch warnings on tab-complete in IPy 6
Properties may run code with Jedi completion in IPython 6 Closes #16409
1 parent 6614e26 commit 8d9689f

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

doc/source/whatsnew/v0.20.2.txt

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ Bug Fixes
3838

3939
- Bug in using ``pathlib.Path`` or ``py.path.local`` objects with io functions (:issue:`16291`)
4040

41+
42+
- Fixed a compatibility issue with IPython 6.0's tab completion showing deprecation warnings on Categoricals (:issue:`16409`)
43+
44+
4145
Conversion
4246
^^^^^^^^^^
4347

pandas/conftest.py

+10
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,13 @@ def spmatrix(request):
4545
tm._skip_if_no_scipy()
4646
from scipy import sparse
4747
return getattr(sparse, request.param + '_matrix')
48+
49+
50+
@pytest.fixture
51+
def ip():
52+
"""An instance of IPython.InteractiveShell.
53+
Will raise a skip if IPython is not installed.
54+
"""
55+
pytest.importorskip('IPython', minversion="6.0.0")
56+
from IPython.core.interactiveshell import InteractiveShell
57+
return InteractiveShell()

pandas/core/categorical.py

+7
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,13 @@ def __init__(self, values, categories=None, ordered=False, fastpath=False):
342342
self._categories = categories
343343
self._codes = coerce_indexer_dtype(codes, categories)
344344

345+
def __dir__(self):
346+
# Avoid IPython warnings for deprecated properties
347+
# https://github.com/pandas-dev/pandas/issues/16409
348+
rv = set(dir(type(self)))
349+
rv.discard("labels")
350+
return sorted(rv)
351+
345352
@property
346353
def _constructor(self):
347354
return Categorical

pandas/tests/test_categorical.py

+14
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,20 @@ def test_unicode_print(self):
736736

737737
assert _rep(c) == expected
738738

739+
def test_tab_complete_warning(self, ip):
740+
# https://github.com/pandas-dev/pandas/issues/16409
741+
pytest.importorskip('IPython', minversion="6.0.0")
742+
from IPython.core.interactiveshell import InteractiveShell
743+
from IPython.core.completer import provisionalcompleter
744+
745+
code = "import pandas as pd; c = pd.Categorical([])"
746+
ip.run_code(code)
747+
with pytest.warns(None) as record:
748+
with provisionalcompleter('ignore'):
749+
list(ip.Completer.completions('c.', 1))
750+
751+
assert len(record) == 0
752+
739753
def test_periodindex(self):
740754
idx1 = PeriodIndex(['2014-01', '2014-01', '2014-02', '2014-02',
741755
'2014-03', '2014-03'], freq='M')

pandas/tests/test_resample.py

+16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from warnings import catch_warnings
44
from datetime import datetime, timedelta
55
from functools import partial
6+
from textwrap import dedent
67

78
import pytz
89
import pytest
@@ -2816,6 +2817,21 @@ def test_back_compat_v180(self):
28162817
expected = df.groupby('A').resample('4s').mean().ffill()
28172818
assert_frame_equal(result, expected)
28182819

2820+
@pytest.mark.xfail
2821+
def test_tab_complete_ipython6(self, ip):
2822+
from IPython.core.completer import provisionalcompleter
2823+
code = dedent("""\
2824+
import pandas.util.testing as tm
2825+
s = tm.makeTimeSeries()
2826+
rs = s.resample("D")
2827+
""")
2828+
ip.run_code(code)
2829+
2830+
with pytest.warns(None) as record:
2831+
with provisionalcompleter('ignore'):
2832+
list(ip.Completer.completions('rs.', 1))
2833+
assert len(record) == 0
2834+
28192835
def test_deferred_with_groupby(self):
28202836

28212837
# GH 12486

0 commit comments

Comments
 (0)