Raise TypeError for boolean dtype subtraction to match NumPy behavior#645
Merged
Raise TypeError for boolean dtype subtraction to match NumPy behavior#645
Conversation
Copilot
AI
changed the title
[WIP] Fix subtraction of array with boolean dtypes
Raise TypeError for boolean dtype subtraction to match NumPy behavior
Mar 10, 2026
There was a problem hiding this comment.
Pull request overview
This PR ensures pytato raises TypeError for boolean dtype subtraction, matching NumPy's behavior. When both operands resolve to bool_ via np.result_type, the - operator now raises a TypeError with the canonical NumPy error message, instead of silently producing a boolean IndexLambda.
Changes:
- Add
_BOOL_DTYPEmodule-level constant inarray.pyand guard__sub__/__rsub__with a dtype check. - Add
test_boolean_subtract_raisescovering array–array, array–scalar, scalar–array, and positive cases.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
pytato/array.py |
Adds _BOOL_DTYPE constant and dtype guard in __sub__/__rsub__ to raise TypeError for boolean subtraction. |
test/test_pytato.py |
Adds test_boolean_subtract_raises with three error cases and two positive cases verifying the new behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Co-authored-by: inducer <352067+inducer@users.noreply.github.com>
581ea0f to
fff2316
Compare
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.
Pytato silently accepted boolean array subtraction, whereas NumPy raises a
TypeError. This adds the same guard to match NumPy semantics.Changes
pytato/array.py:__sub__and__rsub__now check if_np_result_dtypewould yieldbool_and raiseTypeErrorwith NumPy's canonical message. Added_BOOL_DTYPEmodule-level constant to avoid repeated dtype object construction.test/test_pytato.py: Addedtest_boolean_subtract_raisescoveringarray - array,array - scalar, andscalar - arrayboolean cases, plus positive cases confirming non-boolean subtraction andbool - int(result is int) still work.Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.