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

Enhancement: default handlers names for url_for use #2892

Open
euri10 opened this issue Dec 13, 2023 · 4 comments
Open

Enhancement: default handlers names for url_for use #2892

euri10 opened this issue Dec 13, 2023 · 4 comments
Labels
Enhancement This is a new feature or request

Comments

@euri10
Copy link
Contributor

euri10 commented Dec 13, 2023

Summary

Currently if in a template I want t o do:
<a href="{{ url_for("login_get_name") }}"></a>

then in litestar I have to explicitely use the name kwarg in the handler's decorator:

@get("/login", name="login_get_name")
async def login_get_func(
    request: Request
) -> Template:

I'd like to be able to do <a href="{{ url_for("login_get_func") }}"></a> with:

@get("/login")
async def login_get_func(
    request: Request
) -> Template:

currently a 500 is raised
litestar.exceptions.http_exceptions.NoRouteMatchFoundException: 500: Route login_get_func can not be found

Basic Example

No response

Drawbacks and Impact

No response

Unresolved questions

No response


Note

While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.

Check out all issues funded or available for funding on our Polar.sh dashboard

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.
Fund with Polar
@euri10 euri10 added the Enhancement This is a new feature or request label Dec 13, 2023
@peterschutt
Copy link
Contributor

peterschutt commented Dec 13, 2023

A few things to think about:

  • if functions with the same name are declared in different modules and both registered on the app:
# a.py

@get("/a")
def get_handler() -> A:
    ...
# b.py

@get("/b")
def get_handler() -> B:
    ...
# app.py

from a import get_handler as get_a
from b import get_handler as get_b

app = Litestar([get_a, get_b])
  • where handlers are registered on a router
  • where handlers are registered on a controller

What if we built a name from the handler method/func that included the module path and the __qualname__ of the handler?

@provinzkraut
Copy link
Member

if functions with the same name are declared in different modules and both registered on the app

Also, if a handler is registered multiple times

@erhuabushuo
Copy link
Contributor

erhuabushuo commented Dec 16, 2023

What about "module.function"

@provinzkraut
Copy link
Member

What about "module.function"

Unfortunately it's not that easy, because it would mean that

class SomeController(Controller):
  @get()
  def handler(self) -> str:
    ...

and

@get()
def handler() -> str:
  ...

would produce the same name.

The pattern @peterschutt suggested in this comment, using __qualname__ would get around this, since __qualname__ is the "full attribute path" to an object, taking into account containing objects such as classes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement This is a new feature or request
Projects
Status: Triage
Development

Successfully merging a pull request may close this issue.

4 participants