argument type that is constrained to a class and protocol #1881
-
Not sure if I explained it correctly in the title, so here is some sample code. from typing import Protocol, TypeVar
class HasJob(Protocol):
def job(self) -> str: ...
class Person:
def __init__(self, name: str, age: int):
self.name = name
self.age = age
class Engineer(Person):
def __init__(self, name: str, age: int):
super().__init__(name, age)
def job(self) -> str:
return "Engineer"
# T = TypeVar("T", bound=HasJob)
# A function that accepts a Person with a job method
def go_to_work(person: Person):
print(f"{person.name} is going to work as a {person.job()}")
bob = Engineer("Bob", 30)
go_to_work(bob) I would like my I have not found a way to do it with |
Beta Was this translation helpful? Give feedback.
Answered by
JelleZijlstra
Nov 7, 2024
Replies: 1 comment 2 replies
-
What you're looking for is called an intersection type. This is not currently part of the Python type system, but it's a popular idea: #213. |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
shadycuz
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What you're looking for is called an intersection type. This is not currently part of the Python type system, but it's a popular idea: #213.