Skip to content

Let scalar * tensor fall back to the tensor's reflected operator - #2435

Open
anishmehta24 wants to merge 1 commit into
pymc-devs:mainfrom
anishmehta24:fix/scalar-binary-ops-return-notimplemented
Open

anishmehta24 wants to merge 1 commit into
pymc-devs:mainfrom
anishmehta24:fix/scalar-binary-ops-return-notimplemented

Conversation

@anishmehta24

Copy link
Copy Markdown
Contributor

Description

scalar * tensor (and +, -, /, **, comparisons, bitwise ops) raised TypeError: Cannot convert b to a scalar type, because the scalar operator methods called the op directly. Python only tries the right operand's reflected method (TensorVariable.__rmul__) when the left one returns NotImplemented.

The binary operator methods on _scalar_py_operators now go through a small helper that returns NotImplemented when either operand can't be converted with as_scalar, the same idea as the try/except in TensorVariable.__add__. Errors raised by the op itself still propagate, so test_mod_complex_fail keeps getting ComplexError.

Added test_scalar_tensor_binary_op, parametrized over the operators, next to test_rtruediv; it fails on main. tests/scalar and tests/tensor/test_elemwise.py pass.

Related Issue

Checklist

Type of change

  • New feature / enhancement
  • Bug fix
  • Documentation
  • Maintenance
  • Other (please specify):

This branch has not been deployed

No deployments
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.

Scalar * Tensor operations fails

1 participant