Hi! I’m having a little trouble understanding what I can do with the new runtime checkable protocols. The PEP says that data protocols (those with typed attributes) can be checked with isinstance
, so I’ve tried this:
from typing import Protocol, runtime_checkable
@runtime_checkable
class Foo(Protocol):
x: int
class Bar(object):
def __init__(self, x):
self.x = x
assert isinstance(Bar(10), Foo)
assert not isinstance(Bar(10.), Foo) # This fails, which I didn't expect
I feel like I’m missing something about how protocols are supposed to be used. Is there a way for me to get the result I expected? I.e. this instance has attribute x
which is an int
?
Thanks for any help!