Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement __repr__ for route classes #1864

Merged
merged 17 commits into from Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 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:
return (
f"<{self.__class__.__name__}: "
f"path={self.path}, name={self.name}, methods={sorted(self.methods or [])}>"
)


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}, name={self.name}>"


class Mount(BaseRoute):
def __init__(
Expand Down Expand Up @@ -440,6 +449,12 @@ def __eq__(self, other: typing.Any) -> bool:
and self.app == other.app
)

def __repr__(self) -> str:
return (
f"<{self.__class__.__name__}: "
f"path={self.path}, name={self.name or ''}, app={self.app}>"
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Suggested change
f"path={self.path}, name={self.name or ''}, app={self.app}>"
f"path={self.path}, name={self.name or ''}, app={self.app!r}>"

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the !r is used on all parameters in the other __repr__ that we have in code 🤔

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I got why... It's because with them, there's no need to wrap on parenthesis!

)


class Host(BaseRoute):
def __init__(
Expand Down Expand Up @@ -507,6 +522,12 @@ def __eq__(self, other: typing.Any) -> bool:
and self.app == other.app
)

def __repr__(self) -> str:
return (
f"<{self.__class__.__name__}: "
f"host={self.host}, name={self.name or ''}, app={self.app}>"
)


_T = typing.TypeVar("_T")

Expand Down Expand Up @@ -847,3 +868,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}>"
82 changes: 82 additions & 0 deletions tests/test_routing.py
Expand Up @@ -952,3 +952,85 @@ 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),
],
)
assert repr(route) == "<Mount: path=/app, name=, app=<Router: 1 route>>"


def test_mount_named_repr() -> None:
route = Mount(
"/app",
name="app",
routes=[
Route("/", endpoint=homepage),
],
)
assert repr(route) == "<Mount: path=/app, name=app, app=<Router: 1 route>>"


def test_host_repr() -> None:
route = Host(
"example.com",
app=Router(
[
Route("/", endpoint=homepage),
]
),
)
assert repr(route) == "<Host: host=example.com, name=, app=<Router: 1 route>>"


def test_host_named_repr() -> None:
route = Host(
"example.com",
name="app",
app=Router(
[
Route("/", endpoint=homepage),
]
),
)
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>"