pytest-dev / pytest-dev/pytest

type hinting for request.param (such as SubRequest[MyType])

Open
#8,763 5 comments 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: fixtures topic: typing type: enhancement
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

I could presumably submit a PR for this if it making SubRequest a Generic is deemed a good approach.

What's the problem this feature will solve?

When type hinting a parameter for a request fixture the request.param attribute is typed as Any and I have to # type: ignore[no-any-return].

Describe the solution you'd like

Long term, a plugin would be nice to automatically relate the .param to it's source in the @pytest.fixture() decoration. Short term, Request could just be a Generic.

This would allow me to use request: _pytest.fixtures.SubRequest[MyType] instead of the type ignore and get any useful hints on the .param attribute inside the fixture as well as return type verification.

Alternative Solutions

I have not tried anything beyond the type ignore.

Additional context

None.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing _pytest.fixtures.SubRequest and the request.param attribute typing described in the issue. Compare the proposed generic annotation with the existing fixture typing approach. Done means a request annotated as SubRequest[MyType] provides MyType for request.param and supports return-type verification without a type ignore.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.