]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Implement __repr__ for route classes (#1864)
authorAlex Oleshkevich <alex.oleshkevich@gmail.com>
Fri, 23 Sep 2022 12:54:55 +0000 (15:54 +0300)
committerGitHub <noreply@github.com>
Fri, 23 Sep 2022 12:54:55 +0000 (14:54 +0200)
* implement __repr__ for Route

* implemenr __repr__ for WebSocketRoute, Host, and Mount.

* fix linting issues

* change repr format

* force repr() for inner apps

* Update starlette/routing.py

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
* Update starlette/routing.py

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
* Update starlette/routing.py

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
* Update starlette/routing.py

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
* Update tests/test_routing.py

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
* Update tests/test_routing.py

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
* Update tests/test_routing.py

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
* Update tests/test_routing.py

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
* Update tests/test_routing.py

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
* fix linting issues and tests

* remove repr from Router

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
starlette/routing.py
tests/test_routing.py

index 2c6965be0f30876c72050409e50395b3e9fd321c..479d4ae6538e5af217ad01408e73449a3f9b23dc 100644 (file)
@@ -283,6 +283,12 @@ class Route(BaseRoute):
             and self.methods == other.methods
         )
 
+    def __repr__(self) -> str:
+        class_name = self.__class__.__name__
+        methods = sorted(self.methods or [])
+        path, name = self.path, self.name
+        return f"{class_name}(path={path!r}, name={name!r}, methods={methods!r})"
+
 
 class WebSocketRoute(BaseRoute):
     def __init__(
@@ -341,6 +347,9 @@ class WebSocketRoute(BaseRoute):
             and self.endpoint == other.endpoint
         )
 
+    def __repr__(self) -> str:
+        return f"{self.__class__.__name__}(path={self.path!r}, name={self.name!r})"
+
 
 class Mount(BaseRoute):
     def __init__(
@@ -440,6 +449,11 @@ class Mount(BaseRoute):
             and self.app == other.app
         )
 
+    def __repr__(self) -> str:
+        class_name = self.__class__.__name__
+        name = self.name or ""
+        return f"{class_name}(path={self.path!r}, name={name!r}, app={self.app!r})"
+
 
 class Host(BaseRoute):
     def __init__(
@@ -507,6 +521,11 @@ class Host(BaseRoute):
             and self.app == other.app
         )
 
+    def __repr__(self) -> str:
+        class_name = self.__class__.__name__
+        name = self.name or ""
+        return f"{class_name}(host={self.host!r}, name={name!r}, app={self.app!r})"
+
 
 _T = typing.TypeVar("_T")
 
index 750f324960cef7a812f4879a7227084a8f92570c..e2c2eb2c4b729be85888a3dc1aede8602ba96975 100644 (file)
@@ -952,3 +952,71 @@ def test_mounted_middleware_does_not_catch_exception(
     resp = client.get("/mount/err")
     assert resp.status_code == 403, resp.content
     assert "X-Mounted" not in resp.headers
+
+
+def test_route_repr() -> None:
+    route = Route("/welcome", endpoint=homepage)
+    assert (
+        repr(route)
+        == "Route(path='/welcome', name='homepage', methods=['GET', 'HEAD'])"
+    )
+
+
+def test_route_repr_without_methods() -> None:
+    route = Route("/welcome", endpoint=Endpoint, methods=None)
+    assert repr(route) == "Route(path='/welcome', name='Endpoint', methods=[])"
+
+
+def test_websocket_route_repr() -> None:
+    route = WebSocketRoute("/ws", endpoint=websocket_endpoint)
+    assert repr(route) == "WebSocketRoute(path='/ws', name='websocket_endpoint')"
+
+
+def test_mount_repr() -> None:
+    route = Mount(
+        "/app",
+        routes=[
+            Route("/", endpoint=homepage),
+        ],
+    )
+    # test for substring because repr(Router) returns unique object ID
+    assert repr(route).startswith("Mount(path='/app', name='', app=")
+
+
+def test_mount_named_repr() -> None:
+    route = Mount(
+        "/app",
+        name="app",
+        routes=[
+            Route("/", endpoint=homepage),
+        ],
+    )
+    # test for substring because repr(Router) returns unique object ID
+    assert repr(route).startswith("Mount(path='/app', name='app', app=")
+
+
+def test_host_repr() -> None:
+    route = Host(
+        "example.com",
+        app=Router(
+            [
+                Route("/", endpoint=homepage),
+            ]
+        ),
+    )
+    # test for substring because repr(Router) returns unique object ID
+    assert repr(route).startswith("Host(host='example.com', name='', app=")
+
+
+def test_host_named_repr() -> None:
+    route = Host(
+        "example.com",
+        name="app",
+        app=Router(
+            [
+                Route("/", endpoint=homepage),
+            ]
+        ),
+    )
+    # test for substring because repr(Router) returns unique object ID
+    assert repr(route).startswith("Host(host='example.com', name='app', app=")