Skip to content

Commit 75efd8a

Browse files
committed
COMPAT: Catch warnings on tab-complete in IPy 6 (pandas-dev#16414)
Properties may run code with Jedi completion in IPython 6 Closes pandas-dev#16409 (cherry picked from commit 3c9a74b)
1 parent 61503ea commit 75efd8a

File tree

6 files changed

+53
-2
lines changed

6 files changed

+53
-2
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/core/resample.py

+6
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ def __getattr__(self, attr):
184184
matches_pattern = any(attr.startswith(x) for x
185185
in self._deprecated_valid_patterns)
186186
if not matches_pattern and attr not in self._deprecated_valids:
187+
# avoid the warning, if it's just going to be an exception
188+
# anyway.
189+
if not hasattr(self.obj, attr):
190+
raise AttributeError("'{}' has no attribute '{}'".format(
191+
type(self.obj).__name__, attr
192+
))
187193
self = self._deprecated(attr)
188194

189195
return object.__getattribute__(self, attr)

pandas/tests/test_categorical.py

+11
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,17 @@ def test_unicode_print(self):
737737

738738
assert _rep(c) == expected
739739

740+
def test_tab_complete_warning(self, ip):
741+
# https://github.com/pandas-dev/pandas/issues/16409
742+
pytest.importorskip('IPython', minversion="6.0.0")
743+
from IPython.core.completer import provisionalcompleter
744+
745+
code = "import pandas as pd; c = pd.Categorical([])"
746+
ip.run_code(code)
747+
with tm.assert_produces_warning(None):
748+
with provisionalcompleter('ignore'):
749+
list(ip.Completer.completions('c.', 1))
750+
740751
def test_periodindex(self):
741752
idx1 = PeriodIndex(['2014-01', '2014-01', '2014-02', '2014-02',
742753
'2014-03', '2014-03'], freq='M')

pandas/tests/test_resample.py

+15-2
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 pytest
89
import numpy as np
@@ -282,8 +283,7 @@ def test_attribute_access(self):
282283
tm.assert_series_equal(r.A.sum(), r['A'].sum())
283284

284285
# getting
285-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
286-
pytest.raises(AttributeError, lambda: r.F)
286+
pytest.raises(AttributeError, lambda: r.F)
287287

288288
# setting
289289
def f():
@@ -2820,6 +2820,19 @@ def test_back_compat_v180(self):
28202820
expected = df.groupby('A').resample('4s').mean().ffill()
28212821
assert_frame_equal(result, expected)
28222822

2823+
def test_tab_complete_ipython6_warning(self, ip):
2824+
from IPython.core.completer import provisionalcompleter
2825+
code = dedent("""\
2826+
import pandas.util.testing as tm
2827+
s = tm.makeTimeSeries()
2828+
rs = s.resample("D")
2829+
""")
2830+
ip.run_code(code)
2831+
2832+
with tm.assert_produces_warning(None):
2833+
with provisionalcompleter('ignore'):
2834+
list(ip.Completer.completions('rs.', 1))
2835+
28232836
def test_deferred_with_groupby(self):
28242837

28252838
# GH 12486

0 commit comments

Comments
 (0)