Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 28, 2025

Division of a constant by a ScalarVariable (e.g., 1.0 / x or 5.0 // x) was raising TypeError due to missing reverse division operators.

Changes

Scalar variables:

  • Added __rtruediv__ method to _scalar_py_operators class, delegating to true_div(other, self)
  • Added __rfloordiv__ method to _scalar_py_operators class, delegating to int_div(other, self)
  • Added test_rtruediv to verify true division works correctly
  • Added test_rfloordiv to verify floor division works correctly

Tensor and XTensor variables:

  • Removed legacy Python 2 __div__ and __rdiv__ methods from _tensor_py_operators class in pytensor/tensor/variable.py
  • Removed legacy Python 2 __div__ and __rdiv__ methods from XTensorVariable class in pytensor/xtensor/type.py
  • Removed unused IntegerDivisionError import

All division operations now use Python 3's modern operators: __truediv__, __floordiv__, __rtruediv__, and __rfloordiv__.

Examples

from pytensor.scalar.basic import ScalarType, TrueDiv, IntDiv

# True division
x = ScalarType(dtype="float64")()
y = 1.0 / x  # Previously raised TypeError, now creates TrueDiv operation
assert isinstance(y.owner.op, TrueDiv)
assert isinstance(y.type, ScalarType)
assert y.eval({x: 2.0}) == 0.5

# Floor division
z = 5.0 // x  # Previously raised TypeError, now creates IntDiv operation
assert isinstance(z.owner.op, IntDiv)
assert isinstance(z.type, ScalarType)
assert z.eval({x: 2.0}) == 2.0

The implementation follows the existing pattern of other reverse arithmetic operators (__radd__, __rsub__, __rmul__, __rpow__).

Original prompt

The user wants to add __rtruediv__ and __rdiv__ to the _scalar_py_operators class in pytensor/scalar/basic.py.

A simple test should also be added to tests/scalar/test_basic.py to cover this new functionality.

This is to fix a bug where division of a constant by a ScalarVariable is unsupported.

The changes to pytensor/scalar/basic.py should be:

    def __rtruediv__(self, other):
        return true_div(other, self)

    def __rdiv__(self, other):
        return true_div(other, self)

These should be added to the _scalar_py_operators class.

And a test case should be added to tests/scalar/test_basic.py like:

def test_rtruediv():
    x = pytensor.scalar.Scalar(dtype="float64")
    y = 1.0 / x
    assert isinstance(y.owner.op, pytensor.scalar.TrueDiv)

This pull request was created as a result of the following prompt from Copilot chat.

The user wants to add __rtruediv__ and __rdiv__ to the _scalar_py_operators class in pytensor/scalar/basic.py.

A simple test should also be added to tests/scalar/test_basic.py to cover this new functionality.

This is to fix a bug where division of a constant by a ScalarVariable is unsupported.

The changes to pytensor/scalar/basic.py should be:

    def __rtruediv__(self, other):
        return true_div(other, self)

    def __rdiv__(self, other):
        return true_div(other, self)

These should be added to the _scalar_py_operators class.

And a test case should be added to tests/scalar/test_basic.py like:

def test_rtruediv():
    x = pytensor.scalar.Scalar(dtype="float64")
    y = 1.0 / x
    assert isinstance(y.owner.op, pytensor.scalar.TrueDiv)

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Add __rtruediv__ and __rdiv__ methods to _scalar_py_operators Add __rtruediv__ and __rdiv__ to _scalar_py_operators for reverse division support Oct 28, 2025
Copilot AI requested a review from ricardoV94 October 28, 2025 10:30
Copy link
Member

@ricardoV94 ricardoV94 left a comment

Choose a reason for hiding this comment

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

@copilot changes needed

Copilot AI changed the title Add __rtruediv__ and __rdiv__ to _scalar_py_operators for reverse division support Add __rtruediv__ and __rfloordiv__ to _scalar_py_operators for reverse division support Oct 28, 2025
Copilot AI requested a review from ricardoV94 October 28, 2025 11:25
@ricardoV94 ricardoV94 changed the title Add __rtruediv__ and __rfloordiv__ to _scalar_py_operators for reverse division support Add __rtruediv__ and __rfloordiv__ to Scalar variables Oct 28, 2025
@ricardoV94 ricardoV94 marked this pull request as ready for review October 28, 2025 13:34
@ricardoV94
Copy link
Member

@copilot remove legacy __div__ and __rdiv__ from tensor (and possibly) sparse variables methods.

Copy link
Contributor Author

Copilot AI commented Oct 28, 2025

@copilot remove legacy __div__ and __rdiv__ from tensor (and possibly) sparse variables methods.

Fixed in a4881ae. Removed legacy __div__ and __rdiv__ methods from:

  • _tensor_py_operators in pytensor/tensor/variable.py
  • XTensorVariable in pytensor/xtensor/type.py

Also removed unused IntegerDivisionError import. All division operations now use Python 3 operators (__truediv__, __floordiv__, __rtruediv__, __rfloordiv__).

Copilot AI changed the title Add __rtruediv__ and __rfloordiv__ to Scalar variables Add __rtruediv__ and __rfloordiv__ to Scalar variables and remove legacy __div__ operators Oct 28, 2025
Copilot AI requested a review from ricardoV94 October 28, 2025 13:53
@ricardoV94 ricardoV94 changed the title Add __rtruediv__ and __rfloordiv__ to Scalar variables and remove legacy __div__ operators Add __rtruediv__ and __rfloordiv__ to Scalar variables Oct 28, 2025
@codecov
Copy link

codecov bot commented Oct 28, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.68%. Comparing base (60ba7c7) to head (a4881ae).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1701      +/-   ##
==========================================
+ Coverage   81.67%   81.68%   +0.01%     
==========================================
  Files         244      244              
  Lines       53558    53549       -9     
  Branches     9433     9433              
==========================================
  Hits        43741    43741              
+ Misses       7337     7328       -9     
  Partials     2480     2480              
Files with missing lines Coverage Δ
pytensor/scalar/basic.py 81.05% <100.00%> (+0.03%) ⬆️
pytensor/tensor/variable.py 88.09% <ø> (+1.10%) ⬆️
pytensor/xtensor/type.py 70.19% <ø> (+0.19%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

y = 5.0 // x
assert isinstance(y.owner.op, IntDiv)
assert isinstance(y.type, ScalarType)
assert y.eval({x: 2.0}) == 2.0
Copy link
Member

Choose a reason for hiding this comment

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

We don't cast to int here?

Copy link
Member

Choose a reason for hiding this comment

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

This isn't testing the dtype, 2 == 2.0 is true in Python. I trust our old code was doing the correct thing already

@ricardoV94 ricardoV94 merged commit f83c05b into main Oct 28, 2025
61 of 73 checks passed
@ricardoV94 ricardoV94 deleted the copilot/add-rdiv-rtruediv-operators branch October 28, 2025 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: "constant / ScalarVariable" is unsupported + output_types_preference is missing in InRange

2 participants