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
enhancement for operator 'assert' #62842
Comments
It would be pretty good, if 'assert' could raise specified exception, like that:
<s>AssertionError</s>TypeError: 'data' must be a string |
What's wrong with: if not isinstance(data, basestring):
raise TypeError(...) ? In any case, you appear to be wanting to use assert to check user input. That's not its intended use; instead, it's there for making debugging assertions. Bear in mind that when running in optimized mode (with python -O), Python won't execute those asserts at all. (See http://docs.python.org/3.4/reference/simple_stmts.html#the-assert-statement for more.) I think this should be rejected. |
Mark Dickinson, and I just think it might be useful in debug. PYO is another story. |
Ah, so I think I don't understand the proposal. In your original message, is it your intention that the assert raises TypeError, or that it raises AssertionError? Again: what's the benefit over existing solutions? Either: if not isinstance(data, basestring):
raise TypeError("Bad user! You gave me the wrong type") or
? |
I think it would be confusing for assert to raise anything other than an AssertionError, so I also think this should be rejected. It might be interesting for there to be a way to call unittest's assert methods in a debug context (that is, without having to have a test case around), but that is a very non-trivial (and probably impractical) thought :) |
Mark Dickinson, #1 if dedug (and type check, respectively, as in this example, and 'raise') isn't needed we just need pyo
if not isinstance(data, basestring):
raise TypeError(...)
R. David Murray, yes, maybe confusing, but I can imagine many ways when it is very convenient. |
Mark Dickinson, sorry, didn't answer the first questiuon.
I suggest to add the ability to raise relevant (for specific part of code) exception on checking assertion. |
Assert with this feature will make code simplest. Simplification isn't python way? Why don't add it in python? It's must be really useful. Sometimes its necessary, when need to catch specified exception. Its easier to search errors by type, not by error body |
If your code is catching an exception generated by an assert statement, your code is using assert incorrectly. There is never any reason, as far as I can see, to catch an assert outside of a testing framework, and in a testing framework you definitely want to be catching an AssertionError when you are trying to catch an assert failing. Making it some other error would just confuse the testing framework. |
Okay, I'm closing this for now. Al Korgun and mrDoctorWho0 .: if you think this idea deserves wider discussion, you should feel free to bring it up again on the python-ideas mailing list; that's a better forum to discuss these sorts of language changes anyway (too few developers will look at any particular bug on the bug tracker; many more read python-ideas). It seems unlikely to me that the idea would receive widespread acceptance, but I may well be wrong. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: