Stewori / pytypes

Typing-toolbox for Python 3 _and_ 2.7 w.r.t. PEP 484.
Apache License 2.0
200 stars 20 forks source link

List and other invariant types should behave covariantly on runtime typechecking #76

Open Stewori opened 4 years ago

Stewori commented 4 years ago

For runtime typechecking deep_type is used to determine the PEP 484 type of a given object. Then is_subtype is used to detect if the type is acceptable. E.g. [2] is considered List[int] by deep_type although it could also be List[float] or List[Union[int, whatever]].

Solution idea: Widen the use of Empty which is currently used to handle [] and {}, etc. It would be renamed to Sampled or so and serve as a marker type for is_subtype to check its contained type, e.g. Sampled[List[int]] in covariant manner even if it is actually invariant.

jolaf commented 4 years ago

Sounds cool, thank you!

jolaf commented 4 years ago

I wonder if this Sampled[] and Empty[] things should be visible in TypeWarning printout when actual type mismatch occurs.

Probably they are better to be removed at printing to not confuse the user.

Stewori commented 4 years ago

Yes that's right. Or at least invisible unless some debug flag is set. I would cure this in type_str. However this won't fix before Python 3.7 compatibility is achieved because it requires some broader restructuring. I have rather limited capacity to tackle these bigger changes.