Ask HN: What do you think about type coverage in Python?
The idea behind it is to measure how well a python function is tested by recording all inputs/outputs during a complete test run and comparing the values with the annotated types.
e.g. a function `foo(a: Optional[float])` which is only tested with `foo(5)` gets a low coverage because `foo(None)` is not being tested. Or it could be hinted that a more feasible type annotation would be `foo(a: int)`.
As a use case I was thinking of testing APIs, to make sure you covered all use cases of your API which you advertise by the annotated types.
An extension of this concept could be that you check how extensive you tested a type. Like did you test `foo(a: int)` with negative, positive and zero values? If not it could be a hint that your test coverage is too low or you have a wrong type, maybe a enum would be suited better.
I am curious to hear about your thoughts about this concept.
4 comments
[ 3.0 ms ] story [ 22.9 ms ] thread[0] https://mypy.readthedocs.io/en/stable/
What I proposed will complain in this case, that you never tested `foo` with `None` or `float` as input parameters and that it never has seen `foo` returning `None` or `float`.
So even with 100% code coverage and mypy not complaining 'type coverage' could give you some hints that a) your tests are not extensive enough or b) your annotated types are too broad
What you propose will certainly solve the issue when dealing with unannotated code, though.
Regarding literals or annotated variables: mypy will detect mismatching types in both cases.