-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix upcasting with python builtin numbers and numpy 2 (#8946)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Deepak Cherian <[email protected]> Co-authored-by: Justus Magin <[email protected]> Co-authored-by: Justus Magin <[email protected]>
- Loading branch information
1 parent
0fca670
commit d3e7460
Showing
7 changed files
with
134 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import numpy as np | ||
|
||
|
||
def is_weak_scalar_type(t): | ||
return isinstance(t, (bool, int, float, complex, str, bytes)) | ||
|
||
|
||
def _future_array_api_result_type(*arrays_and_dtypes, xp): | ||
# fallback implementation for `xp.result_type` with python scalars. Can be removed once a | ||
# version of the Array API that includes https://github.com/data-apis/array-api/issues/805 | ||
# can be required | ||
strongly_dtyped = [t for t in arrays_and_dtypes if not is_weak_scalar_type(t)] | ||
weakly_dtyped = [t for t in arrays_and_dtypes if is_weak_scalar_type(t)] | ||
|
||
if not strongly_dtyped: | ||
strongly_dtyped = [ | ||
xp.asarray(x) if not isinstance(x, type) else x for x in weakly_dtyped | ||
] | ||
weakly_dtyped = [] | ||
|
||
dtype = xp.result_type(*strongly_dtyped) | ||
if not weakly_dtyped: | ||
return dtype | ||
|
||
possible_dtypes = { | ||
complex: "complex64", | ||
float: "float32", | ||
int: "int8", | ||
bool: "bool", | ||
str: "str", | ||
bytes: "bytes", | ||
} | ||
dtypes = [possible_dtypes.get(type(x), "object") for x in weakly_dtyped] | ||
|
||
return xp.result_type(dtype, *dtypes) | ||
|
||
|
||
def result_type(*arrays_and_dtypes, xp) -> np.dtype: | ||
if xp is np or any( | ||
isinstance(getattr(t, "dtype", t), np.dtype) for t in arrays_and_dtypes | ||
): | ||
return xp.result_type(*arrays_and_dtypes) | ||
else: | ||
return _future_array_api_result_type(*arrays_and_dtypes, xp=xp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters