Skip to content

Commit

Permalink
Adding type annotations to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
delfick committed Oct 12, 2024
1 parent 9f8541d commit 5a1790b
Show file tree
Hide file tree
Showing 19 changed files with 274 additions and 196 deletions.
27 changes: 19 additions & 8 deletions helpers/alt_pytest_asyncio_test_driver/contextvars_for_test.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
import string
from collections.abc import Iterable
from contextvars import ContextVar

allvars = {}
allvars: dict[str, ContextVar[str]] = {}


class Empty:
pass


def assertVarsEmpty(excluding=None):
def assertVarsEmpty(excluding: Iterable[str] | None = None) -> None:
for letter, var in allvars.items():
if not excluding or letter not in excluding:
assert var.get(Empty) is Empty


for letter in string.ascii_letters:
var = ContextVar(letter)
locals()[letter] = var
allvars[letter] = var
a: ContextVar[str] = ContextVar("a")
allvars["a"] = a

__all__ = ["allvars", "Empty", "assertVarsEmpty", *allvars]
b: ContextVar[str] = ContextVar("b")
allvars["b"] = b

c: ContextVar[str] = ContextVar("c")
allvars["c"] = c

d: ContextVar[str] = ContextVar("d")
allvars["d"] = d

e: ContextVar[str] = ContextVar("e")
allvars["e"] = e

f: ContextVar[str] = ContextVar("f")
allvars["f"] = f
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
=======* ERRORS =====*
_______* ERROR at setup of test_fails_on_fixture_returns _____*
*test_fails.py:14: in fixture_returns
*test_fails.py:17: in fixture_returns
await one*()
*test_fails.py:5: in one
return await two*()
*test_fails.py:9: in two
*test_fails.py:8: in one
await two*()
*test_fails.py:12: in two
raise ValueError*("WAT")
E ValueError: WAT
_______* ERROR at setup of test_fails_on_fixture_yields _____*
*test_fails.py:19: in fixture_yields
*test_fails.py:22: in fixture_yields
yield await one*()
*test_fails.py:5: in one
return await two*()
*test_fails.py:9: in two
*test_fails.py:8: in one
await two*()
*test_fails.py:12: in two
raise ValueError*("WAT")
E ValueError: WAT
_______* ERROR at teardown of test_fails_on_fixture_fails_in_finally _____*
*test_fails.py:27: in fixture_fails_in_finally
*test_fails.py:30: in fixture_fails_in_finally
await one*()
*test_fails.py:5: in one
return await two*()
*test_fails.py:9: in two
*test_fails.py:8: in one
await two*()
*test_fails.py:12: in two
raise ValueError*("WAT")
E ValueError: WAT
=======* 1 passed, 3 error* in * ===*
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
from collections.abc import AsyncGenerator
from typing import NoReturn

import pytest


async def one():
return await two()
async def one() -> NoReturn:
await two()


async def two():
async def two() -> NoReturn:
raise ValueError("WAT")


@pytest.fixture()
async def fixture_returns():
async def fixture_returns() -> NoReturn:
await one()


@pytest.fixture()
async def fixture_yields():
async def fixture_yields() -> AsyncGenerator[None]:
yield await one()


@pytest.fixture()
async def fixture_fails_in_finally():
async def fixture_fails_in_finally() -> AsyncGenerator[int]:
try:
yield 1
finally:
await one()


def test_fails_on_fixture_returns(fixture_returns):
def test_fails_on_fixture_returns(fixture_returns: int) -> None:
pass


def test_fails_on_fixture_yields(fixture_yields):
def test_fails_on_fixture_yields(fixture_yields: int) -> None:
pass


def test_fails_on_fixture_fails_in_finally(fixture_fails_in_finally):
def test_fails_on_fixture_fails_in_finally(fixture_fails_in_finally: int) -> None:
pass
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import asyncio
from collections.abc import AsyncGenerator

import pytest


@pytest.fixture(scope="session")
@pytest.mark.async_timeout(0.01)
async def fixture_timeout_in_setup_session():
async def fixture_timeout_in_setup_session() -> AsyncGenerator[int]:
await asyncio.sleep(1)
yield 1


@pytest.fixture(scope="session")
@pytest.mark.async_timeout(0.01)
async def fixture_timeout_session():
async def fixture_timeout_session() -> int:
await asyncio.sleep(1)
return 1


@pytest.fixture(scope="session", autouse=True)
@pytest.mark.async_timeout(0.01)
async def fixture_timeout_in_teardown_session():
async def fixture_timeout_in_teardown_session() -> AsyncGenerator[int]:
try:
yield 1
finally:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,84 +2,84 @@
_______* ERROR at teardown of test_one ____*
*/async_converters.py:*: in raise_error
assert False, f"Took too long to complete: {fle}:{lineno}"
E AssertionError: Took too long to complete: */example_timeouts*/test_fails.py:6
E AssertionError: Took too long to complete: */example_timeouts*/test_fails.py:7
_______* ERROR at setup of test_two ____*
*/async_converters.py:*: in raise_error
assert False, f"Took too long to complete: {fle}:{lineno}"
E AssertionError: Took too long to complete: */example_timeouts*/test_fails.py:15
E AssertionError: Took too long to complete: */example_timeouts*/test_fails.py:16
_______* ERROR at setup of test_three ____*
*/async_converters.py:*: in raise_error
assert False, f"Took too long to complete: {fle}:{lineno}"
E AssertionError: Took too long to complete: */example_timeouts*/test_fails.py:22
E AssertionError: Took too long to complete: */example_timeouts*/test_fails.py:23
_______* ERROR at setup of test_four ____*
*/async_converters.py:*: in raise_error
assert False, f"Took too long to complete: {fle}:{lineno}"
E AssertionError: Took too long to complete: */example_timeouts*/test_fails.py:29
E AssertionError: Took too long to complete: */example_timeouts*/test_fails.py:30
_______* ERROR at setup of test_five ____*
*/async_converters.py:*: in raise_error
assert False, f"Took too long to complete: {fle}:{lineno}"
E AssertionError: Took too long to complete: */example_timeouts*/test_fails.py:45
E AssertionError: Took too long to complete: */example_timeouts*/test_fails.py:46
_______* ERROR at setup of test_six ____*
*/async_converters.py:*: in raise_error
assert False, f"Took too long to complete: {fle}:{lineno}"
E AssertionError: Took too long to complete: */example_timeouts*/conftest.py:6
E AssertionError: Took too long to complete: */example_timeouts*/conftest.py:7
_______* ERROR at setup of test_seven ____*
*/async_converters.py:*: in raise_error
assert False, f"Took too long to complete: {fle}:{lineno}"
E AssertionError: Took too long to complete: */example_timeouts*/conftest.py:13
E AssertionError: Took too long to complete: */example_timeouts*/conftest.py:14
_______* ERROR at teardown of test_seven ____*
*/async_converters.py:* in raise_error
assert False, f"Took too long to complete: {fle}:{lineno}"
E AssertionError: Took too long to complete: */test_fails.py:36
E AssertionError: Took too long to complete: */test_fails.py:37
_______* ERROR at setup of TestAClass.test_2one ____*
*/async_converters.py:*: in raise_error
assert False, f"Took too long to complete: {fle}:{lineno}"
E AssertionError: Took too long to complete: */example_timeouts*/test_fails_method_fixtures.py:7
E AssertionError: Took too long to complete: */example_timeouts*/test_fails_method_fixtures.py:8
_______* ERROR at setup of TestAClass.test_2two ____*
*/async_converters.py:*: in raise_error
assert False, f"Took too long to complete: {fle}:{lineno}"
E AssertionError: Took too long to complete: */example_timeouts*/test_fails_method_fixtures.py:13
E AssertionError: Took too long to complete: */example_timeouts*/test_fails_method_fixtures.py:14
_______* ERROR at teardown of TestAClass.test_2three ____*
*/async_converters.py:*: in raise_error
assert False, f"Took too long to complete: {fle}:{lineno}"
E AssertionError: Took too long to complete: */example_timeouts*/test_fails_method_fixtures.py:19
E AssertionError: Took too long to complete: */example_timeouts*/test_fails_method_fixtures.py:20
_______* ERROR at teardown of test_one ____*
*/async_converters.py:*: in raise_error
assert False, f"Took too long to complete: {fle}:{lineno}"
E AssertionError: Took too long to complete: */example_timeouts*/test_surrounding_pytestmark.py:8
E AssertionError: Took too long to complete: */example_timeouts*/test_surrounding_pytestmark.py:9
_______* ERROR at setup of test_two ____*
*/async_converters.py:*: in raise_error
assert False, f"Took too long to complete: {fle}:{lineno}"
E AssertionError: Took too long to complete: */example_timeouts*/test_surrounding_pytestmark.py:16
E AssertionError: Took too long to complete: */example_timeouts*/test_surrounding_pytestmark.py:17
_______* ERROR at setup of test_three ____*
*/async_converters.py:*: in raise_error
assert False, f"Took too long to complete: {fle}:{lineno}"
E AssertionError: Took too long to complete: */example_timeouts*/test_surrounding_pytestmark.py:22
E AssertionError: Took too long to complete: */example_timeouts*/test_surrounding_pytestmark.py:23
_______* ERROR at setup of test_four ____*
*/async_converters.py:*: in raise_error
assert False, f"Took too long to complete: {fle}:{lineno}"
E AssertionError: Took too long to complete: */example_timeouts*/test_surrounding_pytestmark.py:28
E AssertionError: Took too long to complete: */example_timeouts*/test_surrounding_pytestmark.py:29
_______* ERROR at setup of test_five ____*
*/async_converters.py:*: in raise_error
assert False, f"Took too long to complete: {fle}:{lineno}"
E AssertionError: Took too long to complete: */example_timeouts*/test_surrounding_pytestmark.py:42
E AssertionError: Took too long to complete: */example_timeouts*/test_surrounding_pytestmark.py:43
_______* ERROR at setup of test_six ____*
*/async_converters.py:*: in raise_error
assert False, f"Took too long to complete: {fle}:{lineno}"
E AssertionError: Took too long to complete: */example_timeouts*/conftest.py:6
E AssertionError: Took too long to complete: */example_timeouts*/conftest.py:7
_______* ERROR at setup of test_seven ____*
*/async_converters.py:*: in raise_error
assert False, f"Took too long to complete: {fle}:{lineno}"
E AssertionError: Took too long to complete: */example_timeouts*/conftest.py:13
E AssertionError: Took too long to complete: */example_timeouts*/conftest.py:14
_______* ERROR at teardown of test_seven ____*
+ Exception Group Traceback (most recent call last):
|*ExceptionGroup: errors during test teardown (2 sub-exceptions)
+-+---------------- 1 ----------------
| Traceback (most recent call last):
| AssertionError: Took too long to complete: */conftest.py:20
| AssertionError: Took too long to complete: */conftest.py:21
+---------------- 2 ----------------
| Traceback (most recent call last):
| AssertionError: Took too long to complete: */test_surrounding_pytestmark.py:34
| AssertionError: Took too long to complete: */test_surrounding_pytestmark.py:35
+------------------------------------
=======* FAILURES ====*
_______* test_takes_closest_pytestmark ____*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import asyncio
from collections.abc import AsyncGenerator

import pytest


@pytest.fixture()
@pytest.mark.async_timeout(0.01)
async def fixture_timeout_in_finally():
async def fixture_timeout_in_finally() -> AsyncGenerator[int]:
try:
yield 1
finally:
Expand All @@ -14,28 +15,28 @@ async def fixture_timeout_in_finally():

@pytest.fixture()
@pytest.mark.async_timeout(0.01)
async def fixture_timeout_in_setup():
async def fixture_timeout_in_setup() -> AsyncGenerator[int]:
await asyncio.sleep(1)
yield 1


@pytest.fixture()
@pytest.mark.async_timeout(0.01)
async def fixture_timeout():
async def fixture_timeout() -> int:
await asyncio.sleep(1)
return 1


@pytest.fixture(scope="module")
@pytest.mark.async_timeout(0.01)
async def fixture_timeout_in_setup_module():
async def fixture_timeout_in_setup_module() -> AsyncGenerator[int]:
await asyncio.sleep(1)
yield 1


@pytest.fixture(scope="module", autouse=True)
@pytest.mark.async_timeout(0.01)
async def fixture_timeout_in_teardown_module():
async def fixture_timeout_in_teardown_module() -> AsyncGenerator[int]:
try:
yield 1
finally:
Expand All @@ -44,34 +45,34 @@ async def fixture_timeout_in_teardown_module():

@pytest.fixture(scope="module")
@pytest.mark.async_timeout(0.01)
async def fixture_timeout_module():
async def fixture_timeout_module() -> int:
await asyncio.sleep(1)
return 1


def test_one(fixture_timeout_in_finally):
def test_one(fixture_timeout_in_finally: int) -> None:
pass


def test_two(fixture_timeout_in_setup):
def test_two(fixture_timeout_in_setup: int) -> None:
pass


def test_three(fixture_timeout):
def test_three(fixture_timeout: int) -> None:
pass


def test_four(fixture_timeout_in_setup_module):
def test_four(fixture_timeout_in_setup_module: int) -> None:
pass


def test_five(fixture_timeout_module):
def test_five(fixture_timeout_module: int) -> None:
pass


def test_six(fixture_timeout_in_setup_session):
def test_six(fixture_timeout_in_setup_session: int) -> None:
pass


def test_seven(fixture_timeout_session):
def test_seven(fixture_timeout_session: int) -> None:
pass
Loading

0 comments on commit 5a1790b

Please sign in to comment.