-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
STY: use pytest.raises context syntax (series) #24812
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f50b3fd
STY: use pytest.raises context syntax (series)
simonjayhawkins 4f8b393
Merge remote-tracking branch 'upstream/master' into cm-series
simonjayhawkins 9e06dd3
fix ci errors except DID NOT RAISE
simonjayhawkins ddc69b4
troubleshoot DID NOT RAISE
simonjayhawkins eae318a
revert changes to failing test
simonjayhawkins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,8 +65,10 @@ def test_constructor(self, datetime_series, empty_series): | |
|
||
assert not empty_series.index.is_all_dates | ||
assert not Series({}).index.is_all_dates | ||
pytest.raises(Exception, Series, np.random.randn(3, 3), | ||
index=np.arange(3)) | ||
|
||
# exception raised is of type Exception | ||
with pytest.raises(Exception, match="Data must be 1-dimensional"): | ||
Series(np.random.randn(3, 3), index=np.arange(3)) | ||
|
||
mixed.name = 'Series' | ||
rs = Series(mixed).name | ||
|
@@ -75,7 +77,9 @@ def test_constructor(self, datetime_series, empty_series): | |
|
||
# raise on MultiIndex GH4187 | ||
m = MultiIndex.from_arrays([[1, 2], [3, 4]]) | ||
pytest.raises(NotImplementedError, Series, m) | ||
msg = "initializing a Series from a MultiIndex is not supported" | ||
with pytest.raises(NotImplementedError, match=msg): | ||
Series(m) | ||
|
||
@pytest.mark.parametrize('input_class', [list, dict, OrderedDict]) | ||
def test_constructor_empty(self, input_class): | ||
|
@@ -495,7 +499,9 @@ def test_constructor_broadcast_list(self): | |
# GH 19342 | ||
# construction with single-element container and index | ||
# should raise | ||
pytest.raises(ValueError, Series, ['foo'], index=['a', 'b', 'c']) | ||
msg = "Length of passed values is 1, index implies 3" | ||
with pytest.raises(ValueError, match=msg): | ||
Series(['foo'], index=['a', 'b', 'c']) | ||
|
||
def test_constructor_corner(self): | ||
df = tm.makeTimeDataFrame() | ||
|
@@ -675,10 +681,17 @@ def test_constructor_dtype_datetime64(self): | |
assert s.dtype == 'M8[ns]' | ||
|
||
# GH3414 related | ||
# msg = (r"cannot astype a datetimelike from \[datetime64\[ns\]\] to" | ||
# r" \[int32\]") | ||
# with pytest.raises(TypeError, match=msg): | ||
# Series(Series(dates).astype('int') / 1000000, dtype='M8[ms]') | ||
pytest.raises(TypeError, lambda x: Series( | ||
Series(dates).astype('int') / 1000000, dtype='M8[ms]')) | ||
pytest.raises(TypeError, | ||
lambda x: Series(dates, dtype='datetime64')) | ||
|
||
msg = (r"The 'datetime64' dtype has no unit\. Please pass in" | ||
r" 'datetime64\[ns\]' instead\.") | ||
with pytest.raises(ValueError, match=msg): | ||
Series(dates, dtype='datetime64') | ||
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. i think this test is also not failing as intended and was masked by another lambda error. will do a follow-up PR to address theses cases. |
||
|
||
# invalid dates can be help as object | ||
result = Series([datetime(2, 1, 1)]) | ||
|
@@ -984,9 +997,11 @@ def test_constructor_dict_of_tuples(self): | |
|
||
def test_constructor_set(self): | ||
values = {1, 2, 3, 4, 5} | ||
pytest.raises(TypeError, Series, values) | ||
with pytest.raises(TypeError, match="'set' type is unordered"): | ||
Series(values) | ||
values = frozenset(values) | ||
pytest.raises(TypeError, Series, values) | ||
with pytest.raises(TypeError, match="'frozenset' type is unordered"): | ||
Series(values) | ||
|
||
# https://github.com/pandas-dev/pandas/issues/22698 | ||
@pytest.mark.filterwarnings("ignore:elementwise comparison:FutureWarning") | ||
|
@@ -1081,14 +1096,16 @@ def test_constructor_dtype_timedelta64(self): | |
td.astype('int64') | ||
|
||
# invalid casting | ||
pytest.raises(TypeError, td.astype, 'int32') | ||
msg = (r"cannot astype a timedelta from \[timedelta64\[ns\]\] to" | ||
r" \[int32\]") | ||
with pytest.raises(TypeError, match=msg): | ||
td.astype('int32') | ||
|
||
# this is an invalid casting | ||
def f(): | ||
msg = "Could not convert object to NumPy timedelta" | ||
with pytest.raises(ValueError, match=msg): | ||
Series([timedelta(days=1), 'foo'], dtype='m8[ns]') | ||
|
||
pytest.raises(Exception, f) | ||
|
||
# leave as object here | ||
td = Series([timedelta(days=i) for i in range(3)] + ['foo']) | ||
assert td.dtype == 'object' | ||
|
@@ -1134,9 +1151,11 @@ def test_constructor_name_hashable(self): | |
assert s.name == n | ||
|
||
def test_constructor_name_unhashable(self): | ||
msg = r"Series\.name must be a hashable type" | ||
for n in [['name_list'], np.ones(2), {1: 2}]: | ||
for data in [['name_list'], np.ones(2), {1: 2}]: | ||
pytest.raises(TypeError, Series, data, name=n) | ||
with pytest.raises(TypeError, match=msg): | ||
Series(data, name=n) | ||
|
||
def test_auto_conversion(self): | ||
series = Series(list(date_range('1/1/2000', periods=10))) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this is raising
missing 1 required positional argument: 'x'
because of the lambda and not failing as intended. correcting this with the conversion to context manger is only failing on windows and again from the error message is probably not failing as intendedThere 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.
this should be raising TypeError, what happens when you use the context manager
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.
it is raising TypeError in master because of the lambda. this is a bad test.
correcting it with the context manager raises
TypeError: cannot astype a datetimelike from [datetime64[ns]] to [int32]
but only on the windows tests (see ci failures)i've reverted the changes to the test, but the ci is failing on an unrelated conda error at the moment.
with the changes in this PR there will still be 823 uses of pytest.raises without the cm. so i don't want to get bogged down with bad tests at the moment.
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.
k that's fine (though happy to take your change, though it should raise on windows as well). can you create an issue about this.