Skip to content

Commit d2632ae

Browse files
committed
Change to int | None
1 parent 37e430e commit d2632ae

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

test/test_form.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ def test_form_arity(functional, mass, stiffness, convection, load) -> None:
116116
assert convection.arity == 2
117117
assert load.arity == 1
118118

119+
assert (functional + load).arity is None
120+
assert (functional + mass).arity is None
121+
assert (functional + stiffness).arity is None
122+
assert (load + mass).arity is None
123+
assert (load + stiffness).arity is None
124+
119125

120126
def test_form_coefficients(element, domain):
121127
space = FunctionSpace(domain, element)

ufl/form.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,28 @@ def empty(self):
341341
return len(self.integrals()) == 0
342342

343343
@property
344-
def arity(self):
345-
"""Arity of the form."""
346-
return len(self.arguments())
344+
def arity(self) -> int | None:
345+
"""Arity of the form.
346+
347+
Returns:
348+
Number of arguments if all integrals share the argument count, otherwise None.
349+
"""
350+
if not self._integrals:
351+
return 0
352+
353+
from ufl.algorithms.analysis import extract_terminals_with_domain
354+
355+
arity = None
356+
for integral in self._integrals:
357+
args, _, _ = extract_terminals_with_domain(integral.integrand())
358+
_arity = len(set(args))
359+
360+
if arity is None:
361+
arity = _arity
362+
elif arity != _arity:
363+
return None
364+
365+
return arity
347366

348367
def ufl_domains(self):
349368
"""Return the geometric integration domains occuring in the form.

0 commit comments

Comments
 (0)