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:
"""
If `a` is `None` then `b` is `str`
"""
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.