I ran all of the samples through pyright and uncovered a few small errors. I pushed a PR that fixes these, and Guido already approved and merged the change.
Thanks, Eric, this is super helpful!
Ah, thanks for noticing those, Eric. We had fixed some of these issues in our running Google Doc but not yet updated the PR. Will try to keep the PR more up-to-date.
+1; my bad.
A variadic class without parameters will bind any `Ts` to `Tuple[Any, ...]`.
So, in the above example, `x: Float32Array` will resolve to `Array[np.float32, *Tuple[Any, ...]]`.
This sounds reasonable to me too.
There's one other thing that still troubles me with the latest draft. ... In its strictest mode, pyright complains about both `Ndim` and `Shape` missing type arguments.
Ah, also a great point.
Having thought about this a bit more, I'm actually wondering whether we can
just drop that part of the PEP altogether. Dan Moldovan pointed out that
maybe the example wouldn't make sense anyway, since an `
Array[Ndim[Literal[1]]]` wouldn't be compatible with an `Array[Shape[Any]]`,
and that seems silly. So I think we should just get rid of `Ndim`, use a
sequence of `Any` instead, and not support crazy things like `ShapeType =
TypeVar('ShapeType', Ndim, Shape)` where we somehow have to support
forwarding the type parameter to the type parameter lists of the bounds
argghhh so complicated. That would leave us with just:
```
DataType = TypeVar('DataType')
Shape = TypeVarTuple('Shape')
class Array(Generic[DataType, *Shape]): ...
Float32Array = Array[np.float32, *Shape]
Array1D = Array[DataType, Any]
Array2D = Array[DataType, Any, Any]
# etc.
```
On Sun, 21 Feb 2021 at 10:42, Matthew Rahtz
Filipe - yes, that's right, for your use case `SpecialTuple = Tuple[*Ts]` should work - as long as you don't care about doing e.g. `SpecialTuple[int, ...]`, which currently deliberately doesn't work.
On Sun, 21 Feb 2021 at 05:42, Filipe Laíns
wrote: Hi,
Sorry for the noise, but I just wanted to be sure. I have a fairly simple use-case: I want to essentially alias Tuple (as in: SpecialTuple = Tuple) and be able to tell that type apart at runtime.
My users would be able to do:
def some_function() -> SpecialTuple[int, str]: return 0, 'hi!'
def some_other_function() -> SpecialTuple[int, int, int]: return 1, 2, 3
And I would be able to know at runtime, by looking at the type annotations, that the return value is special and I need to do some custom handling. As far as type checkers would be concerned, SpecialTuple is just a Tuple.
As far as I can tell, this should be possible with the current PEP by doing the following:
_Ts = TypeVarTuple('_Ts') SpecialTuple = Tuple[*_TS]
Correct?
Thank you, Filipe Laíns _______________________________________________ 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: mrahtz@google.com