mirror of
https://github.com/element-hq/synapse.git
synced 2026-09-29 05:01:57 +00:00
Type-check RestServlet.PATTERNS (#20258)
Spawning from https://github.com/element-hq/synapse/pull/20143#discussion_r4080708454 `RestServlet.register` read `PATTERNS` via `getattr`, so it was typed as `Any` and mypy never checked subclasses' patterns against what `HttpServer.register_paths` expects. This declares the attribute so that classes extending `RestServlet` provide the right type for the `PATTERNS` attribute.
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
Add type annotations for `RestServlet.PATTERNS` to prevent misuse.
|
||||
|
||||
@@ -27,8 +27,10 @@ import urllib.parse as urlparse
|
||||
from http import HTTPStatus
|
||||
from typing import (
|
||||
TYPE_CHECKING,
|
||||
Iterable,
|
||||
Literal,
|
||||
Mapping,
|
||||
Pattern,
|
||||
Sequence,
|
||||
TypeVar,
|
||||
overload,
|
||||
@@ -944,9 +946,9 @@ class RestServlet:
|
||||
An implementing class can either provide its own custom 'register' method,
|
||||
or use the automatic pattern handling provided by the base class.
|
||||
|
||||
To use this latter, the implementing class instead provides a `PATTERN`
|
||||
class attribute containing a pre-compiled regular expression. The automatic
|
||||
register method will then use this method to register any of the following
|
||||
To use this latter, the implementing class instead provides a `PATTERNS`
|
||||
class attribute containing pre-compiled regular expressions. The automatic
|
||||
register method will then use them to register any of the following
|
||||
instance methods associated with the corresponding HTTP method:
|
||||
|
||||
on_GET
|
||||
@@ -958,9 +960,11 @@ class RestServlet:
|
||||
into the appropriate HTTP response.
|
||||
"""
|
||||
|
||||
PATTERNS: Iterable[Pattern[str]] | None = None
|
||||
|
||||
def register(self, http_server: HttpServer) -> None:
|
||||
"""Register this servlet with the given HTTP server."""
|
||||
patterns = getattr(self, "PATTERNS", None)
|
||||
patterns = self.PATTERNS
|
||||
if patterns:
|
||||
for method in ("GET", "PUT", "POST", "DELETE"):
|
||||
if hasattr(self, "on_%s" % (method,)):
|
||||
|
||||
Reference in New Issue
Block a user