Skip to content

Commit

Permalink
Implement __repr__ for route classes (#1864)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
alex-oleshkevich and Kludex committed Sep 23, 2022
1 parent e89e131 commit 70971ea
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
19 changes: 19 additions & 0 deletions starlette/routing.py
Expand Up @@ -283,6 +283,12 @@ def __eq__(self, other: typing.Any) -> bool:
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__(
Expand Down Expand Up @@ -341,6 +347,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!r}, name={self.name!r})"


class Mount(BaseRoute):
def __init__(
Expand Down Expand Up @@ -440,6 +449,11 @@ def __eq__(self, other: typing.Any) -> bool:
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__(
Expand Down Expand Up @@ -507,6 +521,11 @@ def __eq__(self, other: typing.Any) -> bool:
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")

Expand Down
68 changes: 68 additions & 0 deletions tests/test_routing.py
Expand Up @@ -952,3 +952,71 @@ 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)
== "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=")

0 comments on commit 70971ea

Please sign in to comment.