On 06/10/2021 17:35, Łukasz Langa wrote: > >> On 6 Oct 2021, at 18:05, Yury Selivanov <yselivanov...@gmail.com> wrote: [...] >> I'll list a few reasons here: >> >> 1. `try: .. except group:` is a valid syntax today. And it will continue to >> be valid syntax. Having both `try: .. except group:` (catch exception >> `group`) and `try: .. except group E:` (catch exceptions of E into a group) >> in the same grammar worries me. >> >> 1a. It can be especially confusing if someone has a local/global variable >> called `group`. > > This is a valid point, also raised by Pablo over WhatsApp (which happens to > work today!). The particular hairy example has to do with your next point so > let's go there first... > > >> 1b. Or, for example, if a user forgets to type `E` and leaves just `except >> group` it would fallback to the regular try..except behavior. And it would >> be a runtime error ("group" is undefined). > > Right. Worse yet, this wouldn't be a runtime error UNLESS user code raises an > exception within that try: block. Otherwise Python would happily take the > unbound name and run with it: > > >>> try: > ... ... > ... except group: > ... ... > ... > Ellipsis > > > When you raise: > > >>> try: > ... 1/0 > ... except group: > ... ... > ... > Traceback (most recent call last): > File "<stdin>", line 2, in <module> > ZeroDivisionError: division by zero > > During handling of the above exception, another exception occurred: > > Traceback (most recent call last): > File "<stdin>", line 3, in <module> > NameError: name 'group' is not defined > > > This is pretty confusing and in my eyes disqualifies the "except group" > proposal. Pablo also claims it would be very hard to generate good error > messages due to this and I can see why. My initial idea here was to modify > this received `NameError` just like we do in other cases with the new "Did > you mean" helper: > > >>> arg = 1 > >>> ar > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > NameError: name 'ar' is not defined. Did you mean: 'arg'? > >>> def f(): > ... ar > ... > >>> f() > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > File "<stdin>", line 2, in f > NameError: name 'ar' is not defined. Did you mean: 'arg'? > > We could potentially do something similar to generate better error messages > for "except group" confusion, right? Only *we can't* if `group` happens to be > bound as a name in a reachable scope which Larry points out is a popular > name. In this scenario any syntax errors would end up with terribly confusing > TypeErrors or AttributeErrors and so on. This is unacceptable.
Now a moot point, but this could be a SyntaxWarning.
_______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/PB4ANBGW4M3DZDQA6C2PPXUPGPBZ3JJZ/ Code of Conduct: http://python.org/psf/codeofconduct/