Skip to content

Add post-processing hook for custom token transformations - #87

Open
hedgert wants to merge 3 commits into
python-trio:masterfrom
hedgert:multi-token-extension
Open

Add post-processing hook for custom token transformations#87
hedgert wants to merge 3 commits into
python-trio:masterfrom
hedgert:multi-token-extension

Conversation

@hedgert

@hedgert hedgert commented Jul 30, 2026

Copy link
Copy Markdown

Summary

unasync currently provides excellent support for single-token replacements
during async-to-sync conversion.

Some transformations, however, require replacing one token with multiple
tokens or vice versa. Examples include:

  • asyncio.current_task → current_thread
  • asyncio.Task → Thread
  • asyncio.sleep → time.sleep

Solution

This change introduces an optional post-processing hook that runs after
the existing token replacement stage.

The hook receives the token stream and allows more complex transformations
to be implemented without affecting the existing replacement mechanism.

Existing users are unaffected unless they choose to provide a
post-processor.

Motivation

This extension makes it possible to implement more sophisticated language
transformations while keeping the existing replacement system simple.

@hedgert hedgert changed the title multi-token extension capability added Add post-processing hook for custom token transformations Jul 30, 2026

@pquentin pquentin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Do you mind adding a test to clarify how this would be used?

Comment thread src/unasync/__init__.py Outdated
@hedgert

hedgert commented Jul 31, 2026

Copy link
Copy Markdown
Author

(I think I overwrote my earlier reply with my comment about the token to tokens change - so repeating it). Re your request for an example:

Thanks for the review. While putting together the test, I realised there are two possible directions:

  • a minimal example that demonstrates the new hook, or
  • extending unasync with a reusable implementation so users can express these more complex transformations through configuration rather than writing code (as I ended up writing code similar to your original to walk the token list but comparing consecutive tokens to look for matches instead of comparing single tokens - and it might be beneficial for others to be able to use that rather than have to write it as an extension using my hook).

Do you have a preference? I'm happy to take whichever approach best fits the project's direction.

@pquentin

Copy link
Copy Markdown
Member

Thanks for asking! Assuming the reusable implementation is built on top of this change, I would start with the minimal test, merge this PR, and then open a new PR with the reusable implementation.

@hedgert
hedgert force-pushed the multi-token-extension branch 2 times, most recently from d472b42 to c920028 Compare July 31, 2026 13:31
@hedgert

hedgert commented Jul 31, 2026

Copy link
Copy Markdown
Author

Thanks for the suggestion. I've added a regression test for the new hook. The test demonstrates a subclass overriding _postprocess_tokens() to perform a context-sensitive token transformation and verifies that the transformed output is written correctly.

@hedgert

hedgert commented Jul 31, 2026

Copy link
Copy Markdown
Author

Sorry please pause - although my test ran successfully standalone I hadn't run the full test suite and it's not running in the test suite - I will fix and update here.

Tim Hedger-Gourlay and others added 3 commits July 31, 2026 15:11
Bug fix (after my testing focused on using the hook to override the post processor method instead of testing the default post processor

Co-authored-by: Quentin Pradet <quentin.pradet@gmail.com>
Test demonstrates a simple real world multitoken transformation
from: asyncio.current_task() to: current_thread()
it also has data to confirm that those tokens on their own are not touched
(so asyncio remains asyncio and curent_task() remains current_task())
@hedgert
hedgert force-pushed the multi-token-extension branch from c920028 to 51ae2f0 Compare July 31, 2026 14:12
@hedgert

hedgert commented Jul 31, 2026

Copy link
Copy Markdown
Author

OK (I had lost the sort directories change that was the subject of a separate - overlapping - pull request). Given the tests here in Github worked, it maybe that has no effect on any environment but mine - but the sorted directories fix is required in my environment or the project structure test fails. My new test is now ready for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants