Skip to content

COMPAT: Catch warnings on tab-complete in IPy 6 #16414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -40,6 +40,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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hah didn't know you could do this. pretty cool.

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