reflex-dev/reflex · error · ValueError

cannot specify both default and default_factory

Error message

cannot specify both default and default_factory

What it means

`props_field()` cannot receive both `default=` and `default_factory=`; they are mutually exclusive because a field can only have one source of its initial value.

Source

Thrown at packages/reflex-base/src/reflex_base/components/props.py:161

def props_field(
    default: PROPS_FIELD_TYPE | _MISSING_TYPE = MISSING,
    default_factory: Callable[[], PROPS_FIELD_TYPE] | None = None,
) -> PROPS_FIELD_TYPE:
    """Create a field for a props class.

    Args:
        default: The default value for the field.
        default_factory: The default factory for the field.

    Returns:
        The field for the props class.

    Raises:
        ValueError: If both default and default_factory are specified.
    """
    if default is not MISSING and default_factory is not None:
        msg = "cannot specify both default and default_factory"
        raise ValueError(msg)
    return PropsField(  # pyright: ignore [reportReturnType]
        default=default,
        default_factory=default_factory,
        annotated_type=MISSING,
    )


@dataclass_transform(field_specifiers=(props_field,))
class PropsBaseMeta(FieldBasedMeta):
    """Meta class for PropsBase."""

    @classmethod
    def _process_annotated_fields(
        cls,
        namespace: dict[str, Any],
        annotations: dict[str, Any],
        inherited_fields: dict[str, PropsField],
    ) -> dict[str, PropsField]:

View on GitHub (pinned to 45b8ed5ab7)

Solutions

  1. Keep `default_factory` for mutable defaults and remove `default`
  2. For immutable defaults (int, str, tuple), keep `default` and remove `default_factory`

Example fix

# before
items: list[str] = props_field(default=[], default_factory=list)

# after
items: list[str] = props_field(default_factory=list)
Defensive patterns

Strategy: validation

Validate before calling

def safe_field(default=None, default_factory=None):
    assert not (default is not None and default_factory is not None), \
        "pick exactly one of default / default_factory"
    return props_field(default=default, default_factory=default_factory)

Prevention

When it happens

Trigger: `props_field(default=0, default_factory=list)` in a Props class definition.

Common situations: Copy-pasting a dataclass field and adding a factory while the default stayed, or editing a field from a scalar default to a mutable one.

Related errors


AI-assisted analysis of reflex-dev/reflex@45b8ed5ab7 (2026-08-28). Data as JSON: /api/errors/ae8976654a65294a. Report an issue: GitHub.