Skip to content

Raise TypeError for boolean dtype subtraction to match NumPy behavior#645

Merged
inducer merged 3 commits intomainfrom
copilot/fix-array-subtraction-bool-dtype
Mar 10, 2026
Merged

Raise TypeError for boolean dtype subtraction to match NumPy behavior#645
inducer merged 3 commits intomainfrom
copilot/fix-array-subtraction-bool-dtype

Conversation

Copy link
Contributor

Copilot AI commented Mar 10, 2026

Pytato silently accepted boolean array subtraction, whereas NumPy raises a TypeError. This adds the same guard to match NumPy semantics.

import numpy as np
import pytato as pt

a_np = np.array([False, True])
a_np - a_np  # TypeError: numpy boolean subtract, the `-` operator, is not supported...

a_pt = pt.make_data_wrapper(a_np)
a_pt - a_pt  # Previously returned an IndexLambda; now raises TypeError

Changes

  • pytato/array.py: __sub__ and __rsub__ now check if _np_result_dtype would yield bool_ and raise TypeError with NumPy's canonical message. Added _BOOL_DTYPE module-level constant to avoid repeated dtype object construction.
  • test/test_pytato.py: Added test_boolean_subtract_raises covering array - array, array - scalar, and scalar - array boolean cases, plus positive cases confirming non-boolean subtraction and bool - int (result is int) still work.
Original prompt

This section details on the original issue you should resolve

<issue_title>Subtraction of array with boolean dtypes should not be allowed.</issue_title>
<issue_description>```python

import numpy as np
import pytato as pt
a_np = np.array([False, True])
a_np - a_np
Traceback (most recent call last):
File "", line 1, in
a_np - a_np
~~~~~^~~~~~
TypeError: numpy boolean subtract, the - operator, is not supported, use the bitwise_xor, the ^ operator, or the logical_xor function instead.
a_pt = pt.make_data_wrapper(a_np)
a_pt - a_pt
IndexLambda(shape=(2), dtype='bool', expr=Sum((Subscript(Variable('_in0'), (Variable('_0'),)), Product((-1, Subscript(Variable('_in1'), (Variable('_0'),)))))), bindings={'_in0': DataWrapper(axes=(Axis(tags=frozenset())), tags={}, non_equality_tags={}, data=array([False, True]), shape=(2)), '_in1': DataWrapper(axes=(Axis(tags=frozenset())), tags={}, non_equality_tags={}, data=array([False, True]), shape=(2))})


We should follow Numpy and raise TypeError as well.</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>


💡 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.

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
@inducer inducer marked this pull request as ready for review March 10, 2026 14:40
Copilot AI review requested due to automatic review settings March 10, 2026 14:40
@inducer inducer enabled auto-merge (rebase) March 10, 2026 14:40
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

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_DTYPE module-level constant in array.py and guard __sub__/__rsub__ with a dtype check.
  • Add test_boolean_subtract_raises covering 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.

@inducer inducer force-pushed the copilot/fix-array-subtraction-bool-dtype branch from 581ea0f to fff2316 Compare March 10, 2026 15:23
@inducer inducer merged commit a6cfcd8 into main Mar 10, 2026
10 checks passed
@inducer inducer deleted the copilot/fix-array-subtraction-bool-dtype branch March 10, 2026 15:43
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.

Subtraction of array with boolean dtypes should not be allowed.

3 participants