Improve traceback for common `with` statement mistake

Hello all, In Python 3.10 and 3.11, exception tracebacks are being greatly improved. I noticed that there's nothing related to a fairly common (in my personal experience) cryptic traceback relating to the `with` statement:
This occurs when one forgets to use a instance of a context manager class and uses the class itself. It's obviously not a very helpful traceback. ("Is it not a context manager?" "Is it the wrong class?") Something like the following would be better.
The actual traceback message should probably be more specific than "<type Context manager> is not a context manager". Thoughts? -- Finn

On Mon, Sep 6, 2021 at 9:37 AM Finn Mason <finnjavier08@gmail.com> wrote:
I'm seeing a different message, so it looks like something HAS been improved: Python 3.11.0a0 (heads/main:ed524b4569, Aug 14 2021, 11:29:01) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information.
The "did you mean" part depends on or assumes that it can figure out that calling it would have given a viable context manager. This could be done by probing whether thing.__enter__ exists, but I'm not sure that'd be entirely safe. Also, it won't catch those made from generators:
In any case, the biggest advantage (IMO) comes from naming the type, which will make it easy to distinguish the object from its type. ChrisA

Thank you for testing that. I dug through the change log, and found bpo-12022: https://bugs.python.org/issue12022 It *has* been fixed in 3.11, but not mentioned in the What's New document. Should it be? On Sun, Sep 5, 2021, 5:49 PM Chris Angelico <rosuav@gmail.com> wrote:

On Tue, Sep 7, 2021 at 1:36 AM Finn Mason <finnjavier08@gmail.com> wrote:
If it was indeed a side effect of that change, then there *is* a What's New entry about it, but it's focusing on other changes: https://github.com/python/cpython/pull/26809/files#diff-78f24041d66ab8ed2ae1... It's looking like 3.11 is the version where error messages get improved in all the different ways :) ChrisA

On Mon, Sep 6, 2021 at 9:37 AM Finn Mason <finnjavier08@gmail.com> wrote:
I'm seeing a different message, so it looks like something HAS been improved: Python 3.11.0a0 (heads/main:ed524b4569, Aug 14 2021, 11:29:01) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information.
The "did you mean" part depends on or assumes that it can figure out that calling it would have given a viable context manager. This could be done by probing whether thing.__enter__ exists, but I'm not sure that'd be entirely safe. Also, it won't catch those made from generators:
In any case, the biggest advantage (IMO) comes from naming the type, which will make it easy to distinguish the object from its type. ChrisA

Thank you for testing that. I dug through the change log, and found bpo-12022: https://bugs.python.org/issue12022 It *has* been fixed in 3.11, but not mentioned in the What's New document. Should it be? On Sun, Sep 5, 2021, 5:49 PM Chris Angelico <rosuav@gmail.com> wrote:

On Tue, Sep 7, 2021 at 1:36 AM Finn Mason <finnjavier08@gmail.com> wrote:
If it was indeed a side effect of that change, then there *is* a What's New entry about it, but it's focusing on other changes: https://github.com/python/cpython/pull/26809/files#diff-78f24041d66ab8ed2ae1... It's looking like 3.11 is the version where error messages get improved in all the different ways :) ChrisA
participants (3)
-
Chris Angelico
-
Finn Mason
-
Serhiy Storchaka