Skip to content

BUG: restrict weak scalar handling in resolve_dtypes to input operands - #31746

Merged
MaanasArora merged 4 commits into
numpy:mainfrom
Diamagnetic:fix-ufunc-resolve-dtypes
Jul 6, 2026
Merged

MaanasArora merged 4 commits into
numpy:mainfrom
Diamagnetic:fix-ufunc-resolve-dtypes

Conversation

@Diamagnetic

Copy link
Copy Markdown
Contributor

PR summary

This PR fixes a SegFault in ufunc.resolve_dtypes for calls like: np.sin.resolve_dtypes((int, int))

The bug:

  • int/float/complex were treated as weak scalar markers in all operand positions.
  • This means for output operands as well. This is invalid for those. And, it results in a segfault.

Fix:

  • Restrict weak-scalar handling to input operands only.
  • For output operands, builtin int/float/complex are interpreted as concrete NumPy dtypes.

Result:

  • np.sin.resolve_dtypes((int, int)) now raises a normal casting error (UFuncOutputCastingError) instead of crashing.
  • Valid weak input behavior remains unchanged (e.g. np.sin.resolve_dtypes((int, None))).

Tests:

  • Updated unary weak-scalar resolve_dtypes test coverage.

First time committer introduction

Hi all, I am new to the NumPy community. I use NumPy mainly in my data science, and graph theory projects. I wanted to make contributions to open source, and my procrastination finally ended recently. I was thinking of contributing to something I use regularly, and that's how I chose NumPy (I use it regularly).

AI Disclosure

  • I used ChatGPT Go and OpenAI Codex (gpt-5.3-codex medium effort) to help with debugging and fixing the issue.
  • I manually created the development environment, built numpy from source, reviewed, edited, and validated all code and tests.

Closes #31734

@Diamagnetic

Copy link
Copy Markdown
Contributor Author

Just following up on this PR in case it got buried. I would really appreciate a review when possible. Thanks in advance!

@MaanasArora

Copy link
Copy Markdown
Contributor

Thanks, this seems to be in the right place but I don't think the fix is right; a UFuncOutputCastingError is clearly the wrong error. Python types and concrete dtypes can't be interchanged like this.

Instead if the descriptor is an output descriptor, we should just skip the weak-scalar branches and then error (a nicer else-if error branch if i < ufunc->nin may be needed).

Comment thread numpy/_core/src/umath/ufunc_object.c Resolved
Comment thread numpy/_core/tests/test_ufunc.py Resolved
Comment thread numpy/_core/src/umath/ufunc_object.c Resolved
@Diamagnetic
Diamagnetic force-pushed the fix-ufunc-resolve-dtypes branch from 7400554 to 31c72ac Compare July 6, 2026 11:22
- Moved the `i < ufunc->nin` check into the condition
- Removed the nested if-else branches
- Raise a dedicated error for weak scalar output descriptors
- Improve `resolve_dtypes` error for invalid output descriptors
@Diamagnetic
Diamagnetic force-pushed the fix-ufunc-resolve-dtypes branch from 31c72ac to eafb576 Compare July 6, 2026 12:54
Comment thread numpy/_core/tests/test_ufunc.py Resolved
- Exact matching for the error instead of regex

@MaanasArora MaanasArora left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome thank you, looks good to me! Will put this in after CI passes.

@MaanasArora
MaanasArora merged commit 12f2e15 into numpy:main Jul 6, 2026
86 of 87 checks passed
MaanasArora pushed a commit to MaanasArora/numpy that referenced this pull request Jul 7, 2026
ngoldbaum pushed a commit to ngoldbaum/numpy that referenced this pull request Sep 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: ufunc.resolve_dtypes segfaults

2 participants