diff --git a/starlette/routing.py b/starlette/routing.py index 0388304c9..ea6ec2117 100644 --- a/starlette/routing.py +++ b/starlette/routing.py @@ -84,7 +84,7 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None: def get_name(endpoint: typing.Callable) -> str: - if inspect.isfunction(endpoint) or inspect.isclass(endpoint): + if inspect.isroutine(endpoint) or inspect.isclass(endpoint): return endpoint.__name__ return endpoint.__class__.__name__ diff --git a/tests/test_routing.py b/tests/test_routing.py index 7077c5616..e8adaca48 100644 --- a/tests/test_routing.py +++ b/tests/test_routing.py @@ -1,4 +1,5 @@ import functools +import typing import uuid import pytest @@ -710,3 +711,38 @@ def test_duplicated_param_names(): match="Duplicated param names id, name at path /{id}/{name}/{id}/{name}", ): Route("/{id}/{name}/{id}/{name}", user) + + +class Endpoint: + async def my_method(self, request): + ... # pragma: no cover + + @classmethod + async def my_classmethod(cls, request): + ... # pragma: no cover + + @staticmethod + async def my_staticmethod(request): + ... # pragma: no cover + + def __call__(self, request): + ... # pragma: no cover + + +@pytest.mark.parametrize( + "endpoint, expected_name", + [ + pytest.param(func_homepage, "func_homepage", id="function"), + pytest.param(Endpoint().my_method, "my_method", id="method"), + pytest.param(Endpoint.my_classmethod, "my_classmethod", id="classmethod"), + pytest.param( + Endpoint.my_staticmethod, + "my_staticmethod", + id="staticmethod", + ), + pytest.param(Endpoint(), "Endpoint", id="object"), + pytest.param(lambda request: ..., "", id="lambda"), + ], +) +def test_route_name(endpoint: typing.Callable, expected_name: str): + assert Route(path="/", endpoint=endpoint).name == expected_name