Fixed several omissions and errors in sample code in PEP 646. (#1826)
Co-authored-by: Eric Traut <erictr@microsoft.com>
This commit is contained in:
parent
4304da21ec
commit
35a0204848
20
pep-0646.rst
20
pep-0646.rst
|
@ -143,7 +143,7 @@ signatures and variable annotations:
|
|||
|
||||
def __abs__(self) -> Array[*Shape]: ...
|
||||
|
||||
def __add__(self, other: Array[*Shape]) -> Array[*Shape]) ...
|
||||
def __add__(self, other: Array[*Shape]) -> Array[*Shape]: ...
|
||||
|
||||
shape = (Height(480), Width(640))
|
||||
x: Array[Height, Width] = Array(shape)
|
||||
|
@ -272,7 +272,7 @@ a ``Tuple`` of a ``Union`` of types:
|
|||
|
||||
::
|
||||
|
||||
def foo(arg1: Tuple[*Ts], arg2: Tuple[*Ts])
|
||||
def foo(arg1: Tuple[*Ts], arg2: Tuple[*Ts]): ...
|
||||
|
||||
a = (0,)
|
||||
b = ('0',)
|
||||
|
@ -318,14 +318,15 @@ prefixed and/or suffixed:
|
|||
|
||||
Shape = TypeVarTuple('Shape')
|
||||
Batch = NewType('Batch', int)
|
||||
Channels = NewType('Channels', int)
|
||||
|
||||
def add_batch_axis(x: Array[*Shape]) -> Array[Batch, *Shape]: ...
|
||||
def del_batch_axis(x: Array[Batch, *Shape]) -> Array[*Shape]: ...
|
||||
def add_batch_channels(x: Array[*Shape]) -> Array[Batch, *Shape, Channels]: ...
|
||||
|
||||
a: Array[Height, Width]
|
||||
b = add_batch(a) # Inferred type is Array[Batch, Height, Width]
|
||||
c = del_batch(b) # Array[Height, Width]
|
||||
b = add_batch_axis(a) # Inferred type is Array[Batch, Height, Width]
|
||||
c = add_batch_axis(b) # Array[Height, Width]
|
||||
d = add_batch_channels(a) # Array[Batch, Height, Width, Channels]
|
||||
|
||||
|
||||
|
@ -393,7 +394,7 @@ course, valid at runtime):
|
|||
|
||||
::
|
||||
|
||||
def foo(*args: Tuple[*Ts]): ...
|
||||
def foo(*args: *Ts): ...
|
||||
|
||||
def bar(x: Tuple[int, ...]):
|
||||
foo(*x) # NOT valid
|
||||
|
@ -417,6 +418,7 @@ Type variable tuples can also be used in the arguments section of a
|
|||
|
||||
class Process:
|
||||
def __init__(
|
||||
self,
|
||||
target: Callable[[*Ts], Any],
|
||||
args: Tuple[*Ts]
|
||||
): ...
|
||||
|
@ -440,7 +442,7 @@ Type variable tuples can also be used with ``Union``:
|
|||
|
||||
::
|
||||
|
||||
def f(*args: Tuple[*Ts]) -> Union[*Ts]:
|
||||
def f(*args: *Ts) -> Union[*Ts]:
|
||||
return random.choice(args)
|
||||
|
||||
f(1, 'foo') # Inferred type is Union[int, str]
|
||||
|
@ -516,7 +518,7 @@ For situations where we require access to each individual type, overloads can be
|
|||
Axis2 = TypeVar('Axis2')
|
||||
Axis3 = TypeVar('Axis3')
|
||||
|
||||
class Array(Generic[Shape]): ...
|
||||
class Array(Generic[*Shape]):
|
||||
|
||||
@overload
|
||||
def transpose(
|
||||
|
@ -525,7 +527,7 @@ For situations where we require access to each individual type, overloads can be
|
|||
|
||||
@overload
|
||||
def transpose(
|
||||
self: Array[Axis1, Axis2, Axis3)
|
||||
self: Array[Axis1, Axis2, Axis3]
|
||||
) -> Array[Axis3, Axis2, Axis1]: ...
|
||||
|
||||
(For array shape operations in particular, having to specify
|
||||
|
@ -566,7 +568,7 @@ in a complete array type.
|
|||
class Shape(Generic[*Axes]): ...
|
||||
|
||||
DataType = TypeVar('DataType')
|
||||
ShapeType = TypeVar('ShapeType', NDim, Shape)
|
||||
ShapeType = TypeVar('ShapeType', Ndim, Shape)
|
||||
|
||||
# The most verbose type
|
||||
# E.g. Array[np.float32, Ndim[Literal[3]]
|
||||
|
|
Loading…
Reference in New Issue