typing.Protocol subclasses are not collected.
See original GitHub issue- a detailed description of the bug or problem you are having:
Extending
Protocoland using it with mixin classes won’t collect tests from mixins. - output of
pip listfrom the virtual environment you are using
attrs 22.1.0 Classes Without Boilerplate
iniconfig 1.1.1 iniconfig: brain-dead simple config-ini parsing
packaging 21.3 Core utilities for Python packages
pluggy 1.0.0 plugin and hook calling mechanisms for python
py 1.11.0 library with cross-python path, ini-parsing, io, code, log facilities
pyparsing 3.0.9 pyparsing module - Classes and methods to define and execute parsing grammars
pytest 7.1.3 pytest: simple powerful testing with Python
tomli 2.0.1 A lil' TOML parser
- pytest and operating system versions:
pytest 7.1.2, ubuntu wayland 22.04, Python 3.10
- minimal example if possible:
File: test_foo.py
from typing import Protocol
class Proto(Protocol):
a: int = 2
b: int = 3
def parse(self) -> int:
...
class TestBase(Proto):
@classmethod
def add(cls, a, b):
return a + b
@classmethod
def subtract(cls, a, b):
return a - b
class SubtractTestCase(TestBase):
def parse(self) -> int:
return self.subtract(self.a, self.b)
class AddTestCase(TestBase):
def parse(self) -> int:
return self.add(self.a, self.b)
class OneTestMixin(Proto):
a = 3
b = 3
def test_parse(self):
assert self.parse() in (0, 6)
class TestOneAdd(AddTestCase, OneTestMixin):
...
class TestOneSubstract(SubtractTestCase, OneTestMixin):
...
run py.test no test would be collected.
If you would omit Protocol from Proto everything will pass.
repo with the code: https://github.com/nrbnlulu/pytest-protocol-issue
Issue Analytics
- State:
- Created a year ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Protocols and structural subtyping - mypy 0.991 documentation
Protocols and structural subtyping#. Mypy supports two ways of deciding whether two classes are compatible as types: nominal subtyping and structural ...
Read more >PEP 544 – Protocols: Structural subtyping (static duck typing)
Protocol as an explicit base class. Without this base, the class is “downgraded” to a regular ABC that cannot be used with structural...
Read more >mypy Protocol - Does extending a Protocol not make it a ...
In the following setup I define a protocol LambdaContext , and extend this protocol in LogContext by subclassing. I then define a function ......
Read more >Should concrete implementations be required to ... - GitHub
It imposes a restriction on protocol subclasses at type-checking time which has no basis in actual runtime behavior.
Read more >Protocol Types in Python 3.8 - Auth0
Protocols are defined by including the special typing.Protocol class in the base class list. The annotated class does not lose any semantics of ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

The benefit of protocol typically is that you intentionally do not have it on the implementation classes
Yeah, I just realized this.