Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multitoken substitution #74

Open
david-alm opened this issue May 7, 2021 · 4 comments
Open

Multitoken substitution #74

david-alm opened this issue May 7, 2021 · 4 comments

Comments

@david-alm
Copy link

Hey 👋

I recently came across unasync and wanted to commend you on this incredibly promising & useful project.

In that vein, I wanted to note that certain substitutions (notably additional_replacements={"@pytest.mark.trio": ""}) effectively results in a no-op as a result of the tokenization converting @pytest.mark.trio to the tokens @pytest, mark and trio.
Although token-based execution is theoretically superior, it adds a layer of complexity that httpcore's unasync.py doesn't face.

My recommendation would be to also tokenize the additional_replacements and attempt to match the token streams.
Happy to implement it once we're aligned on a solution.

@sethmlarson
Copy link
Contributor

Multi-token replacements are a little more complex, but if you want a workaround for this now you can use the following:

# pytest marks
pytest_mark_trio = pytest.mark.trio

def pytest_mark_sync(f):
    return f  # no-op decorator

# unasync config

additional_replacements={"pytest_mark_trio": "pytest_mark_sync"}

# test files

@pytest_mark_trio
async def test_example(): ...

@pytest_mark_sync
def test_example(): ...

@johtso
Copy link

johtso commented May 7, 2021

I'm doing something even more basic, just replacing the trio or asyncio token with sync..

https://github.com/johtso/httpx-caching/blob/master/tests/_async/test_chunked_response.py#L11

https://github.com/johtso/httpx-caching/blob/master/gen_sync.py#L24

I guess that's probably not the best idea, and only works because I'm not referencing trio or asyncio directly anywhere. But yeah, if that's an issue, you can just assign it to a variable to make it more explicit.

@david-alm
Copy link
Author

Appreciate the workarounds. Just flagging that this doesn't behave as expected.
I'll PR this enhancement if you're okay with it.

@sethmlarson
Copy link
Contributor

That'd be great, thanks!

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

No branches or pull requests

3 participants