Title: Validate callbacks in 'contextlib.ExitStack.callback()'
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.5
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Yury.Selivanov, ncoghlan, serhiy.storchaka, yselivanov
Priority: normal Keywords: patch

Created on 2012-06-25 22:31 by Yury.Selivanov, last changed 2020-04-25 16:18 by serhiy.storchaka.

File name Uploaded Description Edit
contextlib.patch Yury.Selivanov, 2012-06-25 22:31 review
Messages (4)
msg164021 - (view) Author: Yury Selivanov (Yury.Selivanov) * Date: 2012-06-25 22:31
With this patch 'contextlib.ExitStack.callback(callback, *args, **kwds)' will validate that the passed callback actually accept the passed '*args' and '**kwds'.

P.S. If this patch comes through - I'll create two more issues for 'atexit.register' and 'unittest.TestCase.addCleanup'.  (I'll also look through the stdlib to find if there are some other places where this is needed.)
msg164024 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2012-06-26 00:07
(my previous message edit: no support for 'atexit' - it's implemented in C)
msg164025 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2012-06-26 01:02
I'm not comfortable adding a dependency on inspect to the contextlib module, and I think it would be undesirable to have too many cases where the early validation is silently skipped.

While I like the idea of adding early signature validation to these APIs in principle, given the above concerns and the fact this probably crosses the line into "feature" territory, I think we want to wait until 3.4 and the introduction of a mechanism that adds introspection support to callables implemented in C.

That will likely involve moving the signature support somewhere lower in the module stack (perhaps to a private "_signature" module, with inspect remaining the official API), and will reduce the number of cases where pre-validation is skipped.
msg367276 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-04-25 16:18
I think it is rather a work for linter's and static type checking. Runtime check adds an overhead, and it is expected that in correct program it will always pass. You need the check only while write the program, it may help in debugging. But once it has been debugged and tested, the check becomes a waste of CPU time.

Since this issue was not touched for 8 years and there is a large progress in static code analysis, I suggest to close it.
Date User Action Args
2020-04-25 16:18:48serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg367276
2014-01-27 20:33:43yselivanovsetversions: + Python 3.5, - Python 3.4
2012-06-26 01:02:41ncoghlansetmessages: + msg164025
versions: + Python 3.4, - Python 3.3
2012-06-26 00:07:28yselivanovsetmessages: + msg164024
2012-06-25 23:11:34yselivanovsetnosy: + yselivanov
2012-06-25 22:31:40Yury.Selivanovcreate