27 Nov
2020
27 Nov
'20
8:17 p.m.
Hello, trycast() works beautifully at runtime. There's just one problem:
I can't actually spell the types in the function signature for trycast(...)! You might think it would be:
T = TypeVar('T')
def trycast(type: Type[T], value: object) -> Optional[T]: ...
I've recently had to relax a type annotation in cattrs ( https://github.com/Tinche/cattrs/issues/105) due to a bug report for this exact problem, so I'm interested in a solution as well.
Slightly offtopic since we're working on similar problems: what's the benefit of using typed dictionaries instead of classes? To me structured data in Python automatically means a class.