pytest-dev / pytest-dev/pytest

FixtureRequest.node should be typed to return a Node

Open
#13,888 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Currently the property node of FixtureRequest is defined as

    @property
    @abc.abstractmethod
    def node(self):
         """Underlying collection node (depends on current request scope)."""
         raise NotImplementedError()

which implicitly types the return type as Any. However, based on the name of the function and the underlying implementations it should return a nodes.Node.

Unfortunately, when I tried this on a fork, I got the following error:

mypy............................................................................Failed
- hook id: mypy
- exit code: 1

src/_pytest/junitxml.py:294: error: "Node" has no attribute "user_properties"  [attr-defined]
Found 1 error in 1 file (checked 239 source files)

This happens because junitxml actually expects to receive a nodes.NodeItem instead of a plain nodes.Node.

One quick fix for this would be to put a type assertion in junitxml, but I'm not sure if that's the preferred approach. I'd be happy to send a PR if someone could give ve some guidance on how to best resolve this.

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 inspecting the FixtureRequest.node declaration and its underlying implementations, then read src/_pytest/junitxml.py around line 294. Run the mypy check and determine how the Node versus NodeItem expectation should be represented; done means the return type is accurate and mypy passes without the reported user_properties error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.