Skip to content

Add typing information for parameterized fixtures #8073

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

Open
tobiasdiez opened this issue Nov 25, 2020 · 6 comments
Open

Add typing information for parameterized fixtures #8073

tobiasdiez opened this issue Nov 25, 2020 · 6 comments
Labels
topic: typing type-annotation issue type: enhancement new feature or API change, should be merged into features branch

Comments

@tobiasdiez
Copy link
Contributor

What's the problem this feature will solve?

Add typing information for parameterized fixtures to have better support for type checkers.

Describe the solution you'd like

Given a fixture like

@pytest.fixture(params=["--setup-only", "--setup-plan", "--setup-show"])
def mode(request):
    return request.param

what is the type of request? I thought it would be FixtureRequest but this class doesn't have a param object. Thus I suggest to add a generic class ParametrizedFixtureRequest[T] where T is the type of the parameter. Thus in the above example one could annotate request: ParametrizedFixtureRequest[str] and then the type checker would know that request.param is a string.

Alternative Solutions

Additional context

@RonnyPfannschmidt
Copy link
Member

@bluetech would it be sensible to introduce a generic protocol here ?

@bluetech
Copy link
Member

Thanks for bringing this up @tobiasdiez.

This seems doable but would require some work:

The param is only found on SubRequest and not on FixtureRequest, and I'm not yet sufficiently familiar with it and its weird relationship with FixtureRequest to say whether and how it should be exposed.

For unparametrized fixtures, pytest currently leaves request.param entirely undefined, i.e. accessing it leads to AttributeError. That's no good for static typing: Python/mypy does not provide a way to model this, there is no undefined type like in JavaScript (luckily).

Python/mypy currently does not support giving defaults to type parameters, so this will be a bit of an ergonomic hit for annotating with FixtureRequest in the non-parametrized case when using the disallow_any_generics mypy option. It will have to be FixtureRequest[None] (or something else depending on the solution to the previous point).

@RonnyPfannschmidt
Copy link
Member

im happy to have 2 types for that, the fixture decorator could even apply it based on params and/or a flag for the param type required

then mypy would fail with missing attribute for non-parameterized fixtures while giving off the exxact type in the other case

@Zac-HD Zac-HD added topic: typing type-annotation issue type: enhancement new feature or API change, should be merged into features branch labels Nov 27, 2020
@nicoddemus
Copy link
Member

nicoddemus commented Nov 27, 2020

On a related note: I'm happy to just add a param = None to FixtureRequest rather than leave it undefined (or a UNDEFINED sentinel if we want to distinguish between no parametrization vs parametrization with a None value, but I'm not sure of a use case where the distinction makes a difference). The current AttributeError a user gets is very unfriendly.

We did something similar with cached_result.

@RonnyPfannschmidt
Copy link
Member

Parameter values may be none, we either have to have a extra flag for indication, or a singleton type

@leonarduschen
Copy link
Contributor

Any plans to implement this feature anytime soon?

nipunn1313 added a commit to nipunn1313/pytest that referenced this issue Jul 14, 2022
For now, mark it as Any until pytest-dev#8073 is solved

Fixes pytest-dev#9514
nipunn1313 added a commit to nipunn1313/pytest that referenced this issue Jul 14, 2022
For now, mark it as Any until pytest-dev#8073 is solved

Fixes pytest-dev#9514
nipunn1313 added a commit to nipunn1313/pytest that referenced this issue Jul 14, 2022
For now, mark it as Any until pytest-dev#8073 is solved

Fixes pytest-dev#9514
nicoddemus pushed a commit that referenced this issue Jul 14, 2022
For now, mark it as Any until #8073 is solved

Fixes #9514
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: typing type-annotation issue type: enhancement new feature or API change, should be merged into features branch
Projects
None yet
Development

No branches or pull requests

6 participants