pytest-dev / pytest-dev/pytest
Cannot collect test classes that are inherited from `Protocol`
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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