Opened 4 months ago

Last modified 4 months ago

#28766 assigned New feature

Add route information to ResolverMatch

Reported by: Benjamin Wohlwend Owned by: Benjamin Wohlwend
Component: Core (URLs) Version: 2.0
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

The new route-based URL patterns are fantastic, but when introspecting the ResolverMatch object, the route information is not available. The route would be of great use for tools/libraries that need to introspect the URL resolver.

GitHub PR with a first try at adding this will follow.

Change History (8)

comment:1 Changed 4 months ago by Benjamin Wohlwend

Has patch: set
Owner: changed from nobody to Benjamin Wohlwend
Status: newassigned

comment:2 Changed 4 months ago by Tomer Chachamu

Triage Stage: UnreviewedAccepted

Thanks for the patch. If it's ready to review you can deassign the Trac ticket. :)

comment:3 Changed 4 months ago by Tim Graham

For URL patterns that use re_path() is the route already available from ResolverMatch? If not, I'm unsure why this attribute would only be set for path().

(By the way, there's no need to deassign the ticket to get it in the review queue.)

comment:4 in reply to:  3 Changed 4 months ago by Benjamin Wohlwend

Replying to Tim Graham:

For URL patterns that use re_path() is the route already available from ResolverMatch? If not, I'm unsure why this attribute would only be set for path().

re_path() uses RegexPattern, which doesn't have a route, but a regex. Do you think the regex should be set in that case? That would simplify the code a bit. Instead of getattr(pattern._route), we could just use str(pattern) (__str__ prints _route for RoutePattern objects, and _regex for RegexPattern).

comment:5 Changed 4 months ago by Tim Graham

Yes, that's what I meant to suggest.

comment:6 Changed 4 months ago by Benjamin Wohlwend

OK. However, in that case I think the attribute on ResolverMatch should be named differently, since AFAICT, "route" is specific to the new simplified URLs from DEP 201. Maybe ResolverMatch.pattern?

comment:7 Changed 4 months ago by Tim Graham

The first argument of re_path() is also documented as route so I think it's okay to use that.

comment:8 Changed 4 months ago by Tim Graham

Patch needs improvement: set
Note: See TracTickets for help on using tickets.
Back to Top