8000 BUG: Type casting with numpy scalars violate promotion rules · Issue #20323 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Type casting with numpy scalars violate promotion rules #20323

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

Closed
ChristianReinbold opened this issue Nov 8, 2021 · 2 comments
Closed
Labels

Comments

@ChristianReinbold
Copy link
ChristianReinbold commented Nov 8, 2021

Describe the issue:

Implicit type casting for binary operators seems to ignore types of scalar values, see code example below where the left operand is passed a 0D scalar and 1D array respectively.

This behaviour provokes unexpected overflow errors in corner cases of empty shape tuples that can be easily overseen.

Reproduce the code example:

import numpy as np
type_w_scalar = (np.ones((), dtype=np.int32) + np.ones(5, dtype=np.int16)).dtype
type_wo_scalar = (np.ones(1, dtype=np.int32) + np.ones(5, dtype=np.int16)).dtype
promoted_type = np.promote_types(np.int32, np.int16)
# Note that promoted_type == type_wo_scalar != type_w_scalar
# promoted_type == np.int32
# type_wo_scalar == np.int32
# type_w_scalar == np.int16
print(type_w_scalar, type_wo_scalar, promoted_type)

Error message:

No response

NumPy/Python version information:

1.20.3 3.8.8 (default, Feb 24 2021, 21:46:12)
[GCC 7.3.0]

@seberg
Copy link
Member
seberg commented Nov 8, 2021

Yeah, it is a known issue, but hard to fix (i.e. may even need a NumPy 2.0 release due to incompatibility)... Which is why this has been lying around for years without anything serious being done. Basically a duplicate of gh-10322. (and probably others)

@ChristianReinbold
Copy link
Author

Thanks for the quick reply and clarification. I guess this issue can be closed then!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants
0