Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cumbersome to pass curly brackets to field converters #2062

Open
vytas7 opened this issue May 7, 2022 · 3 comments
Open

Cumbersome to pass curly brackets to field converters #2062

vytas7 opened this issue May 7, 2022 · 3 comments

Comments

@vytas7
Copy link
Member

vytas7 commented May 7, 2022

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"

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?

@vytas7
Copy link
Member Author

vytas7 commented May 13, 2022

Perhaps we could support escaping curly brackets by doubling them ({{ & }}), just as the Python format string syntax does?

@CaselIT
Copy link
Member

CaselIT commented May 13, 2022

Perhaps we could support escaping curly brackets by doubling them ({{ & }}), just as the Python format string syntax does?

I like that idea, since it would be somewhat natural to python users to escape them that way. Especially since f-string has become a thing

@vytas7
Copy link
Member Author

vytas7 commented May 13, 2022

(It's the same {{ & }} even in str.format(), which dates back to early 3.x versions and 2.7.)

@vytas7 vytas7 added this to the Version 4.x milestone Jul 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants