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 __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))
|
shape = (Height(480), Width(640))
|
||||||
x: Array[Height, Width] = Array(shape)
|
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,)
|
a = (0,)
|
||||||
b = ('0',)
|
b = ('0',)
|
||||||
|
@ -318,14 +318,15 @@ prefixed and/or suffixed:
|
||||||
|
|
||||||
Shape = TypeVarTuple('Shape')
|
Shape = TypeVarTuple('Shape')
|
||||||
Batch = NewType('Batch', int)
|
Batch = NewType('Batch', int)
|
||||||
|
Channels = NewType('Channels', int)
|
||||||
|
|
||||||
def add_batch_axis(x: Array[*Shape]) -> Array[Batch, *Shape]: ...
|
def add_batch_axis(x: Array[*Shape]) -> Array[Batch, *Shape]: ...
|
||||||
def del_batch_axis(x: Array[Batch, *Shape]) -> Array[*Shape]: ...
|
def del_batch_axis(x: Array[Batch, *Shape]) -> Array[*Shape]: ...
|
||||||
def add_batch_channels(x: Array[*Shape]) -> Array[Batch, *Shape, Channels]: ...
|
def add_batch_channels(x: Array[*Shape]) -> Array[Batch, *Shape, Channels]: ...
|
||||||
|
|
||||||
a: Array[Height, Width]
|
a: Array[Height, Width]
|
||||||
b = add_batch(a) # Inferred type is Array[Batch, Height, Width]
|
b = add_batch_axis(a) # Inferred type is Array[Batch, Height, Width]
|
||||||
c = del_batch(b) # Array[Height, Width]
|
c = add_batch_axis(b) # Array[Height, Width]
|
||||||
d = add_batch_channels(a) # Array[Batch, Height, Width, Channels]
|
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, ...]):
|
def bar(x: Tuple[int, ...]):
|
||||||
foo(*x) # NOT valid
|
foo(*x) # NOT valid
|
||||||
|
@ -417,6 +418,7 @@ Type variable tuples can also be used in the arguments section of a
|
||||||
|
|
||||||
class Process:
|
class Process:
|
||||||
def __init__(
|
def __init__(
|
||||||
|
self,
|
||||||
target: Callable[[*Ts], Any],
|
target: Callable[[*Ts], Any],
|
||||||
args: Tuple[*Ts]
|
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)
|
return random.choice(args)
|
||||||
|
|
||||||
f(1, 'foo') # Inferred type is Union[int, str]
|
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')
|
Axis2 = TypeVar('Axis2')
|
||||||
Axis3 = TypeVar('Axis3')
|
Axis3 = TypeVar('Axis3')
|
||||||
|
|
||||||
class Array(Generic[Shape]): ...
|
class Array(Generic[*Shape]):
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
def transpose(
|
def transpose(
|
||||||
|
@ -525,7 +527,7 @@ For situations where we require access to each individual type, overloads can be
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
def transpose(
|
def transpose(
|
||||||
self: Array[Axis1, Axis2, Axis3)
|
self: Array[Axis1, Axis2, Axis3]
|
||||||
) -> Array[Axis3, Axis2, Axis1]: ...
|
) -> Array[Axis3, Axis2, Axis1]: ...
|
||||||
|
|
||||||
(For array shape operations in particular, having to specify
|
(For array shape operations in particular, having to specify
|
||||||
|
@ -566,7 +568,7 @@ in a complete array type.
|
||||||
class Shape(Generic[*Axes]): ...
|
class Shape(Generic[*Axes]): ...
|
||||||
|
|
||||||
DataType = TypeVar('DataType')
|
DataType = TypeVar('DataType')
|
||||||
ShapeType = TypeVar('ShapeType', NDim, Shape)
|
ShapeType = TypeVar('ShapeType', Ndim, Shape)
|
||||||
|
|
||||||
# The most verbose type
|
# The most verbose type
|
||||||
# E.g. Array[np.float32, Ndim[Literal[3]]
|
# E.g. Array[np.float32, Ndim[Literal[3]]
|
||||||
|
|
Loading…
Reference in New Issue