'How can I perform a type guard on a property of an object in Python
PEP 647 introduced type guards to perform complex type narrowing operations using functions. If I have a class where properties can have various types, is there a way that I can perform a similar type narrowing operation on the property of an object given as the function argument?
class MyClass:
a: Optional[int]
b: Optional[str]
# Some other things
def someTypeGuard(my_obj: MyClass) -> ???:
return my_obj.a is not None
I'm thinking it might be necessary for me to implement something to do with square brackets in type hints, but I really don't know where to start on this.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
