Something I forgot to mention in yesterday’s introduction to this series is that one of my goals is to commit patches that have been produced by people who don’t themselves have commit privileges. Today’s bug is the first of that sort.
issue 19449 (closed) is pretty straightforward: the code for generating the error message in csv.DictWriter fails with an TypeError if the dictionary passed to the writerow() method contains any unknown keys that are not strings. Since csv otherwise copes with non-string keys just fine (by calling str on them), the error handling code just needs to do something similar.
In both the initial patch suggested by the bug reporter Thomas Grahn and the expanded patch-with-test provided by Vajrasky Kok, this is accomplished by calling repr on the “wrong keys” as we collect them. We use repr rather than str because it is an error message, making it easier to identify the types of the incorrect keys.
Thanks to Thomas for reporting the bug and providing a fix, and to Vajrasky for providing the patch with a test.