Type annotation for fixtures

See original GitHub issue

As reported by others, the fact that fixture can exist magically is confusing for many users unfamiliar with pytest and it can be gruesome to track in larger projects with many fixtures.

Since type annotation is working well in python 3 I was thinking that one explicit way would be to have a new Fixture type to help annotate the fixture arguments.

from typing import Union
import pytest

Fixture = Union

@pytest.fixture
def bob() -> str:
    return '42'

@pytest.fixture
def alice() -> int:
    return 42

def test_foo(bob: Fixture[str], alice: Fixture[int]):
    assert bob != alice

In this example I ‘abuse’ Union so that existing tools are taking the hinting without any issue. For the person coming in and reading the code, especially in larger projects, the fact that the arguments are fixtures becomes very explicit and in the spirit of the Zen of Python:

Explicit is better than implicit.

Unfortunately mypy and other type checking tools don’t seem to ‘alias’ Union since it is a special case.

This on the other hand works but I would prefer the annotation of the first example:

from typing import Union
import pytest

@pytest.fixture
def bob() -> str:
    return '42'

@pytest.fixture
def alice() -> int:
    return 42

FixtureStr = Union[str]
FixtureInt = Union[int]

def test_bar(bob: FixtureStr, alice: FixtureInt):
    assert bob != alice

IDEs such as PyCharm then now able to hint on bob being a string and as a user I can tell that it is a fixture argument.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:22 (14 by maintainers)

github_iconTop GitHub Comments

14reactions
asottilecommented, May 10, 2022

I’m personally opposed to typing tests and find it a waste of time (though I do check_untyped_defs for tests which I do think is valuable). tests are going to fail anyway if there’s a typing error so it does not provide value for me

but that’s entirely off topic for this issue. this issue is about changing the way fixtures work to not be dependency injected by name

11reactions
davidhaltercommented, Oct 17, 2019

Why not just write it as def test_bar(bob: str): ... at that point? IMO the Fixture just makes it more confusing.

I just finally have to write pytest support in Jedi. I’m using both tools daily so I should really just do myself a favor 😃.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Type hints for pytests fixtures that return methods
I would like to use type hints for all my methods. To say that a method return a method I can use Callable...
Read more >
pytest-fixture-typecheck - PyPI
pytest-fixture-typecheck is a pytest plugin that inserts runtime assertions that your fixture usages are properly typed.
Read more >
pytest fixtures: explicit, modular, scalable
Software test fixtures initialize test functions. They provide a fixed baseline so that tests execute reliably and produce consistent, repeatable, results.
Read more >
pytest fixtures with yield - return types defined as Iterator
If the fixture return type is defined as Iterator[ type ] or Iterable[ type ] , then the PyCharm inspection assumes that the...
Read more >
Fixtures From Type Annotations - Pygopar
Fixtures From Type Annotations ... post about converting function/method arguments into what their type annotations mentioned they would be.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found