Adding loosly defined types
Problems:
- You want define a type that is not possible to define in python at the moment of writing.
- You can define a type but you choose to defer its precise definition is a later time.
You have a type annotation that allow you to declare that the type you are defining is just an approximation of the type you would like to define.
Example: You want define a type to define the data structure that can
be json serialize, at this time this is not possible with actual typing system.
So you make the best approximation you do and decorate it with the LooseType annotation.
JsonType = LooseType(Union[List[..,], Dict[...]])
Now you can use this annotation with mypy or other related tool to keep track of the type that are not precisely defined, and that in the future you can improve thanks to update of the python typing system.
This is essentially what Any is: a marker that the type can't be represented with the current type system. Your example above could be written as: JsonType = Union[List[Any], Dict[str, Any]], giving you partial type checking an marking some values as "loose".
Thanks @srittau for your feedback. The type you define could be a precise type, witch is different from the corresponding lose one, and for that I think could make sense distinguish the two.
I still fail to see the difference between these two:
JsonType = LooseType(Union[List[..,], Dict[...]])
JsonType = Union[List[Any], Dict[Any, Any]]
How would a type checker check the first type differently from the second type?
How would a type checker check the first type differently from the second type?
@cesco-fran ?