Skip to content

Commit 7080b55

Browse files
committed
endpoint type is Any
1 parent 7555eff commit 7080b55

File tree

4 files changed

+19
-14
lines changed

4 files changed

+19
-14
lines changed

CHANGES.rst

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Unreleased
1010
- Inform Python < 3.12 how to handle ``itms-services`` URIs correctly, rather
1111
than using an overly-broad workaround in Werkzeug that caused some redirect
1212
URIs to be passed on without encoding. :issue:`2828`
13+
- Type annotation for ``Rule.endpoint`` and other uses of ``endpoint`` is
14+
``Any``. :issue:`2836`
1315

1416

1517
Version 3.0.2

src/werkzeug/routing/exceptions.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def __init__(self, path_info: str) -> None:
5959
class RequestAliasRedirect(RoutingException): # noqa: B903
6060
"""This rule is an alias and wants to redirect to the canonical URL."""
6161

62-
def __init__(self, matched_values: t.Mapping[str, t.Any], endpoint: str) -> None:
62+
def __init__(self, matched_values: t.Mapping[str, t.Any], endpoint: t.Any) -> None:
6363
super().__init__()
6464
self.matched_values = matched_values
6565
self.endpoint = endpoint
@@ -72,7 +72,7 @@ class BuildError(RoutingException, LookupError):
7272

7373
def __init__(
7474
self,
75-
endpoint: str,
75+
endpoint: t.Any,
7676
values: t.Mapping[str, t.Any],
7777
method: str | None,
7878
adapter: MapAdapter | None = None,
@@ -93,7 +93,10 @@ def _score_rule(rule: Rule) -> float:
9393
[
9494
0.98
9595
* difflib.SequenceMatcher(
96-
None, rule.endpoint, self.endpoint
96+
# endpoints can be any type, compare as strings
97+
None,
98+
str(rule.endpoint),
99+
str(self.endpoint),
97100
).ratio(),
98101
0.01 * bool(set(self.values or ()).issubset(rule.arguments)),
99102
0.01 * bool(rule.methods and self.method in rule.methods),

src/werkzeug/routing/map.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def __init__(
104104
host_matching: bool = False,
105105
) -> None:
106106
self._matcher = StateMachineMatcher(merge_slashes)
107-
self._rules_by_endpoint: dict[str, list[Rule]] = {}
107+
self._rules_by_endpoint: dict[t.Any, list[Rule]] = {}
108108
self._remap = True
109109
self._remap_lock = self.lock_class()
110110

@@ -131,7 +131,7 @@ def merge_slashes(self) -> bool:
131131
def merge_slashes(self, value: bool) -> None:
132132
self._matcher.merge_slashes = value
133133

134-
def is_endpoint_expecting(self, endpoint: str, *arguments: str) -> bool:
134+
def is_endpoint_expecting(self, endpoint: t.Any, *arguments: str) -> bool:
135135
"""Iterate over all rules and check if the endpoint expects
136136
the arguments provided. This is for example useful if you have
137137
some URLs that expect a language code and others that do not and
@@ -155,7 +155,7 @@ def is_endpoint_expecting(self, endpoint: str, *arguments: str) -> bool:
155155
def _rules(self) -> list[Rule]:
156156
return [rule for rules in self._rules_by_endpoint.values() for rule in rules]
157157

158-
def iter_rules(self, endpoint: str | None = None) -> t.Iterator[Rule]:
158+
def iter_rules(self, endpoint: t.Any | None = None) -> t.Iterator[Rule]:
159159
"""Iterate over all rules or the rules of an endpoint.
160160
161161
:param endpoint: if provided only the rules for that endpoint
@@ -470,14 +470,14 @@ def application(environ, start_response):
470470
raise
471471

472472
@t.overload
473-
def match( # type: ignore
473+
def match(
474474
self,
475475
path_info: str | None = None,
476476
method: str | None = None,
477477
return_rule: t.Literal[False] = False,
478478
query_args: t.Mapping[str, t.Any] | str | None = None,
479479
websocket: bool | None = None,
480-
) -> tuple[str, t.Mapping[str, t.Any]]: ...
480+
) -> tuple[t.Any, t.Mapping[str, t.Any]]: ...
481481

482482
@t.overload
483483
def match(
@@ -496,7 +496,7 @@ def match(
496496
return_rule: bool = False,
497497
query_args: t.Mapping[str, t.Any] | str | None = None,
498498
websocket: bool | None = None,
499-
) -> tuple[str | Rule, t.Mapping[str, t.Any]]:
499+
) -> tuple[t.Any | Rule, t.Mapping[str, t.Any]]:
500500
"""The usage is simple: you just pass the match method the current
501501
path info as well as the method (which defaults to `GET`). The
502502
following things can then happen:
@@ -770,7 +770,7 @@ def make_redirect_url(
770770
def make_alias_redirect_url(
771771
self,
772772
path: str,
773-
endpoint: str,
773+
endpoint: t.Any,
774774
values: t.Mapping[str, t.Any],
775775
method: str,
776776
query_args: t.Mapping[str, t.Any] | str,
@@ -786,7 +786,7 @@ def make_alias_redirect_url(
786786

787787
def _partial_build(
788788
self,
789-
endpoint: str,
789+
endpoint: t.Any,
790790
values: t.Mapping[str, t.Any],
791791
method: str | None,
792792
append_unknown: bool,
@@ -827,7 +827,7 @@ def _partial_build(
827827

828828
def build(
829829
self,
830-
endpoint: str,
830+
endpoint: t.Any,
831831
values: t.Mapping[str, t.Any] | None = None,
832832
method: str | None = None,
833833
force_external: bool = False,

src/werkzeug/routing/rules.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ def __init__(
453453
subdomain: str | None = None,
454454
methods: t.Iterable[str] | None = None,
455455
build_only: bool = False,
456-
endpoint: str | None = None,
456+
endpoint: t.Any | None = None,
457457
strict_slashes: bool | None = None,
458458
merge_slashes: bool | None = None,
459459
redirect_to: str | t.Callable[..., str] | None = None,
@@ -493,7 +493,7 @@ def __init__(
493493
)
494494

495495
self.methods = methods
496-
self.endpoint: str = endpoint # type: ignore
496+
self.endpoint: t.Any = endpoint
497497
self.redirect_to = redirect_to
498498

499499
if defaults:

0 commit comments

Comments
 (0)