pytypes icon indicating copy to clipboard operation
pytypes copied to clipboard

List and other invariant types should behave covariantly on runtime typechecking

Open Stewori opened this issue 4 years ago • 3 comments

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.

Stewori avatar Aug 29 '19 22:08 Stewori

Sounds cool, thank you!

jolaf avatar Aug 30 '19 09:08 jolaf

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.

jolaf avatar Aug 30 '19 09:08 jolaf

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.

Stewori avatar Aug 30 '19 13:08 Stewori