Skip to content

TYP: Type annotations overhaul, part 2 #291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions array_api_compat/common/_aliases.py
Original file line number Diff line number Diff line change
@@ -67,7 +67,7 @@ def eye(

def full(
shape: Union[int, Tuple[int, ...]],
fill_value: complex,
fill_value: bool | int | float | complex,
xp: Namespace,
*,
dtype: Optional[DType] = None,
@@ -80,7 +80,7 @@ def full(
def full_like(
x: Array,
/,
fill_value: complex,
fill_value: bool | int | float | complex,
*,
xp: Namespace,
dtype: Optional[DType] = None,
5 changes: 4 additions & 1 deletion array_api_compat/cupy/_aliases.py
Original file line number Diff line number Diff line change
@@ -68,7 +68,10 @@
# asarray also adds the copy keyword, which is not present in numpy 1.0.
def asarray(
obj: (
Array | bool | complex | NestedSequence[bool | complex] | SupportsBufferProtocol
Array
| bool | int | float | complex
| NestedSequence[bool | int | float | complex]
| SupportsBufferProtocol
),
/,
*,
5 changes: 4 additions & 1 deletion array_api_compat/dask/array/_aliases.py
Original file line number Diff line number Diff line change
@@ -136,7 +136,10 @@ def arange(
# asarray also adds the copy keyword, which is not present in numpy 1.0.
def asarray(
obj: (
Array | bool | complex | NestedSequence[bool | complex] | SupportsBufferProtocol
Array
| bool | int | float | complex
| NestedSequence[bool | int | float | complex]
| SupportsBufferProtocol
),
/,
*,
5 changes: 4 additions & 1 deletion array_api_compat/numpy/_aliases.py
Original file line number Diff line number Diff line change
@@ -77,7 +77,10 @@ def _supports_buffer_protocol(obj):
# rather than trying to combine everything into one function in common/
def asarray(
obj: (
Array | bool | complex | NestedSequence[bool | complex] | SupportsBufferProtocol
Array
| bool | int | float | complex
| NestedSequence[bool | int | float | complex]
| SupportsBufferProtocol
),
/,
*,
14 changes: 11 additions & 3 deletions array_api_compat/torch/_aliases.py
Original file line number Diff line number Diff line change
@@ -116,7 +116,9 @@ def _fix_promotion(x1, x2, only_scalar=True):
_py_scalars = (bool, int, float, complex)


def result_type(*arrays_and_dtypes: Array | DType | complex) -> DType:
def result_type(
*arrays_and_dtypes: Array | DType | bool | int | float | complex
) -> DType:
num = len(arrays_and_dtypes)

if num == 0:
@@ -550,10 +552,16 @@ def count_nonzero(
return result


def where(condition: Array, x1: Array, x2: Array, /) -> Array:
def where(
condition: Array,
x1: Array | bool | int | float | complex,
x2: Array | bool | int | float | complex,
/,
) -> Array:
x1, x2 = _fix_promotion(x1, x2)
return torch.where(condition, x1, x2)


# torch.reshape doesn't have the copy keyword
def reshape(x: Array,
/,
@@ -622,7 +630,7 @@ def linspace(start: Union[int, float],
# torch.full does not accept an int size
# https://github.com/pytorch/pytorch/issues/70906
def full(shape: Union[int, Tuple[int, ...]],
fill_value: complex,
fill_value: bool | int | float | complex,
*,
dtype: Optional[DType] = None,
device: Optional[Device] = None,
2 changes: 1 addition & 1 deletion array_api_compat/torch/linalg.py
Original file line number Diff line number Diff line change
@@ -85,7 +85,7 @@ def vector_norm(
axis: Optional[Union[int, Tuple[int, ...]]] = None,
keepdims: bool = False,
# float stands for inf | -inf, which are not valid for Literal
ord: Union[int, float, float] = 2,
ord: Union[int, float] = 2,
**kwargs,
) -> Array:
# torch.vector_norm incorrectly treats axis=() the same as axis=None