Reputation: 44818
I have legacy code with inheriting dataclasses:
@dataclass
class Base:
a: int
@dataclass
class Derived1(Base):
b: int
@dataclass
class Derived2(Base):
b: int
I want to use Python type hints so that methods know when they're getting something with a b
attribute. However, I cannot import actual Derived1 or Derived2. So I want something like:
def uses_b(b_supporting_object: TIsBaseWithB):
...
How do you define a TIsBaseWithB
? Is NewType related?
Upvotes: 0
Views: 315
Reputation: 3439
Use typing.Protocol
:
from typing import Protocol
class SupportsB(Protocol):
b: int
Upvotes: 1