From 0ac936e36915532c4f1aad2099365868c32aec1b Mon Sep 17 00:00:00 2001 From: "alex.oleshkevich" Date: Thu, 22 Sep 2022 14:35:08 +0300 Subject: [PATCH 01/16] implement __repr__ for Route --- starlette/routing.py | 3 +++ tests/test_routing.py | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/starlette/routing.py b/starlette/routing.py index 2c6965be0..de68c0b87 100644 --- a/starlette/routing.py +++ b/starlette/routing.py @@ -283,6 +283,9 @@ def __eq__(self, other: typing.Any) -> bool: and self.methods == other.methods ) + def __repr__(self) -> str: + return f"<{self.__class__.__name__}: path={self.path}, name={self.name}, methods={sorted(self.methods)}>" + class WebSocketRoute(BaseRoute): def __init__( diff --git a/tests/test_routing.py b/tests/test_routing.py index 750f32496..de776c127 100644 --- a/tests/test_routing.py +++ b/tests/test_routing.py @@ -952,3 +952,10 @@ async def modified_send(msg: Message) -> None: 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) == "" + ) From 049edb9731a09b2ebebbfd320d03a7781418d363 Mon Sep 17 00:00:00 2001 From: "alex.oleshkevich" Date: Thu, 22 Sep 2022 14:46:50 +0300 Subject: [PATCH 02/16] implemenr __repr__ for WebSocketRoute, Host, and Mount. --- starlette/routing.py | 14 +++++++++ tests/test_routing.py | 70 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/starlette/routing.py b/starlette/routing.py index de68c0b87..9f5aa8789 100644 --- a/starlette/routing.py +++ b/starlette/routing.py @@ -344,6 +344,9 @@ def __eq__(self, other: typing.Any) -> bool: and self.endpoint == other.endpoint ) + def __repr__(self) -> str: + return f"<{self.__class__.__name__}: path={self.path}, name={self.name}>" + class Mount(BaseRoute): def __init__( @@ -443,6 +446,9 @@ def __eq__(self, other: typing.Any) -> bool: and self.app == other.app ) + def __repr__(self) -> str: + return f"<{self.__class__.__name__}: path={self.path}, name={self.name or ''}, app={self.app}>" + class Host(BaseRoute): def __init__( @@ -510,6 +516,9 @@ def __eq__(self, other: typing.Any) -> bool: and self.app == other.app ) + def __repr__(self) -> str: + return f"<{self.__class__.__name__}: host={self.host}, name={self.name or ''}, app={self.app}>" + _T = typing.TypeVar("_T") @@ -850,3 +859,8 @@ def decorator(func: typing.Callable) -> typing.Callable: return func return decorator + + def __repr__(self) -> str: + routes_count = len(self.routes) + noun = "route" if routes_count == 1 else "routes" + return f"<{self.__class__.__name__}: {routes_count} {noun}>" diff --git a/tests/test_routing.py b/tests/test_routing.py index de776c127..05899e2ab 100644 --- a/tests/test_routing.py +++ b/tests/test_routing.py @@ -959,3 +959,73 @@ def test_route_repr() -> None: assert ( repr(route) == "" ) + + +def test_websocket_route_repr() -> None: + route = WebSocketRoute("/ws", endpoint=websocket_endpoint) + assert repr(route) == "" + + +def test_mount_repr() -> None: + route = Mount( + "/app", + routes=[ + Route("/", endpoint=homepage), + ], + ) + assert repr(route) == ">" + + +def test_mount_named_repr() -> None: + route = Mount( + "/app", + name="app", + routes=[ + Route("/", endpoint=homepage), + ], + ) + assert repr(route) == ">" + + +def test_host_repr() -> None: + route = Host( + "example.com", + app=Router( + [ + Route("/", endpoint=homepage), + ] + ), + ) + assert repr(route) == ">" + + +def test_host_named_repr() -> None: + route = Host( + "example.com", + name="app", + app=Router( + [ + Route("/", endpoint=homepage), + ] + ), + ) + assert repr(route) == ">" + + +def test_router_repr() -> None: + route = Router( + routes=[ + Route("/", endpoint=homepage), + ] + ) + assert repr(route) == "" + + +def test_router_repr_plural() -> None: + route = Router( + routes=[ + Route("/", endpoint=homepage), + Route("/app", endpoint=homepage), + ] + ) + assert repr(route) == "" From 8768bb9a14e0751ab36d7eec83862a7b1b345d02 Mon Sep 17 00:00:00 2001 From: "alex.oleshkevich" Date: Thu, 22 Sep 2022 14:55:20 +0300 Subject: [PATCH 03/16] fix linting issues --- starlette/routing.py | 15 ++++++++++++--- tests/test_routing.py | 5 +++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/starlette/routing.py b/starlette/routing.py index 9f5aa8789..af5ddab22 100644 --- a/starlette/routing.py +++ b/starlette/routing.py @@ -284,7 +284,10 @@ def __eq__(self, other: typing.Any) -> bool: ) def __repr__(self) -> str: - return f"<{self.__class__.__name__}: path={self.path}, name={self.name}, methods={sorted(self.methods)}>" + return ( + f"<{self.__class__.__name__}: " + f"path={self.path}, name={self.name}, methods={sorted(self.methods or [])}>" + ) class WebSocketRoute(BaseRoute): @@ -447,7 +450,10 @@ def __eq__(self, other: typing.Any) -> bool: ) def __repr__(self) -> str: - return f"<{self.__class__.__name__}: path={self.path}, name={self.name or ''}, app={self.app}>" + return ( + f"<{self.__class__.__name__}: " + f"path={self.path}, name={self.name or ''}, app={self.app}>" + ) class Host(BaseRoute): @@ -517,7 +523,10 @@ def __eq__(self, other: typing.Any) -> bool: ) def __repr__(self) -> str: - return f"<{self.__class__.__name__}: host={self.host}, name={self.name or ''}, app={self.app}>" + return ( + f"<{self.__class__.__name__}: " + f"host={self.host}, name={self.name or ''}, app={self.app}>" + ) _T = typing.TypeVar("_T") diff --git a/tests/test_routing.py b/tests/test_routing.py index 05899e2ab..fe3395ffd 100644 --- a/tests/test_routing.py +++ b/tests/test_routing.py @@ -961,6 +961,11 @@ def test_route_repr() -> None: ) +def test_route_repr_without_methods() -> None: + route = Route("/welcome", endpoint=Endpoint, methods=None) + assert repr(route) == "" + + def test_websocket_route_repr() -> None: route = WebSocketRoute("/ws", endpoint=websocket_endpoint) assert repr(route) == "" From c9851666450f7b1ef79c60734038812cb7553663 Mon Sep 17 00:00:00 2001 From: "alex.oleshkevich" Date: Thu, 22 Sep 2022 18:15:24 +0300 Subject: [PATCH 04/16] change repr format --- starlette/routing.py | 17 +++++++++-------- tests/test_routing.py | 19 ++++++++++--------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/starlette/routing.py b/starlette/routing.py index af5ddab22..2544e471b 100644 --- a/starlette/routing.py +++ b/starlette/routing.py @@ -285,8 +285,9 @@ def __eq__(self, other: typing.Any) -> bool: def __repr__(self) -> str: return ( - f"<{self.__class__.__name__}: " - f"path={self.path}, name={self.name}, methods={sorted(self.methods or [])}>" + f"{self.__class__.__name__}(" + f'path="{self.path}", name="{self.name}", ' + f"methods={sorted(self.methods or [])})" ) @@ -348,7 +349,7 @@ def __eq__(self, other: typing.Any) -> bool: ) def __repr__(self) -> str: - return f"<{self.__class__.__name__}: path={self.path}, name={self.name}>" + return f'{self.__class__.__name__}(path="{self.path}", name="{self.name}")' class Mount(BaseRoute): @@ -451,8 +452,8 @@ def __eq__(self, other: typing.Any) -> bool: def __repr__(self) -> str: return ( - f"<{self.__class__.__name__}: " - f"path={self.path}, name={self.name or ''}, app={self.app}>" + f"{self.__class__.__name__}(" + f'path="{self.path}", name="{self.name or ""}", app="{self.app}")' ) @@ -524,8 +525,8 @@ def __eq__(self, other: typing.Any) -> bool: def __repr__(self) -> str: return ( - f"<{self.__class__.__name__}: " - f"host={self.host}, name={self.name or ''}, app={self.app}>" + f"{self.__class__.__name__}(" + f'host="{self.host}", name="{self.name or ""}", app="{self.app}")' ) @@ -872,4 +873,4 @@ def decorator(func: typing.Callable) -> typing.Callable: def __repr__(self) -> str: routes_count = len(self.routes) noun = "route" if routes_count == 1 else "routes" - return f"<{self.__class__.__name__}: {routes_count} {noun}>" + return f"{self.__class__.__name__}({routes_count} {noun})" diff --git a/tests/test_routing.py b/tests/test_routing.py index fe3395ffd..d159882f8 100644 --- a/tests/test_routing.py +++ b/tests/test_routing.py @@ -957,18 +957,19 @@ async def modified_send(msg: Message) -> None: def test_route_repr() -> None: route = Route("/welcome", endpoint=homepage) assert ( - repr(route) == "" + 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) == "" + assert repr(route) == 'Route(path="/welcome", name="Endpoint", methods=[])' def test_websocket_route_repr() -> None: route = WebSocketRoute("/ws", endpoint=websocket_endpoint) - assert repr(route) == "" + assert repr(route) == 'WebSocketRoute(path="/ws", name="websocket_endpoint")' def test_mount_repr() -> None: @@ -978,7 +979,7 @@ def test_mount_repr() -> None: Route("/", endpoint=homepage), ], ) - assert repr(route) == ">" + assert repr(route) == 'Mount(path="/app", name="", app="Router(1 route)")' def test_mount_named_repr() -> None: @@ -989,7 +990,7 @@ def test_mount_named_repr() -> None: Route("/", endpoint=homepage), ], ) - assert repr(route) == ">" + assert repr(route) == 'Mount(path="/app", name="app", app="Router(1 route)")' def test_host_repr() -> None: @@ -1001,7 +1002,7 @@ def test_host_repr() -> None: ] ), ) - assert repr(route) == ">" + assert repr(route) == 'Host(host="example.com", name="", app="Router(1 route)")' def test_host_named_repr() -> None: @@ -1014,7 +1015,7 @@ def test_host_named_repr() -> None: ] ), ) - assert repr(route) == ">" + assert repr(route) == 'Host(host="example.com", name="app", app="Router(1 route)")' def test_router_repr() -> None: @@ -1023,7 +1024,7 @@ def test_router_repr() -> None: Route("/", endpoint=homepage), ] ) - assert repr(route) == "" + assert repr(route) == "Router(1 route)" def test_router_repr_plural() -> None: @@ -1033,4 +1034,4 @@ def test_router_repr_plural() -> None: Route("/app", endpoint=homepage), ] ) - assert repr(route) == "" + assert repr(route) == "Router(2 routes)" From 94df496d4ce5f3fd1e7c65353088b7e08b2a1c48 Mon Sep 17 00:00:00 2001 From: "alex.oleshkevich" Date: Thu, 22 Sep 2022 18:16:17 +0300 Subject: [PATCH 05/16] force repr() for inner apps --- starlette/routing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/starlette/routing.py b/starlette/routing.py index 2544e471b..ca52f9fcb 100644 --- a/starlette/routing.py +++ b/starlette/routing.py @@ -453,7 +453,7 @@ def __eq__(self, other: typing.Any) -> bool: def __repr__(self) -> str: return ( f"{self.__class__.__name__}(" - f'path="{self.path}", name="{self.name or ""}", app="{self.app}")' + f'path="{self.path}", name="{self.name or ""}", app="{self.app!r}")' ) @@ -526,7 +526,7 @@ def __eq__(self, other: typing.Any) -> bool: def __repr__(self) -> str: return ( f"{self.__class__.__name__}(" - f'host="{self.host}", name="{self.name or ""}", app="{self.app}")' + f'host="{self.host}", name="{self.name or ""}", app="{self.app!r}")' ) From af81645f838129c59623b3e94ad4817f9713fd6d Mon Sep 17 00:00:00 2001 From: Alex Oleshkevich Date: Thu, 22 Sep 2022 19:32:58 +0300 Subject: [PATCH 06/16] Update starlette/routing.py Co-authored-by: Marcelo Trylesinski --- starlette/routing.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/starlette/routing.py b/starlette/routing.py index ca52f9fcb..549850a8f 100644 --- a/starlette/routing.py +++ b/starlette/routing.py @@ -284,11 +284,10 @@ def __eq__(self, other: typing.Any) -> bool: ) def __repr__(self) -> str: - return ( - f"{self.__class__.__name__}(" - f'path="{self.path}", name="{self.name}", ' - f"methods={sorted(self.methods or [])})" - ) + 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): From ff8d3a9752d19908e1a81c45b0eca26d8885a5ee Mon Sep 17 00:00:00 2001 From: Alex Oleshkevich Date: Thu, 22 Sep 2022 19:33:13 +0300 Subject: [PATCH 07/16] Update starlette/routing.py Co-authored-by: Marcelo Trylesinski --- starlette/routing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/starlette/routing.py b/starlette/routing.py index 549850a8f..461bb1351 100644 --- a/starlette/routing.py +++ b/starlette/routing.py @@ -348,7 +348,7 @@ def __eq__(self, other: typing.Any) -> bool: ) def __repr__(self) -> str: - return f'{self.__class__.__name__}(path="{self.path}", name="{self.name}")' + return f"{self.__class__.__name__}(path={self.path!r}, name={self.name!r})" class Mount(BaseRoute): From c8ce4eb5943d4a7c5f85a639c31ecd3dbb83ced4 Mon Sep 17 00:00:00 2001 From: Alex Oleshkevich Date: Thu, 22 Sep 2022 19:33:18 +0300 Subject: [PATCH 08/16] Update starlette/routing.py Co-authored-by: Marcelo Trylesinski --- starlette/routing.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/starlette/routing.py b/starlette/routing.py index 461bb1351..7134ebcbe 100644 --- a/starlette/routing.py +++ b/starlette/routing.py @@ -450,10 +450,9 @@ def __eq__(self, other: typing.Any) -> bool: ) def __repr__(self) -> str: - return ( - f"{self.__class__.__name__}(" - f'path="{self.path}", name="{self.name or ""}", app="{self.app!r}")' - ) + 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): From d7a721f348ccb24713c2051550c1e1b6237f301f Mon Sep 17 00:00:00 2001 From: Alex Oleshkevich Date: Thu, 22 Sep 2022 19:33:32 +0300 Subject: [PATCH 09/16] Update starlette/routing.py Co-authored-by: Marcelo Trylesinski --- starlette/routing.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/starlette/routing.py b/starlette/routing.py index 7134ebcbe..feff8b99d 100644 --- a/starlette/routing.py +++ b/starlette/routing.py @@ -522,10 +522,9 @@ def __eq__(self, other: typing.Any) -> bool: ) def __repr__(self) -> str: - return ( - f"{self.__class__.__name__}(" - f'host="{self.host}", name="{self.name or ""}", app="{self.app!r}")' - ) + class_name = self.__class__.__name__ + name = self.name or "" + return f"{class_name}(host={self.host!r}, name={self.name!r}, app={self.app!r})" _T = typing.TypeVar("_T") From d66d46053f93337e0d97583171ea882f67c17e1b Mon Sep 17 00:00:00 2001 From: Alex Oleshkevich Date: Thu, 22 Sep 2022 19:34:28 +0300 Subject: [PATCH 10/16] Update tests/test_routing.py Co-authored-by: Marcelo Trylesinski --- tests/test_routing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_routing.py b/tests/test_routing.py index d159882f8..cb2ed4b4e 100644 --- a/tests/test_routing.py +++ b/tests/test_routing.py @@ -969,7 +969,7 @@ def test_route_repr_without_methods() -> None: def test_websocket_route_repr() -> None: route = WebSocketRoute("/ws", endpoint=websocket_endpoint) - assert repr(route) == 'WebSocketRoute(path="/ws", name="websocket_endpoint")' + assert repr(route) == "WebSocketRoute(path='/ws', name='websocket_endpoint')" def test_mount_repr() -> None: From 442a9512e9c7770cbeea6eb158ec70e56e17a9c8 Mon Sep 17 00:00:00 2001 From: Alex Oleshkevich Date: Thu, 22 Sep 2022 19:34:34 +0300 Subject: [PATCH 11/16] Update tests/test_routing.py Co-authored-by: Marcelo Trylesinski --- tests/test_routing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_routing.py b/tests/test_routing.py index cb2ed4b4e..982af5805 100644 --- a/tests/test_routing.py +++ b/tests/test_routing.py @@ -990,7 +990,7 @@ def test_mount_named_repr() -> None: Route("/", endpoint=homepage), ], ) - assert repr(route) == 'Mount(path="/app", name="app", app="Router(1 route)")' + assert repr(route) == "Mount(path='/app', name='app', app=\"Router(1 route)\")" def test_host_repr() -> None: From c29042a41900b83af373e810a0b9f85ef169899f Mon Sep 17 00:00:00 2001 From: Alex Oleshkevich Date: Thu, 22 Sep 2022 19:34:40 +0300 Subject: [PATCH 12/16] Update tests/test_routing.py Co-authored-by: Marcelo Trylesinski --- tests/test_routing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_routing.py b/tests/test_routing.py index 982af5805..594c5188c 100644 --- a/tests/test_routing.py +++ b/tests/test_routing.py @@ -1002,7 +1002,7 @@ def test_host_repr() -> None: ] ), ) - assert repr(route) == 'Host(host="example.com", name="", app="Router(1 route)")' + assert repr(route) == "Host(host='example.com', name='', app=\"Router(1 route)\")" def test_host_named_repr() -> None: From 77d764d5d42bca8bceba2bc03d9335372a661eeb Mon Sep 17 00:00:00 2001 From: Alex Oleshkevich Date: Thu, 22 Sep 2022 19:34:45 +0300 Subject: [PATCH 13/16] Update tests/test_routing.py Co-authored-by: Marcelo Trylesinski --- tests/test_routing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_routing.py b/tests/test_routing.py index 594c5188c..222019c69 100644 --- a/tests/test_routing.py +++ b/tests/test_routing.py @@ -1015,7 +1015,7 @@ def test_host_named_repr() -> None: ] ), ) - assert repr(route) == 'Host(host="example.com", name="app", app="Router(1 route)")' + assert repr(route) == "Host(host='example.com', name='app', app=\"Router(1 route)\")" def test_router_repr() -> None: From 162abb188f2d7cf6631b02c1ac563e45d5a02adb Mon Sep 17 00:00:00 2001 From: Alex Oleshkevich Date: Thu, 22 Sep 2022 19:34:52 +0300 Subject: [PATCH 14/16] Update tests/test_routing.py Co-authored-by: Marcelo Trylesinski --- tests/test_routing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_routing.py b/tests/test_routing.py index 222019c69..53535b8dc 100644 --- a/tests/test_routing.py +++ b/tests/test_routing.py @@ -979,7 +979,7 @@ def test_mount_repr() -> None: Route("/", endpoint=homepage), ], ) - assert repr(route) == 'Mount(path="/app", name="", app="Router(1 route)")' + assert repr(route) == "Mount(path='/app', name='', app=\"Router(1 route)\")" def test_mount_named_repr() -> None: From c16be82f75b07941771781a0aea6081149604107 Mon Sep 17 00:00:00 2001 From: "alex.oleshkevich" Date: Thu, 22 Sep 2022 19:45:42 +0300 Subject: [PATCH 15/16] fix linting issues and tests --- starlette/routing.py | 2 +- tests/test_routing.py | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/starlette/routing.py b/starlette/routing.py index feff8b99d..7e1202f79 100644 --- a/starlette/routing.py +++ b/starlette/routing.py @@ -524,7 +524,7 @@ def __eq__(self, other: typing.Any) -> bool: def __repr__(self) -> str: class_name = self.__class__.__name__ name = self.name or "" - return f"{class_name}(host={self.host!r}, name={self.name!r}, app={self.app!r})" + return f"{class_name}(host={self.host!r}, name={name!r}, app={self.app!r})" _T = typing.TypeVar("_T") diff --git a/tests/test_routing.py b/tests/test_routing.py index 53535b8dc..e57da5957 100644 --- a/tests/test_routing.py +++ b/tests/test_routing.py @@ -958,13 +958,13 @@ def test_route_repr() -> None: route = Route("/welcome", endpoint=homepage) assert ( repr(route) - == "Route(path=\"/welcome\", name=\"homepage\", methods=['GET', 'HEAD'])" + == "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=[])' + assert repr(route) == "Route(path='/welcome', name='Endpoint', methods=[])" def test_websocket_route_repr() -> None: @@ -979,7 +979,7 @@ def test_mount_repr() -> None: Route("/", endpoint=homepage), ], ) - assert repr(route) == "Mount(path='/app', name='', app=\"Router(1 route)\")" + assert repr(route) == "Mount(path='/app', name='', app=Router(1 route))" def test_mount_named_repr() -> None: @@ -990,7 +990,7 @@ def test_mount_named_repr() -> None: Route("/", endpoint=homepage), ], ) - assert repr(route) == "Mount(path='/app', name='app', app=\"Router(1 route)\")" + assert repr(route) == "Mount(path='/app', name='app', app=Router(1 route))" def test_host_repr() -> None: @@ -1002,7 +1002,7 @@ def test_host_repr() -> None: ] ), ) - assert repr(route) == "Host(host='example.com', name='', app=\"Router(1 route)\")" + assert repr(route) == "Host(host='example.com', name='', app=Router(1 route))" def test_host_named_repr() -> None: @@ -1015,7 +1015,7 @@ def test_host_named_repr() -> None: ] ), ) - assert repr(route) == "Host(host='example.com', name='app', app=\"Router(1 route)\")" + assert repr(route) == "Host(host='example.com', name='app', app=Router(1 route))" def test_router_repr() -> None: From 80878068f53e831fef5fe74fe4aa580bd832b877 Mon Sep 17 00:00:00 2001 From: "alex.oleshkevich" Date: Thu, 22 Sep 2022 20:02:03 +0300 Subject: [PATCH 16/16] remove repr from Router --- starlette/routing.py | 5 ----- tests/test_routing.py | 31 ++++++++----------------------- 2 files changed, 8 insertions(+), 28 deletions(-) diff --git a/starlette/routing.py b/starlette/routing.py index 7e1202f79..479d4ae65 100644 --- a/starlette/routing.py +++ b/starlette/routing.py @@ -866,8 +866,3 @@ def decorator(func: typing.Callable) -> typing.Callable: return func return decorator - - def __repr__(self) -> str: - routes_count = len(self.routes) - noun = "route" if routes_count == 1 else "routes" - return f"{self.__class__.__name__}({routes_count} {noun})" diff --git a/tests/test_routing.py b/tests/test_routing.py index e57da5957..e2c2eb2c4 100644 --- a/tests/test_routing.py +++ b/tests/test_routing.py @@ -979,7 +979,8 @@ def test_mount_repr() -> None: Route("/", endpoint=homepage), ], ) - assert repr(route) == "Mount(path='/app', name='', app=Router(1 route))" + # 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: @@ -990,7 +991,8 @@ def test_mount_named_repr() -> None: Route("/", endpoint=homepage), ], ) - assert repr(route) == "Mount(path='/app', name='app', app=Router(1 route))" + # 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: @@ -1002,7 +1004,8 @@ def test_host_repr() -> None: ] ), ) - assert repr(route) == "Host(host='example.com', name='', app=Router(1 route))" + # 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: @@ -1015,23 +1018,5 @@ def test_host_named_repr() -> None: ] ), ) - assert repr(route) == "Host(host='example.com', name='app', app=Router(1 route))" - - -def test_router_repr() -> None: - route = Router( - routes=[ - Route("/", endpoint=homepage), - ] - ) - assert repr(route) == "Router(1 route)" - - -def test_router_repr_plural() -> None: - route = Router( - routes=[ - Route("/", endpoint=homepage), - Route("/app", endpoint=homepage), - ] - ) - assert repr(route) == "Router(2 routes)" + # test for substring because repr(Router) returns unique object ID + assert repr(route).startswith("Host(host='example.com', name='app', app=")