This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: new assert raises syntax proposal
Type: enhancement Stage: resolved
Components: Tests Versions: Python 3.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, ezio.melotti, tbicr
Priority: normal Keywords:

Created on 2014-07-07 11:25 by tbicr, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg222450 - (view) Author: Pavel Tyslyatsky (tbicr) Date: 2014-07-07 11:25
This proposal look preaty close to pep-463: http://legacy.python.org/dev/peps/pep-0463/, but in assertion context.

Now python test libraries have different aproach for assertions, some try use own implementations, for example, for equality `assertEqual` (`assertEqulas`), `eq_`, some use python `assert` keyword.

`assert` work fine for variables, but requred verbose wrappers for exception raising, for example:

>>> try:
...   do_raise_code()
... except Exception:
...   assert False

Test libraries already have self implementations but it still not pure python like `assert`:

unittest: `self.assertRaises`
py.test: `pytest.raises`
nose: `nose.tools.raises` or `nose.tools.assert_raises`

I propose add pure python implementation for this case because it enough popular, for example:

>>> assert do_raise_code() raises  # ok if `do_raise_code` raise any exception
>>> assert do_raise_code() raises, 'text message'  # ok if `do_raise_code` raise any exception with message
>>> assert do_raise_code() raises Exception  # ok if `do_raise_code` raise specific exception
>>> assert do_raise_code() raises Exception, 'text message'  # ok if `do_raise_code` raise specific exception with message
>>> assert do_raise_code() raises (TypeError, ValueError)  # ok if `do_raise_code` raise one of specific exceptions
>>> assert do_raise_code() raises (TypeError, ValueError), 'text message'  # ok if `do_raise_code` raise one of specific exceptions with message

Test libraries can use tham raises implementations as decorator, this proposal currently ignore similar behaviour.
Test libraries can use them raises implementations as context, this propasal currently ignore similar behaviour.
`unittest` module also has `assertRaisesRegex` method, this proposal currently ignore similar behaviour.
`unittest` module also has `assertWarns` and `assertLogs`, this proposal currently ignore similar behaviour.
Also this proposal currently ignore any access to exception object and it fields.
msg222459 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2014-07-07 13:04
The best way to get language changes made is to first propose them on the python-ideas mailing list (https://docs.python.org/devguide/faq.html#suggesting-changes).
msg222460 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2014-07-07 13:11
Agreed, but there are a few problems with the proposals:
1) it requires the addition of a new keyword (raises), and that will break any program that uses it as an identifier;
2) the use case is arguably limited;
3) assertRaises is consistent with the other assert* methods, and it also provides way to inspect the exception, whereas IIUC what you are proposing does not (and also doesn't replace assertRaisesRegex);
4) I don't find your proposal simpler or more readable than what we have now:

In other words there are quite a few problems for little benefit, so even if you decide to bring this up to python-ideas, it's very unlikely that it will be accepted in its current form.
msg222463 - (view) Author: Pavel Tyslyatsky (tbicr) Date: 2014-07-07 13:46
Thanks for reply, I really missed many cases. I will try look deeper.
msg222538 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2014-07-07 23:12
See also https://mail.python.org/pipermail/python-ideas/2014-June/028206.html for a list of things to keep in mind while adding new features (especially keywords).
History
Date User Action Args
2022-04-11 14:58:05adminsetgithub: 66129
2014-07-07 23:12:13ezio.melottisetmessages: + msg222538
2014-07-07 13:46:47tbicrsetmessages: + msg222463
2014-07-07 13:11:36ezio.melottisetstatus: open -> closed

components: + Tests

nosy: + ezio.melotti
messages: + msg222460
resolution: rejected
stage: resolved
2014-07-07 13:04:56brett.cannonsetnosy: + brett.cannon
messages: + msg222459
2014-07-07 11:25:24tbicrcreate