BUG: raise a clear error in common_type for non-array input#31592
Open
Lawson-Darrow wants to merge 1 commit into
Open
BUG: raise a clear error in common_type for non-array input#31592Lawson-Darrow wants to merge 1 commit into
Lawson-Darrow wants to merge 1 commit into
Conversation
common_type does a.dtype.type on each input, so passing a dtype or a
scalar type like np.common_type(np.dtype("f4")) raised a confusing
AttributeError. Catch that case and raise a TypeError that tells the
user to reach for np.result_type or np.promote_types instead. Valid
array inputs are unchanged.
Closes numpygh-30890
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #30890
Summary
numpy.common_typedoesa.dtype.typeon each input so passing a dtype or a scalar type, for examplenp.common_type(np.dtype("f4")), raised a confusingAttributeError('numpy.dtypes.Float32DType' object has no attribute 'dtype').This catches that case and raises a
TypeErrorthat points tonp.result_typeandnp.promote_typeswhich are the tools meant for combining dtypes and scalar types. A dtype instance is shown asdtype('float32')and a scalar type as'float32'. Valid array inputs are unchanged. This follows the direction @ngoldbaum and @seberg suggested on the issue.Before:
After:
Test plan
Added tests in
numpy/lib/tests/test_type_check.pyfor dtype and scalar-type inputs: a clearTypeErrorwith thenp.result_type/np.promote_typesguidance, the dtype instance shown asdtype('float32'), and the scalar type as'float32'. ExistingTestCommonType.test_basicstill covers real arrays. Release note added indoc/release/upcoming_changes/30890.improvement.rst.This change was prepared with AI assistance (Claude Code, OpenAI Codex). I reviewed and verified the change and the tests locally and take responsibility for it.