Open rayansostenes opened 8 months ago
This is pretty much my exact use case but I hope it will also work with TypeVar
. It seems like Pylance understands whats going on if you check it with isinstance
as I show in https://github.com/python/typing/discussions/1881
It would be nice to have something like...
T = TypeVar["T", bound=models.Model)
ActiveModel = T & SupportsActiveProtocol
Suppose that we have a function called
activate
that can take any django model that implements theSupportsActiveProtocol
and make it active.Calling it with a
Person
object will work at runtime, and it type checks.But calling it with another object that also implements the
SupportsActiveProtocol
will fail at runtime, because we use theModel.save
method, which is not part of the protocol.Right now the only way to fix this is to add a
save
method to the protocol, or create a new protocol likeSupportsSaveProtocol
and create a third protocol that extends bothSupportsActiveProtocol
andSupportsSaveProtocol
.This can become very cumbersome to maintain, and it's not very DRY, since we may ending up creating a protocol that contains all the methods of the model.
The addition of a
Intersection
type would make it possible to solve this problem in a more elegant way.