Following system colour scheme Selected dark colour scheme Selected light colour scheme

Python Enhancement Proposals

PEP 473 – Adding structured data to built-in exceptions

Author:
Sebastian Kreft <skreft at deezer >
Status:
Rejected
Type:
Standards Track
Created:
29-Mar-2014
Post-History:

Resolution:
Python-Dev message

Table of Contents

Abstract

Exceptions likeAttributeError,IndexError,KeyError, LookupError,NameError,TypeError,andValueErrordo not provide all information required by programmers to debug and better understand what caused them. Furthermore, in some cases the messages even have slightly different formats, which makes it really difficult for tools to automatically provide additional information to diagnose the problem. To tackle the former and to lay ground for the latter, it is proposed to expand these exceptions so to hold both the offending and affected entities.

Rationale

The main issue this PEP aims to solve is the fact that currently error messages are not that expressive and lack some key information to resolve the exceptions. Additionally, the information present on the error message is not always in the same format, which makes it very difficult for third-party libraries to provide automated diagnosis of the error.

These automated tools could, for example, detect typos or display or log extra debug information. These could be particularly useful when running tests or in a long running application.

Although it is in theory possible to have such libraries, they need to resort to hacks in order to achieve the goal. One such example is Python -improved-exceptions[1],which modifies the byte-code to keep references to the possibly interesting objects and also parses the error messages to extract information like types or names. Unfortunately, such approach is extremely fragile and not portable.

A similar proposal[2]has been implemented forImportErrorand in the same fashion this idea has received support[3].Additionally, almost 10 years ago Guido asked in[11]to have a clean API to access the affected objects in Exceptions likeKeyError,AttributeError,NameError,and IndexError.Similar issues and proposals ideas have been written in the last year. Some other issues have been created, but despite receiving support they finally get abandoned. References to the created issues are listed below:

To move forward with the development and to centralize the information and discussion, this PEP aims to be a meta-issue summarizing all the above discussions and ideas.

Examples

IndexError

The error message does not reference the list’s length nor the index used.

a=[1,2,3,4,5]
a[5]
IndexError:listindexoutofrange

KeyError

By convention the key is the first element of the error’s argument, but there’s no other information regarding the affected dictionary (keys types, size, etc.)

b={'foo':1}
b['fo']
KeyError:'fo'

AttributeError

The object’s type and the offending attribute are part of the error message. However, there are some different formats and the information is not always available. Furthermore, although the object type is useful in some cases, given the dynamic nature of Python, it would be much more useful to have a reference to the object itself. Additionally the reference to the type is not fully qualified and in some cases the type is just too generic to provide useful information, for example in case of accessing a module’s attribute.

c=object()
c.foo
AttributeError:'object'objecthasnoattribute'foo'

importstring
string.foo
AttributeError:'module'objecthasnoattribute'foo'

a=string.Formatter()
a.foo
AttributeError:'Formatter'objecthasnoattribute'foo'

NameError

The error message provides typically the name.

foo=1
fo
NameError:globalname'fo'isnotdefined

Other Cases

Issues are even harder to debug when the target object is the result of another expression, for example:

a[b[c[0]]]

This issue is also related to the fact that opcodes only have line number information and not the offset. This proposal would help in this case but not as much as having offsets.

Proposal

Extend the exceptionsAttributeError,IndexError,KeyError, LookupError,NameError,TypeError,andValueErrorwith the following:

  • AttributeError:targetw,attribute
  • IndexError:targetw,keyw,index (just an alias to key)
  • KeyError:targetw,keyw
  • LookupError:targetw,keyw
  • NameError:name, scope?
  • TypeError:unexpected_type
  • ValueError:unexpected_valuew

Attributes with the superscriptwmay need to be weak references[12]to prevent any memory cycles. However, this may add an unnecessary extra complexity as noted by R. David Murray[13].This is specially true given that builtin types do not support being weak referenced.

TODO(skreft): expand this with examples of corner cases.

To remain backwards compatible these new attributes will be optional and keyword only.

It is proposed to add this information, rather than just improve the error, as the former would allow new debugging frameworks and tools and also in the future to switch to a lazy generated message. Generated messages are discussed in[2], although they are not implemented at the moment. They would not only save some resources, but also uniform the messages.

The stdlib will be then gradually changed so to start using these new attributes.

Potential Uses

An automated tool could for example search for similar keys within the object, allowing to display the following::

a = {'foo': 1}
a['fo']
KeyError: 'fo'. Did you mean 'foo'?

foo = 1
fo
NameError: global name 'fo' is not defined. Did you mean 'foo'?

See[3]for the output a TestRunner could display.

Performance

Filling these new attributes would only require two extra parameters with data already available so the impact should be marginal. However, it may need special care forKeyErroras the following pattern is already widespread.

try:
a[foo]=a[foo]+1
except:
a[foo]=0

Note as well that storing these objects into the error itself would allow the lazy generation of the error message, as discussed in[2].

References


Source:https://github / Python /peps/blob/main/peps/pep-0473.rst

Last modified:2023-09-09 17:39:29 GMT