pytest-dev / pytest-dev/pytest

Cannot collect test classes that are inherited from `Protocol`

Open
#12,807 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

System Info
  • Pytest Version: 8.3.3
  • OS: Windows 10

The problem

Currently pytest doesn't collect classes that contain __init__ which seems to conflict with typing.Protocol:

class _BaseTest(Protocol):
    sample_01: bytes
    cls: type[Union[HtpasswdFile, HtdigestFile]]


@typing.final
class TestHtpasswdFile(_BaseTest):
    sample_01 = SAMPLE_01
    cls = HtpasswdFile

typing.Protocol could be very useful if you want to have fields without having to create a method for them with @property.
Protocol seems to be patching __init__ for it's own one that raises a TypeError if it's a protocol, so perhaps an exception could be made for protocol classes here?

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

Reproduce the issue using the _BaseTest(Protocol) and TestHtpasswdFile example on Python with pytest 8.3.3. Trace pytest's test-class collection behavior around classes with __init__; done means inherited Protocol-based test classes are collected without breaking normal class collection.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Bug
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.