Skip to content

Support empty containers in dx() to align with standard Python behavior #4452

@anilbeycorintis

Description

@anilbeycorintis

Describe the current issue

Currently, fd.dx() behaves inconsistently with common Python conventions when given empty containers.

fd.dx()         ✅  # Equivalent to Measure('cell', subdomain_id='everywhere')
fd.dx(None)     ✅  # Same as above
fd.dx([])       ❌  # Raises an error
fd.dx(())       ❓  # Accepted, but becomes Measure('cell', subdomain_id=()), not 'everywhere'
fd.dx(set())    ❌  # Raises an error

This inconsistency affects not only dx but likely ds and other measure-related APIs as well.

If these inconsistencies remain, how can I know which standard Python constructs are safe to use with Firedrake and which are not?

Shouldn't a Python API aim to be consistent with standard Python constructs to ensure intuitive and reliable usage?

Describe the solution you'd like

The API should treat empty containers consistent with both its own handling of None and Python's standard behavior:

>>> sum([])
0
>>> sum(())
0
>>> sum(set())
0
>>> sum(list())
0
>>> sum(tuple())
0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions