Skip to content

Commit 880108b

Browse files
TomAugspurgerstangirala
authored andcommitted
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
1 parent 02c87dd commit 880108b

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
@@ -42,6 +42,10 @@ Bug Fixes
4242
- Bug in ``DataFrame.update()`` with ``overwrite=False`` and ``NaN values`` (:issue:`15593`)
4343

4444

45+
46+
- Fixed a compatibility issue with IPython 6.0's tab completion showing deprecation warnings on Categoricals (:issue:`16409`)
47+
48+
4549
Conversion
4650
^^^^^^^^^^
4751

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
@@ -736,6 +736,17 @@ 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.completer import provisionalcompleter
743+
744+
code = "import pandas as pd; c = pd.Categorical([])"
745+
ip.run_code(code)
746+
with tm.assert_produces_warning(None):
747+
with provisionalcompleter('ignore'):
748+
list(ip.Completer.completions('c.', 1))
749+
739750
def test_periodindex(self):
740751
idx1 = PeriodIndex(['2014-01', '2014-01', '2014-02', '2014-02',
741752
'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 pytz
89
import pytest
@@ -284,8 +285,7 @@ def test_attribute_access(self):
284285
tm.assert_series_equal(r.A.sum(), r['A'].sum())
285286

286287
# getting
287-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
288-
pytest.raises(AttributeError, lambda: r.F)
288+
pytest.raises(AttributeError, lambda: r.F)
289289

290290
# setting
291291
def f():
@@ -2816,6 +2816,19 @@ def test_back_compat_v180(self):
28162816
expected = df.groupby('A').resample('4s').mean().ffill()
28172817
assert_frame_equal(result, expected)
28182818

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

28212834
# GH 12486

0 commit comments

Comments
 (0)