4 ms·
The best answer is, "with some difficulty". I've written a bit about the struggles here: https://thinc.ai/docs/usage-type-checking#model-types https://thinc.ai/
by syllogism 7y ago
The best answer is, "with some difficulty". I've written a bit about the struggles here: https://thinc.ai/docs/usage-type-checking#model-types https://thinc.ai/docs/usage-type-checking#model-types
There are basically two approaches, TypeVar and overload. You probably want TyepVar if you have exactly that sort of situations, but there's a lot about type vars I still don't understand, and the docs say almost nothing. The problem is the responsibility is kind of split in two places: the Python typing module doesn't really take any responsibility for usage, and mypy sees it as not mypy-specific, so neither describes it very well.
Our current solution is to avoid being fully variadic in the tensor type, and instead have a limited number of subclasses. This allows us to get by with overload, because we can enumerate out the set of argument-to-return mappings. This is especially important for the numpy API where you often have to overload on the types and values of other arguments, e.g. if you pass axis=(0, 1), you'll get a different rank of tensor back than if you had written axis=-1.
You can see the gory details of how I've done the numpy types here: https://github.com/explosion/thinc/blob/master/thinc/types.py#L260 https://github.com/explosion/thinc/blob/master/thinc/types.p...
- ipsum2 7y agoThanks for the detailed reply and the links!