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

warn if asyncio test requests async pytest fixture in strict mode #979

Conversation

jakkdl
Copy link
Member

@jakkdl jakkdl commented Nov 5, 2024

partially fixes pytest-dev/pytest#10839, see pytest-dev/pytest#10839 (comment)

It got a bit messy to implement, and relies on non-public API in _fixtureinfo, so I'm not 100% that this is the best way to approach it. Another approach would be to try and catch this on collection, but then you end up having to handle overriding fixtures and the like.

It's also possible that this breaks tests for end users, but if they really do want an unawaited object from a fixture they can resolve it in various ways by returning an Awaitable from their fixture.

@codecov-commenter
Copy link

codecov-commenter commented Nov 5, 2024

Codecov Report

Attention: Patch coverage is 60.00000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 90.83%. Comparing base (1057723) to head (b208df6).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
pytest_asyncio/plugin.py 60.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #979      +/-   ##
==========================================
- Coverage   91.15%   90.83%   -0.33%     
==========================================
  Files           2        2              
  Lines         509      513       +4     
  Branches       64       66       +2     
==========================================
+ Hits          464      466       +2     
- Misses         27       28       +1     
- Partials       18       19       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

@jakkdl
Copy link
Member Author

jakkdl commented Nov 5, 2024

actually, it's not that unlikely that this will break tests for some users if they're using @pytest.fixture(autouse=True) and mixing asyncio and async-but-not-asyncio tests. But... that's probably not a great thing to do.

Copy link
Contributor

@seifertm seifertm left a comment

Choose a reason for hiding this comment

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

I'm loosely following the linked pytest issue. I really appreciate all the work you've put into this and I will try to support the initiative from the pytest-asyncio side.

It would be great, if you could also add a corresponding entry to docs/reference/changelog.rst, since this is a user-facing change.

pytest_asyncio/plugin.py Outdated Show resolved Hide resolved
@jakkdl jakkdl changed the title fail if asyncio test requests async pytest fixture in strict mode warn if asyncio test requests async pytest fixture in strict mode Nov 8, 2024
@jakkdl jakkdl requested a review from seifertm November 8, 2024 12:44
@seifertm
Copy link
Contributor

This looks great, thank you!

You mentioned in pytest-dev/pytest#12930 (comment) that this change may become redundant once the same check is in place in pytest. I'm generally okay with having the check in pytest-asyncio, too, at least temporarily. At some point, pytest-asyncio will bump the minimum required pytest version to a version where this check is included in pytest and we can remove it here.

I'd like to wait for a decision on your pytest PR before merging (whether you go with a deprecation or plain error), but other than that it looks good to me.

Thanks for the effort!

@jakkdl
Copy link
Member Author

jakkdl commented Nov 15, 2024

Looks like we're going with a warning in the upstream PR.
It's probably good to have the check in pytest-asyncio, even if somewhat redundant, since for this case we can give a more specific & actionable error message for the user.

fix test

make it raise PytestDeprecationWarning instead

add changelog entry

docs: Changed release date of v0.25.0 to UNRELEASED.
@seifertm seifertm force-pushed the error_strict_asyncio_test_request_async_pytest_fixture branch from 3fedb5a to b208df6 Compare November 21, 2024 05:57
@seifertm
Copy link
Contributor

I adjusted the release date of v0.25.0 to UNRELEASED and squashed the commits.
Thanks again @jakkdl !

@seifertm seifertm enabled auto-merge November 21, 2024 05:59
@seifertm seifertm added this pull request to the merge queue Nov 21, 2024
Merged via the queue into pytest-dev:main with commit ba8c0a1 Nov 21, 2024
15 checks passed
@jakkdl jakkdl deleted the error_strict_asyncio_test_request_async_pytest_fixture branch November 21, 2024 12:21
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.

Add a warning when setting up async fixtures
3 participants