Skip to content

Commit

Permalink
[typing.Union -> |] Add 'from __future__ import annotations' everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas authored and seifertm committed Oct 28, 2024
1 parent 58bac59 commit 720e28c
Show file tree
Hide file tree
Showing 39 changed files with 112 additions and 44 deletions.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ lint.select = [
"D", # pydocstyle
"E", # pycodestyle
"F", # pyflakes
"FA100", # add future annotations
"PGH004", # pygrep-hooks - Use specific rule codes when using noqa
"PIE", # flake8-pie
"PLE", # pylint error
"PYI", # flake8-pyi
"RUF", # ruff
"T100", # flake8-debugger
Expand Down
2 changes: 2 additions & 0 deletions pytest_asyncio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""The main point for importing pytest-asyncio items."""

from __future__ import annotations

from ._version import version as __version__ # noqa: F401
from .plugin import fixture, is_async_test

Expand Down
80 changes: 36 additions & 44 deletions pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""pytest-asyncio implementation."""

from __future__ import annotations

import asyncio
import contextlib
import enum
Expand All @@ -22,7 +24,6 @@
Any,
Callable,
Literal,
Optional,
TypeVar,
Union,
overload,
Expand Down Expand Up @@ -110,41 +111,41 @@ def pytest_addoption(parser: Parser, pluginmanager: PytestPluginManager) -> None
def fixture(
fixture_function: FixtureFunction,
*,
scope: "Union[_ScopeName, Callable[[str, Config], _ScopeName]]" = ...,
loop_scope: Union[_ScopeName, None] = ...,
params: Optional[Iterable[object]] = ...,
scope: _ScopeName | Callable[[str, Config], _ScopeName] = ...,
loop_scope: _ScopeName | None = ...,
params: Iterable[object] | None = ...,
autouse: bool = ...,
ids: Union[
Iterable[Union[str, float, int, bool, None]],
Callable[[Any], Optional[object]],
None,
] = ...,
name: Optional[str] = ...,
ids: (
Iterable[str | float | int | bool | None]
| Callable[[Any], object | None]
| None
) = ...,
name: str | None = ...,
) -> FixtureFunction: ...


@overload
def fixture(
fixture_function: None = ...,
*,
scope: "Union[_ScopeName, Callable[[str, Config], _ScopeName]]" = ...,
loop_scope: Union[_ScopeName, None] = ...,
params: Optional[Iterable[object]] = ...,
scope: _ScopeName | Callable[[str, Config], _ScopeName] = ...,
loop_scope: _ScopeName | None = ...,
params: Iterable[object] | None = ...,
autouse: bool = ...,
ids: Union[
Iterable[Union[str, float, int, bool, None]],
Callable[[Any], Optional[object]],
None,
] = ...,
name: Optional[str] = None,
ids: (
Iterable[str | float | int | bool | None]
| Callable[[Any], object | None]
| None
) = ...,
name: str | None = None,
) -> FixtureFunctionMarker: ...


def fixture(
fixture_function: Optional[FixtureFunction] = None,
loop_scope: Union[_ScopeName, None] = None,
fixture_function: FixtureFunction | None = None,
loop_scope: _ScopeName | None = None,
**kwargs: Any,
) -> Union[FixtureFunction, FixtureFunctionMarker]:
) -> FixtureFunction | FixtureFunctionMarker:
if fixture_function is not None:
_make_asyncio_fixture_function(fixture_function, loop_scope)
return pytest.fixture(fixture_function, **kwargs)
Expand All @@ -163,9 +164,7 @@ def _is_asyncio_fixture_function(obj: Any) -> bool:
return getattr(obj, "_force_asyncio_fixture", False)


def _make_asyncio_fixture_function(
obj: Any, loop_scope: Union[_ScopeName, None]
) -> None:
def _make_asyncio_fixture_function(obj: Any, loop_scope: _ScopeName | None) -> None:
if hasattr(obj, "__func__"):
# instance method, check the function object
obj = obj.__func__
Expand Down Expand Up @@ -288,7 +287,7 @@ def _add_kwargs(
return ret


def _perhaps_rebind_fixture_func(func: _T, instance: Optional[Any]) -> _T:
def _perhaps_rebind_fixture_func(func: _T, instance: Any | None) -> _T:
if instance is not None:
# The fixture needs to be bound to the actual request.instance
# so it is bound to the same object as the test method.
Expand Down Expand Up @@ -388,9 +387,7 @@ class PytestAsyncioFunction(Function):
"""Base class for all test functions managed by pytest-asyncio."""

@classmethod
def item_subclass_for(
cls, item: Function, /
) -> Union[type["PytestAsyncioFunction"], None]:
def item_subclass_for(cls, item: Function, /) -> type[PytestAsyncioFunction] | None:
"""
Returns a subclass of PytestAsyncioFunction if there is a specialized subclass
for the specified function item.
Expand Down Expand Up @@ -525,10 +522,8 @@ def runtest(self) -> None:
# see https://github.com/pytest-dev/pytest/issues/11307
@pytest.hookimpl(specname="pytest_pycollect_makeitem", tryfirst=True)
def pytest_pycollect_makeitem_preprocess_async_fixtures(
collector: Union[pytest.Module, pytest.Class], name: str, obj: object
) -> Union[
pytest.Item, pytest.Collector, list[Union[pytest.Item, pytest.Collector]], None
]:
collector: pytest.Module | pytest.Class, name: str, obj: object
) -> pytest.Item | pytest.Collector | list[pytest.Item | pytest.Collector] | None:
"""A pytest hook to collect asyncio coroutines."""
if not collector.funcnamefilter(name):
return None
Expand All @@ -540,20 +535,17 @@ def pytest_pycollect_makeitem_preprocess_async_fixtures(
# see https://github.com/pytest-dev/pytest/issues/11307
@pytest.hookimpl(specname="pytest_pycollect_makeitem", hookwrapper=True)
def pytest_pycollect_makeitem_convert_async_functions_to_subclass(
collector: Union[pytest.Module, pytest.Class], name: str, obj: object
collector: pytest.Module | pytest.Class, name: str, obj: object
) -> Generator[None, pluggy.Result, None]:
"""
Converts coroutines and async generators collected as pytest.Functions
to AsyncFunction items.
"""
hook_result = yield
try:
node_or_list_of_nodes: Union[
pytest.Item,
pytest.Collector,
list[Union[pytest.Item, pytest.Collector]],
None,
] = hook_result.get_result()
node_or_list_of_nodes: (
pytest.Item | pytest.Collector | list[pytest.Item | pytest.Collector] | None
) = hook_result.get_result()
except BaseException as e:
hook_result.force_exception(e)
return
Expand Down Expand Up @@ -592,7 +584,7 @@ def pytest_pycollect_makeitem_convert_async_functions_to_subclass(

# A stack used to push package-scoped loops during collection of a package
# and pop those loops during collection of a Module
__package_loop_stack: list[Union[FixtureFunctionMarker, FixtureFunction]] = []
__package_loop_stack: list[FixtureFunctionMarker | FixtureFunction] = []


@pytest.hookimpl
Expand Down Expand Up @@ -868,7 +860,7 @@ def _provide_clean_event_loop() -> None:


def _get_event_loop_no_warn(
policy: Optional[AbstractEventLoopPolicy] = None,
policy: AbstractEventLoopPolicy | None = None,
) -> asyncio.AbstractEventLoop:
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
Expand All @@ -879,7 +871,7 @@ def _get_event_loop_no_warn(


@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_pyfunc_call(pyfuncitem: Function) -> Optional[object]:
def pytest_pyfunc_call(pyfuncitem: Function) -> object | None:
"""
Pytest hook called before a test case is run.
Expand Down Expand Up @@ -999,7 +991,7 @@ def _get_marked_loop_scope(asyncio_marker: Mark) -> _ScopeName:
return scope


def _retrieve_scope_root(item: Union[Collector, Item], scope: str) -> Collector:
def _retrieve_scope_root(item: Collector | Item, scope: str) -> Collector:
node_type_by_scope = {
"class": Class,
"module": Module,
Expand Down
2 changes: 2 additions & 0 deletions tests/async_fixtures/test_async_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import asyncio
import unittest.mock

Expand Down
2 changes: 2 additions & 0 deletions tests/async_fixtures/test_async_fixtures_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module-scoped too.
"""

from __future__ import annotations

import asyncio

import pytest
Expand Down
2 changes: 2 additions & 0 deletions tests/async_fixtures/test_async_fixtures_with_finalizer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import asyncio
import functools

Expand Down
2 changes: 2 additions & 0 deletions tests/async_fixtures/test_async_gen_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import unittest.mock

import pytest
Expand Down
2 changes: 2 additions & 0 deletions tests/async_fixtures/test_nested.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import asyncio

import pytest
Expand Down
2 changes: 2 additions & 0 deletions tests/async_fixtures/test_parametrized_loop.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from textwrap import dedent

from pytest import Pytester
Expand Down
2 changes: 2 additions & 0 deletions tests/async_fixtures/test_shared_module_fixture.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from textwrap import dedent

from pytest import Pytester
Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import asyncio

import pytest
Expand Down
2 changes: 2 additions & 0 deletions tests/hypothesis/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
sync shim for Hypothesis.
"""

from __future__ import annotations

from textwrap import dedent

import pytest
Expand Down
2 changes: 2 additions & 0 deletions tests/loop_fixture_scope/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import asyncio

import pytest
Expand Down
2 changes: 2 additions & 0 deletions tests/loop_fixture_scope/test_loop_fixture_scope.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Unit tests for overriding the event loop with a larger scoped one."""

from __future__ import annotations

import asyncio

import pytest
Expand Down
2 changes: 2 additions & 0 deletions tests/markers/test_class_scope.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Test if pytestmark works when defined on a class."""

from __future__ import annotations

import asyncio
from textwrap import dedent

Expand Down
2 changes: 2 additions & 0 deletions tests/markers/test_function_scope.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from textwrap import dedent

from pytest import Pytester
Expand Down
2 changes: 2 additions & 0 deletions tests/markers/test_invalid_arguments.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from textwrap import dedent

import pytest
Expand Down
2 changes: 2 additions & 0 deletions tests/markers/test_module_scope.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from textwrap import dedent

from pytest import Pytester
Expand Down
2 changes: 2 additions & 0 deletions tests/markers/test_package_scope.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from textwrap import dedent

from pytest import Pytester
Expand Down
2 changes: 2 additions & 0 deletions tests/markers/test_session_scope.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from textwrap import dedent

from pytest import Pytester
Expand Down
2 changes: 2 additions & 0 deletions tests/modes/test_auto_mode.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from textwrap import dedent

from pytest import Pytester
Expand Down
2 changes: 2 additions & 0 deletions tests/modes/test_strict_mode.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from textwrap import dedent

from pytest import Pytester
Expand Down
2 changes: 2 additions & 0 deletions tests/test_asyncio_fixture.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import asyncio
from textwrap import dedent

Expand Down
2 changes: 2 additions & 0 deletions tests/test_asyncio_mark.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from textwrap import dedent

from pytest import Pytester
Expand Down
2 changes: 2 additions & 0 deletions tests/test_dependent_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import asyncio

import pytest
Expand Down
2 changes: 2 additions & 0 deletions tests/test_doctest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from textwrap import dedent

from pytest import Pytester
Expand Down
2 changes: 2 additions & 0 deletions tests/test_event_loop_fixture.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from textwrap import dedent

from pytest import Pytester
Expand Down
2 changes: 2 additions & 0 deletions tests/test_event_loop_fixture_finalizer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from textwrap import dedent

from pytest import Pytester
Expand Down
2 changes: 2 additions & 0 deletions tests/test_event_loop_fixture_override_deprecation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from textwrap import dedent

from pytest import Pytester
Expand Down
2 changes: 2 additions & 0 deletions tests/test_explicit_event_loop_fixture_request.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from textwrap import dedent

from pytest import Pytester
Expand Down
2 changes: 2 additions & 0 deletions tests/test_fixture_loop_scopes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from textwrap import dedent

import pytest
Expand Down
2 changes: 2 additions & 0 deletions tests/test_import.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from textwrap import dedent

from pytest import Pytester
Expand Down
2 changes: 2 additions & 0 deletions tests/test_is_async_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from textwrap import dedent

from pytest import Pytester
Expand Down
2 changes: 2 additions & 0 deletions tests/test_multiloop.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from textwrap import dedent

from pytest import Pytester
Expand Down
Loading

0 comments on commit 720e28c

Please sign in to comment.