Does assert_type
is supposed to work with Protocol
?
#1819
-
Does from typing import Protocol, assert_type
class MyProtocol(Protocol):
def my_method(self) -> None:
...
class Concrete:
def my_method(self) -> None:
...
x: MyProtocol = Concrete() # OK
assert_type(Concrete(), MyProtocol) # "assert_type" mismatch: expected "MyProtocol" but received "Concrete" MyPy has a similar error: error: Expression is of type "Concrete", not "MyProtocol" [assert-type]
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
There is a specification at https://typing.readthedocs.io/en/latest/spec/directives.html#assert-type but it could be made more precise. |
Beta Was this translation helpful? Give feedback.
assert_type()
works with protocols, but it checks for an exact match, not for assignability. To check thatConcrete
is assignable to MyProtocol, it is enough to do e.g.x: MyProtocol = Concrete()
.There is a specification at https://typing.readthedocs.io/en/latest/spec/directives.html#assert-type but it could be made more precise.