Hi, 

This is a great idea, thanks for taking the initiative.

In django-stubs there is a type (QuerySet [1]) where we would like the second typevar parameter to default to the type of the first typevar parameter. Most users don’t care about providing the second type parameter, but it’s important for annotating the types correctly.

Could this PEP be modified such that there would be a way to express this? E.g. by setting the default to the other TypeVar?

Currently, as a workaround, we define the original class as a private _QuerySet class and then make an alias to the original:

QuerySet = _QuerySet[_T, _T]

This works, but causes other problems.
Originally, instead of the alias trick, a mypy plugin would handle defaulting the second typevar parameter.

[1] https://github.com/typeddjango/django-stubs/blob/eb6991b008e7afd2b191d19ca4c574661d99eb93/django-stubs/db/models/query.pyi#L34

On 5 Mar 2022, at 00.41, Mehdi2277 <med2277@gmail.com> wrote:

I would be happy to make tensors in most numeric libraries default to float. Most tensors I come across in numpy/tensorflow are float/int tensors and given int < float making default be float would capture most usages. There are occasionally times where other tensors like string tensor are used, but I think it's reasonable to expect those to specify type variable.

Similarly for tensorflow most layers take 1 tensor as input and return 1 tensor as output. But layer is free to take much more complex types so I still use Layer(Generic[InputT, OutputT]). In practice how I've handled this so far is not with aliases, but with subclasses. So I have,

class LayerGeneric(Generic[InputT, OutputT]):
 ...

class Layer(LayerGeneric[Tensor, Tensor]):
 ...

Since alias does not work for isinstance/issubclass checks, but a subclass does.

Those are the 2 biggest examples I've encountered where I would probably use this.
_______________________________________________
Typing-sig mailing list -- typing-sig@python.org
To unsubscribe send an email to typing-sig-leave@python.org
https://mail.python.org/mailman3/lists/typing-sig.python.org/
Member address: syastrov@gmail.com