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

Request: supporting filter syntax that doesn't collide with f-strings #362

Open
jamesbraza opened this issue Jan 9, 2024 · 2 comments
Open

Comments

@jamesbraza
Copy link

Currently, filter syntax directly overlaps with f-string syntax. This make string building URLs a little more cumbersome:

import httpx

def get_user_from_substring(sub_name: str):
    # I would like to use f-string in URL, but this collides with filter syntax
    response = httpx.get(f"/users/?filter{name.icontains}={sub_name}")
    ...

def get_user_from_substring(sub_name: str):
    # Workaround 1: use `__add__`
    response = httpx.get("/users/?filter{name.icontains}=" + sub_name)
    # Workaround 2: use double curly
    response = httpx.get(f"/users/?filter{{name.icontains}}={sub_name}")
    # Workaround 3: use string concatenation (black autoformats this to Workaround 2)
    response = httpx.get(
        "/users/?filter{name.icontains}=" f"{sub_name}"
    )

    ...

Can we support an alternate syntax for filter strings, so one can avoid the workarounds? Perhaps []

@suavesav
Copy link
Contributor

suavesav commented Apr 8, 2024

Hi @jamesbraza, why would you like to avoid workaround #2?

@jamesbraza
Copy link
Author

Thanks for asking for clarification. I wrote it in the original post, but to restate more concisely, it's to avoid overlap with f-strings:

# Here is an f-string and filter syntax collision, which will lead to a bug
httpx.get(f"/users/?filter{name.icontains}={sub_name}")
# Here is a workaround (requires knowing about the possible collision) to avoid the bug
httpx.get(f"/users/?filter{{name.icontains}}={sub_name}")
# What I am asking for (or something like this): configurability to avoid f-string syntax collision
httpx.get(f"/users/?filter[name.icontains]={sub_name}")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants