Registry / serialization / untokenize

untokenize

JSON →
library0.1.1pypypi✓ verified 23d ago

The `untokenize` library (version 0.1.1) transforms a stream of Python tokens back into source code. Its primary distinction from the standard library's `tokenize.untokenize()` is its ability to preserve the original whitespace between tokens, addressing a common limitation. Released in February 2014, this library is stable for its intended purpose, but is no longer under active development.

pip install untokenize
INSTALL
IMPORT
SIG · UNTOKENIZE
U
untokenize
serializationpythonv0.1.1
Install
2.4s avg
Import
10ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.002s · 19.2MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.4s · import 0.000s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

untokenize
import untokenize
from tokenize import untokenize
This library provides its own untokenize function, distinct from the standard library's tokenize.untokenize, which has different behavior regarding whitespace. Importing directly as `untokenize` avoids confusion.

This example demonstrates how to use `untokenize` to reconstruct source code from a token stream, ensuring original whitespace is preserved. It uses `tokenize.tokenize` from the standard library to generate tokens, then passes them to `untokenize.untokenize`.

import tokenize import io import untokenize source_code_with_whitespace = "def hello( name ):\n print(f'Hello, {name}!') # A comment" # Tokenize the source code using the standard library's generator # The readline callable should return bytes for tokenize.tokenize tokens_generator = tokenize.tokenize(io.BytesIO(source_code_with_whitespace.encode('utf-8')).readline) # Convert generator to a list of tokens tokens = list(tokens_generator) # Use untokenize library to reconstruct source code, preserving whitespace reconstructed_code = untokenize.untokenize(tokens) print("Original:") print(source_code_with_whitespace) print("\nReconstructed (with untokenize library):") print(reconstructed_code) # For comparison, standard library's untokenize (might format differently) # from tokenize import untokenize as std_untokenize # std_reconstructed_code = std_untokenize(tokens) # print("\nReconstructed (with stdlib tokenize.untokenize):") # print(std_reconstructed_code)
Debug
Known issues
gotchaThe `untokenize` library's last release was in February 2014, indicating it is no longer actively maintained. While its core functionality is simple and stable, it will not receive updates for new Python syntax features or potential edge cases that arise with future Python versions.
fix
Be aware of its maintenance status and consider thoroughly testing its behavior with newer Python versions (beyond Python 2.7 era) if used in critical projects. For modern, actively maintained tokenization/untokenization needs, consider alternatives or the standard library's `tokenize` module with its known limitations.
affects: 0.1.1
gotchaThis library provides an `untokenize` function that is distinct from Python's standard `tokenize.untokenize`. The key difference is that this library prioritizes the preservation of original whitespace between tokens, which the standard library function does not reliably do, potentially leading to different formatting in the output.
fix
Always explicitly import `untokenize` as `import untokenize` and call `untokenize.untokenize(tokens)` to ensure you are using this library's version. If you need the standard library's behavior, use `from tokenize import untokenize as std_untokenize`.
affects: 0.1.1
Upgrade
Version history
0.1.1latest on PyPI · released Feb 8, 2014
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
10
OpenAI (training)
1
Resources
untokenize — pip install untokenize · libregistry