question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

typing.Protocol subclasses are not collected.

See original GitHub issue
  • a detailed description of the bug or problem you are having: Extending Protocol and using it with mixin classes won’t collect tests from mixins.
  • output of pip list from 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:closed
  • Created a year ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
RonnyPfannschmidtcommented, Sep 12, 2022

The benefit of protocol typically is that you intentionally do not have it on the implementation classes

0reactions
nrbnlulucommented, Sep 12, 2022

Yeah, I just realized this.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found