Skip to content

CLN: Improve Exceptions in core/frame and testing in test_frame and test_multilievel #4732

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 2 commits into from
Sep 6, 2013

Conversation

jtratner
Copy link
Contributor

@jtratner jtratner commented Sep 2, 2013

More specific Exceptions (and better testing for them) through a number of parts of pandas. In particular, in core/frame / tests/test_frame and tests/test_multilevel Associated changes in some other parts of pandas to support it (mostly making sparse have the same exceptions as core/frame).

  • nearly everything that was a TypeError now checks message (to avoid TypeError confusion from changes in signature).
  • refactored some test methods and changed some places to use assertRaises and assertRaisesRegexp as context managers.
  • core/reshape no longer uses a specific ReshapeError (which wasn't really being used anywhere)
  • marked guard test cases with # pragma: no cover to hopefully make it easier to use coverage.
  • new raise_with_traceback method in compat which allows for 2/3 compatible preservation of traceback.

Obviously some squashing to come.

Related to #3954.

@jtratner
Copy link
Contributor Author

jtratner commented Sep 3, 2013

This is not a huge change - but anyone want to look over it?

@jreback
Copy link
Contributor

jreback commented Sep 4, 2013

looks ok to me

the data conflic error in update is odd
maybe make an issue to have someone look at it (I think I understand the rationale but needs a look)

@jtratner
Copy link
Contributor Author

jtratner commented Sep 4, 2013

It is weird. I wanted it to inherit from ValueError to ease the transition to it (probably) becoming a ValueError. Really sticks out (especially because we don't use it anywhere internally).

@jreback
Copy link
Contributor

jreback commented Sep 4, 2013

prob just something vestigial - just change to valueError (which I think u did)?

@cpcloud
Copy link
Member

cpcloud commented Sep 4, 2013

+1 for ValueError...is this used anywhere else?

@jtratner
Copy link
Contributor Author

jtratner commented Sep 4, 2013

nowhere. All three uses are: the definition of the exception, in update and in the test cases :P. I'm happy to remove it (right now it's just a subclass of ValueError).

@jreback
Copy link
Contributor

jreback commented Sep 4, 2013

out of there

@cpcloud
Copy link
Member

cpcloud commented Sep 4, 2013

cut---it---out ✂️

@jtratner
Copy link
Contributor Author

jtratner commented Sep 4, 2013

okie dokie, goodbye DataConflictError. Any other comments?

raise ValueError('axis must be 0 or 1')
if axis not in (0, 1): # pragma: no cover
raise AssertionError('axis must be 0 or 1')
agg_axis = (axis + 1) % 2
Copy link
Contributor

Choose a reason for hiding this comment

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

or 1 - axis if you like :) (doesn't matter)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like 1 - axis better - seems clearer to me.

@hayd
Copy link
Contributor

hayd commented Sep 4, 2013

(raise_with_traceback is pretty awesome. as is this pr)

@jtratner
Copy link
Contributor Author

jtratner commented Sep 4, 2013

@hayd thanks! 😄 and thanks for catching those mysterious test removals! I spent some time removing obvious dups but definitely didn't want to remove things arbitrarily!

@jtratner
Copy link
Contributor Author

jtratner commented Sep 5, 2013

@hayd I think it's good at this point, if you want to take a final look...

@@ -4392,7 +4395,7 @@ def _get_agg_axis(self, axis_num):
elif axis_num == 1:
return self.index
else:
raise Exception('Must have 0<= axis <= 1')
raise ValueError('Must have 0 <= axis <= 1')
Copy link
Contributor

Choose a reason for hiding this comment

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

to match others think should this be AssertionError('Axis must be 0 or 1. Got %s' % str(axis)) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This error can bubble out to the user, which is why it's a ValueError, the others call a function that should only ever generate 0 or 1 (or error out), so it's a ValueErrror (that's why those other errors also have # pragma: no cover, they're basically just a catch that helper functions worked.)

Copy link
Contributor

Choose a reason for hiding this comment

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

ah, I see, so you don't have AssertionErrors to be user facing?

But why not have the better message? (axis=0.5 is no good!)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

my impression is that usually you use an AssertionError to validate internal logic (this internal function will always be called with a list...etc.). (and that's why if you run something with -O it removes assertion errors - they are supposedly only checks for the programmer). That's a good point on the message, I'll change it.

Copy link
Contributor

Choose a reason for hiding this comment

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

I thought that raise AssertionError will still raise with -o.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it will..just generally, that's why asserts are stripped out with -O. [and for the specific cases here, it might actually be 'more' correct to have them be asserts, but it doesn't really matter]

@hayd
Copy link
Contributor

hayd commented Sep 5, 2013

Pretty tricky to spot if you've any other missing, but if you've checked then I'm happy.

Also, re raise_with_traceback please consider answering this SO question: http://stackoverflow.com/questions/17001473/uncatching-an-exception-in-python :)

* Adds `raise_with_traceback` to `pandas.compat`, which handles the
  Python 2/3 syntax differences for raising with traceback.
* Uses `raise_with_traceback` in `assertRaises` and `assertRaisesRegexp`
  so they provide better feedback.

ENH: raise_with_traceback method.
CLN: Big simplification of test_frame
TST: Add test cases for bad input, etc.
CLN: Exceptions that shouldn't happen should be assertions (mostly _apply*)
CLN/TST: Right Exceptions + remove duplicate tests

A few tests were duplicating the same thing or actually just the same
test, those have been refactored or removed. Many tests were changed to
specify the Exception they were looking for (and also to use the new
`with_statement` format for assertRaises, etc.)

CLN: Harmonize SparseDF and DF Exceptions
ENH: SparseDF apply now accepts axis='columns'
jtratner added a commit that referenced this pull request Sep 6, 2013
CLN: Improve Exceptions in core/frame and testing in test_frame and test_multilievel
@jtratner jtratner merged commit 0d2ef67 into pandas-dev:master Sep 6, 2013
@jtratner jtratner deleted the change-bare-exceptions-pt-3 branch September 6, 2013 02:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants