-
- Notifications
You must be signed in to change notification settings - Fork 19.4k
STY: use pytest.raises context manager (indexes) #25447
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
Changes from 3 commits
577bb83 fb01e00 2835ac7 5c407b3 8135de8 f4311b5 5c7e024 3c0f7d3 File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -4,14 +4,15 @@ | |
| from datetime import datetime, timedelta | ||
| import math | ||
| import operator | ||
| import re | ||
| import sys | ||
| | ||
| import numpy as np | ||
| import pytest | ||
| | ||
| from pandas._libs.tslib import Timestamp | ||
| from pandas.compat import ( | ||
| PY3, PY35, PY36, StringIO, lrange, lzip, range, text_type, u, zip) | ||
| PY2, PY3, PY35, PY36, StringIO, lrange, lzip, range, text_type, u, zip) | ||
| from pandas.compat.numpy import np_datetime64_compat | ||
| | ||
| from pandas.core.dtypes.common import is_unsigned_integer_dtype | ||
| | @@ -107,7 +108,10 @@ def test_constructor_copy(self): | |
| | ||
| def test_constructor_corner(self): | ||
| # corner case | ||
| pytest.raises(TypeError, Index, 0) | ||
| msg = (r"Index\(\.\.\.\) must be called with a collection of some" | ||
| " kind, 0 was passed") | ||
| with pytest.raises(TypeError, match=msg): | ||
| Index(0) | ||
| | ||
| @pytest.mark.parametrize("index_vals", [ | ||
| [('A', 1), 'B'], ['B', ('A', 1)]]) | ||
| | @@ -487,6 +491,7 @@ def test_constructor_cast(self): | |
| with pytest.raises(ValueError, match=msg): | ||
| Index(["a", "b", "c"], dtype=float) | ||
| | ||
| @pytest.mark.skipif(PY2, reason="pytest.raises match regex fails") | ||
| def test_view_with_args(self): | ||
| Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. did you mean to just remove this test completely? (and replace with one below) Member Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the original test has been split | ||
| | ||
| restricted = ['unicodeIndex', 'strIndex', 'catIndex', 'boolIndex', | ||
| | @@ -496,7 +501,12 @@ def test_view_with_args(self): | |
| ind = self.indices[i] | ||
| | ||
| # with arguments | ||
| pytest.raises(TypeError, lambda: ind.view('i8')) | ||
| if i == 'catIndex': | ||
| msg = r"view\(\) takes 1 positional argument but 2 were given" | ||
| ||
| else: | ||
| msg = "Cannot change data-type for object array" | ||
| with pytest.raises(TypeError, match=msg): | ||
| ind.view('i8') | ||
| | ||
| # these are ok | ||
| for i in list(set(self.indices.keys()) - set(restricted)): | ||
| | @@ -565,7 +575,8 @@ def test_delete(self, pos, expected): | |
| | ||
| def test_delete_raises(self): | ||
| index = Index(['a', 'b', 'c', 'd'], name='index') | ||
| with pytest.raises((IndexError, ValueError)): | ||
| msg = "index 5 is out of bounds for axis 0 with size 4" | ||
| with pytest.raises((IndexError, ValueError), match=msg): | ||
| # either depending on numpy version | ||
| ||
| index.delete(5) | ||
| | ||
| | @@ -683,7 +694,9 @@ def test_empty_fancy_raises(self, attr): | |
| | ||
| assert index[[]].identical(empty_index) | ||
| # np.ndarray only accepts ndarray of int & bool dtypes, so should Index | ||
| pytest.raises(IndexError, index.__getitem__, empty_farr) | ||
| msg = r"arrays used as indices must be of integer \(or boolean\) type" | ||
| with pytest.raises(IndexError, match=msg): | ||
| index[empty_farr] | ||
| | ||
| @pytest.mark.parametrize("sort", [None, False]) | ||
| def test_intersection(self, sort): | ||
| | @@ -1426,13 +1439,14 @@ def test_get_indexer_strings(self, method, expected): | |
| def test_get_indexer_strings_raises(self): | ||
| index = pd.Index(['b', 'c']) | ||
| | ||
| with pytest.raises(TypeError): | ||
| msg = r"unsupported operand type\(s\) for -: 'str' and 'str'" | ||
| with pytest.raises(TypeError, match=msg): | ||
| index.get_indexer(['a', 'b', 'c', 'd'], method='nearest') | ||
| | ||
| with pytest.raises(TypeError): | ||
| with pytest.raises(TypeError, match=msg): | ||
| index.get_indexer(['a', 'b', 'c', 'd'], method='pad', tolerance=2) | ||
| | ||
| with pytest.raises(TypeError): | ||
| with pytest.raises(TypeError, match=msg): | ||
| index.get_indexer(['a', 'b', 'c', 'd'], method='pad', | ||
| tolerance=[2, 2, 2, 2]) | ||
| | ||
| | @@ -1685,8 +1699,11 @@ def test_drop_tuple(self, values, to_drop): | |
| tm.assert_index_equal(result, expected) | ||
| | ||
| removed = index.drop(to_drop[1]) | ||
| msg = r"\"\[{}\] not found in axis\"".format( | ||
| re.escape(to_drop[1].__repr__())) | ||
| for drop_me in to_drop[1], [to_drop[1]]: | ||
| pytest.raises(KeyError, removed.drop, drop_me) | ||
| with pytest.raises(KeyError, match=msg): | ||
| removed.drop(drop_me) | ||
| | ||
| @pytest.mark.parametrize("method,expected,sort", [ | ||
| ('intersection', np.array([(1, 'A'), (2, 'A'), (1, 'B'), (2, 'B')], | ||
| | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a huge deal but we won't maintain PY2 support for the 0.25 release, so this will most likely subsequently be removed anyway
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's right @WillAyd , we are just skipping for now and will remove this for 0.25, see #23922 (comment)