[issue20524] format error messages should provide context information

Nikita Sobolev report at bugs.python.org
Mon Sep 13 08:04:47 EDT 2021


Nikita Sobolev <mail at sobolevn.me> added the comment:

> I think that would require some major surgery to the code, but would be worth it.

I've decided to take an easy path: https://github.com/python/cpython/pull/28310

```
PyErr_Format(PyExc_ValueError,
                     "Invalid format specifier: '%s' for object of type '%s'",
                     PyUnicode_AsUTF8AndSize(format_spec, NULL),
                     Py_TYPE(obj)->tp_name);
```

This worked for me as the starting point. It covered: int, float, complex, and str types.

> Note that in your original example, you want the error to contain "{length:%HH:%MM}". By the time the error is detected, the only thing the code knows is the format specifier "%HH:%MM". It doesn't know the "length" part. 

I guess it has changed since the 2014.

I would love to hear any feedback on my proposal and improve it to the level when it can be merged :)

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue20524>
_______________________________________


More information about the Python-bugs-list mailing list