Skip to content

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

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

Add post-processing hook for custom token transformations#87
hedgert wants to merge 2 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
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>
@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.

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