Skip to content

bug: generic function expression fails to parse #117

@MoAly98

Description

@MoAly98

Hi,

I am using pyhs3==0.3.0 and trying to write a Generic function which is very simple:

import pyhs3
from pyhs3.functions import GenericFunction

print(f"pyhs3 version: {pyhs3.__version__}")

# This fails with: unsupported operand type(s) for +: 'One' and 'FunctionClass'
config = {
    "type": "generic_function",
    "name": "beta_factor",
    "expression": "1 + beta"
}
func = GenericFunction(**config)

Running this, I get the following parsing failure:

/Users/moaly/Work/iris-hep/everwillow/.venv/lib/python3.12/site-packages/pyhs3/core.py:15: FutureWarning: `pytensor.graph.basic.applys_between` was moved to `pytensor.graph.traversal.applys_between`. Calling it from the old location will fail in a future release.
  from pytensor.graph.basic import applys_between, graph_inputs
/Users/moaly/Work/iris-hep/everwillow/.venv/lib/python3.12/site-packages/pyhs3/core.py:15: FutureWarning: `pytensor.graph.basic.graph_inputs` was moved to `pytensor.graph.traversal.graph_inputs`. Calling it from the old location will fail in a future release.
  from pytensor.graph.basic import applys_between, graph_inputs
pyhs3 version: 0.3.0
ValueError: Error from parse_expr with transformed code: 'Integer (1 )+beta '

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/moaly/Work/iris-hep/everwillow/.venv/lib/python3.12/site-packages/pyhs3/generic_parse.py", line 78, in parse_expression
    return sympy_parser.parse_expr(expr_str, transformations=transformations)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/moaly/Work/iris-hep/everwillow/.venv/lib/python3.12/site-packages/sympy/parsing/sympy_parser.py", line 1090, in parse_expr
    raise e from ValueError(f"Error from parse_expr with transformed code: {code!r}")
  File "/Users/moaly/Work/iris-hep/everwillow/.venv/lib/python3.12/site-packages/sympy/parsing/sympy_parser.py", line 1081, in parse_expr
    rv = eval_expr(code, local_dict, global_dict)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/moaly/Work/iris-hep/everwillow/.venv/lib/python3.12/site-packages/sympy/parsing/sympy_parser.py", line 905, in eval_expr
    expr = eval(
           ^^^^^
  File "<string>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'One' and 'FunctionClass'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/moaly/Work/iris-hep/everwillow/test_genericfunction_bug.py", line 14, in <module>
    func = GenericFunction(**config)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/moaly/Work/iris-hep/everwillow/.venv/lib/python3.12/site-packages/pydantic/main.py", line 250, in __init__
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/moaly/Work/iris-hep/everwillow/.venv/lib/python3.12/site-packages/pyhs3/functions.py", line 158, in setup_expression
    self._sympy_expr = parse_expression(self.expression_str)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/moaly/Work/iris-hep/everwillow/.venv/lib/python3.12/site-packages/pyhs3/generic_parse.py", line 81, in parse_expression
    raise ExpressionParseError(msg) from exc
pyhs3.exceptions.ExpressionParseError: Failed to parse expression '1 + beta': unsupported operand type(s) for +: 'One' and 'FunctionClass'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions