Skip to content

Switch to pytest-asyncio's strict mode for async tests #1782

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

Merged

Conversation

miguelgrinberg
Copy link
Collaborator

@miguelgrinberg miguelgrinberg commented Apr 18, 2024

The trick is to using the following for async tests:

@pytest.mark.asyncio

which is convert to the following for the unasynced tests:

@pytest.mark.sync

This eliminates issues with unused imports, which would need to be taken care of in some, but not all files. WDYT?

@miguelgrinberg miguelgrinberg requested a review from pquentin April 18, 2024 16:26
Copy link
Member

@pquentin pquentin left a comment

Choose a reason for hiding this comment

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

Thanks!

I think syncio is just too close to asyncio (to the point where my phone autocorrects it back to asyncio). I would be OK with sync or any other word with a Levenshtein distance >= 2 from asyncio.

(I would also be OK with a sed solution until unasync learns to deal with multi-token deletion rules.)

@miguelgrinberg
Copy link
Collaborator Author

A sed solution for this is tricky. Some of the test files do import pytest only to use @pytest.mark.asyncio, while others use it for other things, so it gets difficult to know when the import needs to be removed and when it has to be left alone.

Good point on the name, I'll change it.

@miguelgrinberg miguelgrinberg force-pushed the pytest-asyncio-strict-mode branch from 8002506 to 08036c7 Compare April 22, 2024 10:10
Copy link
Member

@pquentin pquentin left a comment

Choose a reason for hiding this comment

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

Thanks! LGTM.

@@ -70,6 +70,7 @@ def main(check=False):
"async_pull_request": "pull_request",
"async_examples": "examples",
"assert_awaited_once_with": "assert_called_once_with",
"pytest_asyncio": "pytest",
Copy link
Member

Choose a reason for hiding this comment

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

Why is this needed now? Also, I think @pytest_asyncio.fixture and @pytest.fixture are probably clearer than @fixture, but that can be addressed further down the road.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is because pytest-asyncio's auto mode also takes care of reconfiguring any async fixtures declared with @pytest.fixture appropriately. In strict mode you have to use @pytest_asyncio.fixture instead.

I agree that @pytest.fixture is better than @fixture here, I'll change it.

@miguelgrinberg miguelgrinberg merged commit 81181ca into elastic:main Apr 22, 2024
15 checks passed
@miguelgrinberg miguelgrinberg deleted the pytest-asyncio-strict-mode branch April 22, 2024 13:33
@miguelgrinberg miguelgrinberg added the backport 8.x Backport to 8.x label Apr 29, 2024
Copy link

The backport to 8.x failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-8.x 8.x
# Navigate to the new working tree
cd .worktrees/backport-8.x
# Create a new branch
git switch --create backport-1782-to-8.x
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 81181ca8859f6ef8683ee1b323288bb52605f25a
# Push it to GitHub
git push --set-upstream origin backport-1782-to-8.x
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-8.x

Then, create a pull request where the base branch is 8.x and the compare/head branch is backport-1782-to-8.x.

miguelgrinberg added a commit to miguelgrinberg/elasticsearch-dsl-py that referenced this pull request Apr 29, 2024
* Switch to pytest-asyncio's strict mode for async tests

* add one missing test

* use 'sync' as sync I/O marker

(cherry picked from commit 81181ca)
miguelgrinberg added a commit to miguelgrinberg/elasticsearch-dsl-py that referenced this pull request Apr 29, 2024
* Switch to pytest-asyncio's strict mode for async tests

* add one missing test

* use 'sync' as sync I/O marker

(cherry picked from commit 81181ca)
miguelgrinberg added a commit that referenced this pull request Apr 29, 2024
* Switch to pytest-asyncio's strict mode for async tests

* add one missing test

* use 'sync' as sync I/O marker

(cherry picked from commit 81181ca)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 8.x Backport to 8.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants