top | item 26827108

(no title)

nuclear_eclipse | 4 years ago

The big benefit of type hints things like Pydantic (and some things I've written myself) is that it can provide useful runtime information. If you know that property `obj.foo` is hinted with `List[Foo]`, then that allows you to do something expecting to have a list of `Foo` objects. This is really extraordinary for deserializing untyped data (like a json blob) into appropriate objects at runtime. You can take something like `{"foo": [...]}` and attempt to deserialize each element of the `foo` list into `Foo` objects, rather than just guessing at what the list contains. If you stop being able to understand type hints at runtime, then the only thing they're good for is documentation and static analysis.

discuss

order

dec0dedab0de|4 years ago

but what if another developer wants to put a different object in that list, but it has the same methods and properties that I am expecting in Foo?

mixmastamyk|4 years ago

It supports duck typing with Protocol, I believe.