Skip to content

Conversation

@pgjones
Copy link
Member

@pgjones pgjones commented Apr 18, 2025

This is raised if an equivalent rule is added to the matcher to an
existing rule. This should help users of the router know when they've
accidentally duplicated a match.

As from `tox -r -m update`
@davidism davidism changed the base branch from main to stable April 18, 2025 20:32
@davidism davidism changed the base branch from stable to main April 18, 2025 20:32
@502E532E
Copy link

Looks good to me. It should resolve the problem I opened #3037 for.

There are some cases with arguments involved that need to be considered.

This does not raise an error:

_ = r.Map(
    [
        r.Rule("/hello/<argument>", endpoint="hello"),
        r.Rule("/hello/world", endpoint="hello_world"),
    ]
)

but it seems that the second (more specific) rule always takes precedence over the first, regardless of the order they are added (at least in my quick testing). This makes sense and I think is intended behavior, so it should stay this way.

This does raise an error:

_ = r.Map(
    [
        r.Rule("/hello/<arg>", endpoint="hello_world"),
        r.Rule("/hello/<arg>", endpoint="hello"),
    ]
)

which is as expected.

This does not raise an error:

_ = r.Map(
    [
        r.Rule("/hello/<arg1>", endpoint="hello_world"),
        r.Rule("/hello/<arg2>", endpoint="hello"),
    ]
)

but it should do so, because there is no situation in which the second rule can be invoked.
It would be better to warn the user about this as well, because it is almost certainly not intended.

A possible solution would be to make argument names irrelevant in the check for the equality of rules (that is already modified in this pull request)

def __eq__(self, other: object) -> bool:
return isinstance(other, type(self)) and self._trace == other._trace

My basic idea would be that __eq__ just sees /some/<>/url/<>/<>/with/arguments/<>, meaning that argument positions and the fact that arguments are used remains relevant, but not the internal names in the code (which seems to be the current behaviour).
Not sure how though, I did not look close enough to understand what _trace does.

This is raised if an equivalent rule is added to the matcher to an
existing rule. This should help users of the router know when they've
accidentally duplicated a match.
@pgjones
Copy link
Member Author

pgjones commented Apr 27, 2025

Yep, I should have used _parts as updated now.

@502E532E
Copy link

Works perfectly now, thank you!

I do not see further issues and would be happy if this is merged.

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

Successfully merging this pull request may close these issues.

2 participants