Clearly, the new code would have to be repackaged if it were to be made part of tokenize.py. That's all I would like to say now regarding your comments. Perhaps other devs will pick up the ball.
After sleeping on your comments, I would like to distill them to their gist:
1. untokenize does not take a contents argument.
2. untokenize does not do exactly the same thing as the new code.
These are legitimate complaints, and I'm not quite sure what, if anything, can be done about them.
However, I am not quite ready dismiss the possibility of using the the new code in the tokenize module, because...
For convenience, lets call the proposed new code the gem :-)
Let's be clear, there is no reason for python libraries to include every little code gem.
However, I have two motivations adding the gem to the tokenize module:
1. The gem would be useful for any token-based beautification code, such as the token-based versions of black or fstringify that Leo uses. Furthermore, the present untokenize method teaches away
from the gem.
is troubling, because the unit tests failed to detect a code blunder in add_whitespace.
Strengthening unit tests
It's not clear how the gem could strengthen unit tests. Indeed, the gem is always going to pass ;-)
This is frustrating, because full sources are available to
TestRoundtrip.check_roundtrip. Indeed, that method starts with:
if isinstance(f, str):
code = f.encode('utf-8')
code = f.read()
If f is a str, then f is "contents". Otherwise, "contents" can be recreated from the file's bytes.
Your objections are largely valid. Nevertheless, the gem may be worth further consideration.
There is no need to add every little code gem to python's standard library. In this case, there are two reasons why adding the gem to tokenize.py might be a good idea:
1. The gem would be of use to
anyone writing a token-based tool. Such tools do have access to full sources.
2. The present untokenize method teaches away from the gem. The gem corrects the mistaken impression that untokenizing in the presence of full sources is difficult. In fact, it's a snap.
It is an open question whether it might be possible to strengthen the unit tests for the tokenize module using the gem. Full sources are available to