pallets/flask · error · ValueError

'view_func' name may not contain a dot '.' character.

Error message

'view_func' name may not contain a dot '.' character.

What it means

Flask blueprints derive endpoint names from the view function's __name__ when no explicit endpoint is given, and endpoints use dots as namespace separators (blueprint_name.endpoint). If the function name itself contains a dot, the generated endpoint would collide with the blueprint namespacing scheme, so Blueprint.add_url_rule raises this ValueError in src/flask/sansio/blueprints.py.

Source

Thrown at src/flask/sansio/blueprints.py:431

    def add_url_rule(
        self,
        rule: str,
        endpoint: str | None = None,
        view_func: ft.RouteCallable | None = None,
        provide_automatic_options: bool | None = None,
        **options: t.Any,
    ) -> None:
        """Register a URL rule with the blueprint. See :meth:`.Flask.add_url_rule` for
        full documentation.

        The URL rule is prefixed with the blueprint's URL prefix. The endpoint name,
        used with :func:`url_for`, is prefixed with the blueprint's name.
        """
        if endpoint and "." in endpoint:
            raise ValueError("'endpoint' may not contain a dot '.' character.")

        if view_func and hasattr(view_func, "__name__") and "." in view_func.__name__:
            raise ValueError("'view_func' name may not contain a dot '.' character.")

        self.record(
            lambda s: s.add_url_rule(
                rule,
                endpoint,
                view_func,
                provide_automatic_options=provide_automatic_options,
                **options,
            )
        )

    @t.overload
    def app_template_filter(self, name: T_template_filter) -> T_template_filter: ...
    @t.overload
    def app_template_filter(
        self, name: str | None = None
    ) -> t.Callable[[T_template_filter], T_template_filter]: ...
    @setupmethod

View on GitHub ↗ (pinned to 6a2f545bfd)

Solutions

  1. Rename the view function or the endpoint argument to remove the dot (use underscores, e.g. 'api_users').
  2. If you want dotted namespacing, register a nested Blueprint instead — Flask joins names with dots automatically.
  3. When using MethodView.as_view() or dynamic wrappers, pass a dot-free name and set an explicit endpoint= if needed.

Example fix

# before
view = make_view(); view.__name__ = 'api.users'
bp.add_url_rule('/users', view_func=view)
# after
view = make_view(); view.__name__ = 'api_users'
bp.add_url_rule('/users', view_func=view)  # url_for('bp.api_users')
Defensive patterns

Strategy: validation

Validate before calling

def safe_add_url_rule(bp, rule, endpoint=None, view_func=None, **opts):
    name = endpoint or (view_func.__name__ if view_func else None)
    if name and '.' in name:
        raise ValueError(f"endpoint {name!r} contains a dot; use a plain name")
    bp.add_url_rule(rule, endpoint, view_func, **opts)

Prevention

When it happens

Trigger: Calling bp.add_url_rule(rule, view_func=fn) or @bp.route where fn.__name__ contains a '.', e.g. a lambda or wrapper whose __name__ was manually set to 'api.users', or a functools.partial/decorated callable given a dotted __name__. Also raised (sibling check) when passing endpoint='a.b' explicitly to a blueprint.

Common situations: Developers trying to namespace endpoints by putting dots into function names or endpoint arguments instead of using nested blueprints; class-based views wrapped with as_view('name.with.dot'); code generators building view functions with qualified names copied into __name__.

Related errors


AI-assisted analysis of pallets/flask@6a2f545bfd (2026-07-31). Data as JSON: /data/errors/73cbd4114851efcf.json. Report an issue: GitHub ↗.