Ok consider this exmaple:
from typing import Optional
class FooBar:
name: Optional[str]
def __init__(self, name: Optional[str] = None) -> None:
self.name = name
def foo(name: str) -> Optional[FooBar]:
if name == "foo":
return FooBar()
return None
bar: str = foo("foo").name
It errors out with
main.py:13: error: Missing positional argument "name" in call to "FooBar"
main.py:17: error: Item "None" of "Optional[FooBar]" has no attribute "name"
main.py:17: error: Incompatible types in assignment (expression has type "Union[str, None, Any]", variable has type "str")
Found 3 errors in 1 file (checked 1 source file)
It would require multiple cast
to do so where as all can be done with adding a !
at the end @uranusjr