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: Docs on unittest.TestCase.assertRaises() should clarify context manager details
Type: Stage: resolved
Components: Documentation Versions: Python 3.8
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, ezio.melotti, nodakai, r.david.murray
Priority: normal Keywords: patch

Created on 2018-02-28 17:58 by nodakai, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg313061 - (view) Author: NODA Kai (nodakai) Date: 2018-02-28 17:58
https://docs.python.org/dev/library/unittest.html#unittest.TestCase.assertRaises

> If only the exception and possibly the msg arguments are given, return a context manager so that the code under test can be written inline rather than as a function:
> 
>     with self.assertRaises(SomeException):
          do_something()
> 
> When used as a context manager, assertRaises() accepts the additional keyword argument msg.

Perhaps we don't need the second sentence on the `msg` argument which isn't adding anything new.

Ideally it should be more clear when the method operates in context manager mode.
("If only" and "possibly" don't play nicely together.)
Maybe along the lines of "If no callable was passed as an argument ..." ? I haven't looked in to the actual implementation yet...
msg313619 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-03-11 19:28
The implementation is literally that no non-keyword arguments other than the exception are given.  If any keyword arguments other than msg are given, you get a warning. To say just "no callable" would be about as confusing as the 'and possibly' given the documented prototype, since in fact any non-keyword argument will be assumed to be the callable. So the text is correct as written, and I'm not sure how one would make it clearer and still be technically correct.  But further suggestions are welcome.
History
Date User Action Args
2022-04-11 14:58:58adminsetgithub: 77152
2019-05-06 20:20:48anthonypjshawsetstatus: open -> closed
stage: patch review -> resolved
2019-05-06 20:19:49anthonypjshawsetpull_requests: - pull_request9605
2018-11-02 11:25:06dave-shawleysetkeywords: + patch
stage: patch review
pull_requests: + pull_request9605
2018-03-11 19:28:39r.david.murraysetnosy: + r.david.murray
messages: + msg313619
2018-03-01 21:42:19brett.cannonsettitle: Docs on unittest.TestCase.assertRaises() should be improved -> Docs on unittest.TestCase.assertRaises() should clarify context manager details
2018-02-28 18:02:20serhiy.storchakasetnosy: + ezio.melotti
2018-02-28 17:59:16nodakaisettitle: unittest.TestCase.assertRaises -> Docs on unittest.TestCase.assertRaises() should be improved
2018-02-28 17:58:02nodakaicreate