Skip to content

Make Parameter operator dunder methods work in cython 0.x and cython 3

Miron van der Kolk requested to merge fix/cython0.x-operators into develop

The dunder operator methods in the Parameter class have been broken since the cython3 changes were merged in here.

They are only broken if you build and run finesse with cython 0.X, something that is not currently being tested by the pipeline. See also #616

See the explanation here, https://cython.readthedocs.io/en/latest/src/userguide/special_methods.html#arithmetic-methods

If backwards compatibility is needed, the normal operator method (__add__, etc.) can still be implemented to support both variants, applying a type check to the arguments. The reversed method (__radd__, etc.) can always be implemented with self as first argument and will be ignored by older Cython versions, whereas Cython 3.x and later will only call the normal method with the expected argument order, and otherwise call the reversed method instead.

This means that you can not rely on the argument order in the normal operator dunder methods, but only in the reversed ones.

Test locally

# create empty conda/virtual environment

# replace build and runtime requirements of cython to 0.X
sed -i -e 's/cython >= 0.29.36/cython == 0.29.37/' environment* setup.cfg pyproject.toml
# build and install finesse
pip install -e .[test, inplacebuild]
pytest tests # should succeed in this branch, not on develop

# now try building with cython3 and running with cython2
pip install 'cython<3'
pytest tests # this should succeed both on develop and this branch
Edited by Miron van der Kolk

Merge request reports