Add post-processing hook for custom token transformations - #87
Conversation
pquentin
left a comment
There was a problem hiding this comment.
Thanks! Do you mind adding a test to clarify how this would be used?
|
(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:
Do you have a preference? I'm happy to take whichever approach best fits the project's direction. |
|
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. |
d472b42 to
c920028
Compare
|
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. |
|
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. |
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())
c920028 to
51ae2f0
Compare
|
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. |
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:
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.