-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix upcasting with python builtin numbers and numpy 2 #8946
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
Changes from all commits
Commits
Show all changes
52 commits
Select commit
Hold shift + click to select a range
f3c2c93
Fix upcasting with python builtin numbers and numpy 2
djhoese 2c8a607
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] fcbd821
Remove old commented code
djhoese 3f7670b
Try updating result_type for array API
dcherian 6115dd8
xfail
dcherian e3493b0
Revert "Try updating result_type for array API"
dcherian e27f572
Merge branch 'main' into bugfix-scalar-arr-casting
dcherian 5b4384f
Determine python scalar dtype from array arguments
djhoese 33d48e4
Merge branch 'main' into bugfix-scalar-arr-casting
keewis 712c244
extend `get_array_namespace` to get the same namespace for multiple v…
keewis c486f8e
reflow error message
keewis 6a55378
allow weakly dtyped data as a separate argument to `result_type`
keewis c38c07f
allow passing a `dtype` argument to `duck_array_ops.asarray`
keewis 7b39cbe
rewrite `as_shared_dtype`
keewis 85d986f
fall back to the `astype` method if `xp.astype` doesn't exist
keewis a89cba8
determine the smallest possible dtype and call `xp.result_type` again
keewis 62900d6
map python types to dtype names
keewis 3e2855d
apply the custom rules after both dtype combinations
keewis 66fe0c5
add tests for passing scalars to `result_type`
keewis 476cb11
apply the custom casting rules to scalar types + explicit dtypes
keewis 4da4771
go back to the simple dtypes
keewis e193e90
comment on the fallback: if we don't have explicit dtypes, use the de…
keewis cda1ad0
Merge branch 'main' into bugfix-scalar-arr-casting
keewis 47becb8
ignore the typing
keewis 03974b1
don't support `datetime` scalars since that would be a new feature
keewis 92b35b3
make sure passing only scalars also works
keewis 109468a
move the fallback version of `result_type` into a separate function
keewis efb0f84
ignore the custom `inf` / `ninf` / `na` objects
keewis 7b0d478
more temporary fixes
keewis aa2d372
Merge branch 'main' into bugfix-scalar-arr-casting
keewis 463d0d6
remove debug print statements
keewis d33c742
change the representation for inf / ninf for boolean and nonnumeric a…
keewis 51f15fa
pass arrays instead of relying on implicit casting using `asarray`
keewis 2391cfb
refactor the implementation of `result_type`
keewis aaced1d
Merge branch 'main' into bugfix-scalar-arr-casting
keewis 4e86aa2
remove obsolete comment
keewis c28233c
proper placement of the `result_type` comment
keewis 0977707
back to using a list instead of a tuple
keewis 57b4eec
expect the platform-specific default dtype for integers
keewis 96e75c6
move `_future_array_api_result_type` to `npcompat`
keewis afa495e
rename `is_scalar_type` to `is_weak_scalar_type`
keewis 1dc9d46
move the dispatch to `_future_array_api_result_type` to `npcompat`
keewis b405916
support passing *only* weak dtypes to `_future_array_api_result_type`
keewis 66233a1
don't `xfail` the Array API `where` test
keewis 445195b
determine the array namespace if not passed and no `cupy` arrays present
keewis b2a6379
comment on what to do with `npcompat.result_type`
keewis 79d4a26
Merge branch 'main' into bugfix-scalar-arr-casting
keewis ab284f7
move the `result_type` compat code to `array_api_compat`
keewis cf11228
also update the comment
keewis 8dbacbc
mention the Array API issue on python scalars in a comment
keewis d0e08d8
Merge branch 'main' into bugfix-scalar-arr-casting
keewis 4c094c1
Merge branch 'main' into bugfix-scalar-arr-casting
keewis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.