-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot collect test classes that are inherited from Protocol
#12807
Comments
Hi, @ThirVondukr It doesn't make sense to treat only protocol classes as exceptions. Maybe you can try this approach: class _BaseTest(Protocol):
sample_01: bytes
cls: type[Union[HtpasswdFile, HtdigestFile]]
delattr(_BaseTest, '__init__') # Remove unnecessary __init__ from parent class |
Simply deleting init from a class seems like a hack 🤔, it's not a huge issue but I think it's something that pytest should probably support |
It's tricky to pick reasonable exceptions What's the intent for using a protocol there |
@RonnyPfannschmidt It's a way for defining an interface for child test classes (I want to parametrize a bunch of tests that way) and type check them that way, it's just more brief compared to using class ABCTest(ABC):
@property
@abstractmethod
def a() -> A:
raise NotImplementedError
@property
@abstractmethod
def b() -> B:
raise NotImplementedError class ProtocolTest(Protocol):
a: A
b: B |
Honestly I could look into parametrized fixtures instead but classes seemed more explicit to me atm 🤔 |
I believe it's a good idea for a new feature, lets start by just supporting protocol as that didn't exist when init was blocked |
Implementation wise I think you could either import from typing import Protocol
class A(Protocol):
pass
class B(Protocol):
pass
assert A.__init__ is B.__init__ |
System Info
The problem
Currently pytest doesn't collect classes that contain
__init__
which seems to conflict withtyping.Protocol
: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 aTypeError
if it's a protocol, so perhaps an exception could be made for protocol classes here?The text was updated successfully, but these errors were encountered: