Skip to content

Commit

Permalink
COMPAT: Catch warnings on tab-complete in IPy 6 (pandas-dev#16414)
Browse files Browse the repository at this point in the history
Properties may run code with Jedi completion in IPython 6

Closes pandas-dev#16409
  • Loading branch information
TomAugspurger authored and stangirala committed Jun 11, 2017
1 parent 02c87dd commit 880108b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
4 changes: 4 additions & 0 deletions doc/source/whatsnew/v0.20.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ Bug Fixes
- Bug in ``DataFrame.update()`` with ``overwrite=False`` and ``NaN values`` (:issue:`15593`)



- Fixed a compatibility issue with IPython 6.0's tab completion showing deprecation warnings on Categoricals (:issue:`16409`)


Conversion
^^^^^^^^^^

Expand Down
10 changes: 10 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,13 @@ def spmatrix(request):
tm._skip_if_no_scipy()
from scipy import sparse
return getattr(sparse, request.param + '_matrix')


@pytest.fixture
def ip():
"""An instance of IPython.InteractiveShell.
Will raise a skip if IPython is not installed.
"""
pytest.importorskip('IPython', minversion="6.0.0")
from IPython.core.interactiveshell import InteractiveShell
return InteractiveShell()
7 changes: 7 additions & 0 deletions pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,13 @@ def __init__(self, values, categories=None, ordered=False, fastpath=False):
self._categories = categories
self._codes = coerce_indexer_dtype(codes, categories)

def __dir__(self):
# Avoid IPython warnings for deprecated properties
# https://github.com/pandas-dev/pandas/issues/16409
rv = set(dir(type(self)))
rv.discard("labels")
return sorted(rv)

@property
def _constructor(self):
return Categorical
Expand Down
6 changes: 6 additions & 0 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ def __getattr__(self, attr):
matches_pattern = any(attr.startswith(x) for x
in self._deprecated_valid_patterns)
if not matches_pattern and attr not in self._deprecated_valids:
# avoid the warning, if it's just going to be an exception
# anyway.
if not hasattr(self.obj, attr):
raise AttributeError("'{}' has no attribute '{}'".format(
type(self.obj).__name__, attr
))
self = self._deprecated(attr)

return object.__getattribute__(self, attr)
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,17 @@ def test_unicode_print(self):

assert _rep(c) == expected

def test_tab_complete_warning(self, ip):
# https://github.com/pandas-dev/pandas/issues/16409
pytest.importorskip('IPython', minversion="6.0.0")
from IPython.core.completer import provisionalcompleter

code = "import pandas as pd; c = pd.Categorical([])"
ip.run_code(code)
with tm.assert_produces_warning(None):
with provisionalcompleter('ignore'):
list(ip.Completer.completions('c.', 1))

def test_periodindex(self):
idx1 = PeriodIndex(['2014-01', '2014-01', '2014-02', '2014-02',
'2014-03', '2014-03'], freq='M')
Expand Down
17 changes: 15 additions & 2 deletions pandas/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from warnings import catch_warnings
from datetime import datetime, timedelta
from functools import partial
from textwrap import dedent

import pytz
import pytest
Expand Down Expand Up @@ -284,8 +285,7 @@ def test_attribute_access(self):
tm.assert_series_equal(r.A.sum(), r['A'].sum())

# getting
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
pytest.raises(AttributeError, lambda: r.F)
pytest.raises(AttributeError, lambda: r.F)

# setting
def f():
Expand Down Expand Up @@ -2816,6 +2816,19 @@ def test_back_compat_v180(self):
expected = df.groupby('A').resample('4s').mean().ffill()
assert_frame_equal(result, expected)

def test_tab_complete_ipython6_warning(self, ip):
from IPython.core.completer import provisionalcompleter
code = dedent("""\
import pandas.util.testing as tm
s = tm.makeTimeSeries()
rs = s.resample("D")
""")
ip.run_code(code)

with tm.assert_produces_warning(None):
with provisionalcompleter('ignore'):
list(ip.Completer.completions('rs.', 1))

def test_deferred_with_groupby(self):

# GH 12486
Expand Down

0 comments on commit 880108b

Please sign in to comment.