#2062·falcon

Cumbersome to pass curly brackets to field converters

Author: vytas7Created May 7, 2022Updated Sep 11, 2026
Labelsdocumentationenhancementproposal

At the time of this writing, it is cumbersome to instantiate converters with any parameter containing curly brackets in its expression.

For instance, the following attempt to validate color to be one of "red", "green" or "blue"

python
import falcon
import falcon.routing


class In(falcon.routing.BaseConverter):
    def __init__(self, values):
        self._values = frozenset(values)

    def convert(self, value):
        if value not in self._values:
            return None
        return value


class ColorResource:
    def on_get(self, req, resp, color):
        resp.media = {'color': color, 'path': req.path}


app = falcon.App()
app.router_options.converters['in'] = In

app.add_route('/colors/{color:in({"red", "green", "blue"})}', ColorResource())

... fails with the following ValueError: Field names must be valid identifiers (""red", "green", "blue"" is not valid).

One could easily work around the problem in this contrived example by using a list or tuple instead of the set literal, but what if we need curly brackets in a regex, or need to pass a dict?