From b614c77dec6df7414fba78d2fbe7989c6ee16828 Mon Sep 17 00:00:00 2001 From: Michael Seifert Date: Mon, 4 Dec 2023 08:07:51 +0100 Subject: [PATCH] [fix] Fixes a bug that caused an internal pytest error when collecting .txt files. Signed-off-by: Michael Seifert --- docs/source/reference/changelog.rst | 5 +++++ pytest_asyncio/plugin.py | 6 +++++- tests/test_doctest.py | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/docs/source/reference/changelog.rst b/docs/source/reference/changelog.rst index 79440f45..c02a528a 100644 --- a/docs/source/reference/changelog.rst +++ b/docs/source/reference/changelog.rst @@ -2,6 +2,11 @@ Changelog ========= +0.23.2 (2023-12-04) +=================== +- Fixes a bug that caused an internal pytest error when collecting .txt files `#703 `_ + + 0.23.1 (2023-12-03) =================== - Fixes a bug that caused an internal pytest error when using module-level skips `#701 `_ diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index fb0ed226..934fb91c 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -609,7 +609,11 @@ def scoped_event_loop( # collected Python class, where it will be picked up by pytest.Class.collect() # or pytest.Module.collect(), respectively try: - collector.obj.__pytest_asyncio_scoped_event_loop = scoped_event_loop + pyobject = collector.obj + # If the collected module is a DoctestTextfile, collector.obj is None + if pyobject is None: + return + pyobject.__pytest_asyncio_scoped_event_loop = scoped_event_loop except (OutcomeException, Collector.CollectError): # Accessing Module.obj triggers a module import executing module-level # statements. A module-level pytest.skip statement raises the "Skipped" diff --git a/tests/test_doctest.py b/tests/test_doctest.py index 6c26e645..5b79619a 100644 --- a/tests/test_doctest.py +++ b/tests/test_doctest.py @@ -17,3 +17,23 @@ def any_function(): ) result = pytester.runpytest("--asyncio-mode=strict", "--doctest-modules") result.assert_outcomes(passed=1) + + +def test_plugin_does_not_interfere_with_doctest_textfile_collection(pytester: Pytester): + pytester.makefile(".txt", "") # collected as DoctestTextfile + pytester.makepyfile( + __init__="", + test_python_file=dedent( + """\ + import pytest + + pytest_plugins = "pytest_asyncio" + + @pytest.mark.asyncio + async def test_anything(): + pass + """ + ), + ) + result = pytester.runpytest("--asyncio-mode=strict") + result.assert_outcomes(passed=1)