Skip to content

Commit a7136d7

Browse files
committed
Raise error on wrong number of arguments to Function
1 parent d1a50aa commit a7136d7

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

symengine/lib/symengine_wrapper.in.pyx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2412,8 +2412,15 @@ class Pow(Expr):
24122412
class Function(Expr):
24132413

24142414
def __new__(cls, *args, **kwargs):
2415-
if cls == Function and len(args) == 1:
2416-
return UndefFunction(args[0])
2415+
if cls == Function:
2416+
nargs = len(args)
2417+
if nargs == 0:
2418+
raise TypeError("Required at least one argument to Function")
2419+
elif nargs == 1:
2420+
return UndefFunction(args[0])
2421+
elif nargs > 1:
2422+
raise TypeError(f"Unexpected extra arguments {args[1:]}.")
2423+
24172424
return super(Function, cls).__new__(cls)
24182425

24192426
@property

symengine/tests/test_functions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ def test_derivative():
103103
assert i == fxy.diff(y, 1, x)
104104

105105

106+
def test_function():
107+
x = Symbol("x")
108+
assert Function("f")(x) == function_symbol("f", x)
109+
110+
raises(TypeError, lambda: Function("f", "x"))
111+
raises(TypeError, lambda: Function("f", x))
112+
raises(TypeError, lambda: Function())
113+
114+
106115
def test_abs():
107116
x = Symbol("x")
108117
e = abs(x)

0 commit comments

Comments
 (0)