From 70ade1b6a4987cb92126b5eec534266f4499f2c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Sun, 21 Jul 2024 14:14:31 +0200 Subject: [PATCH] Fix `routing.get_name()` not to assume all routines have `__name__` Fix `routing.get_name()` to use the `__name__` attribute only if it is actually present, rather than assuming that all routine and class types have it, and use the fallback to class name otherwise. This is necessary for `functools.partial()` that's not guaranteed to have a `__name__`, and in fact does not have one starting with Python 3.13.0b3. Given that the `__name__` used to be `partial` before, this change effectively preserves the old behavior for `partial`s across all Python implementations. Fixes #2638 --- starlette/routing.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/starlette/routing.py b/starlette/routing.py index 75a5ec3f3..481b13f5d 100644 --- a/starlette/routing.py +++ b/starlette/routing.py @@ -99,9 +99,7 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None: def get_name(endpoint: typing.Callable[..., typing.Any]) -> str: - if inspect.isroutine(endpoint) or inspect.isclass(endpoint): - return endpoint.__name__ - return endpoint.__class__.__name__ + return getattr(endpoint, "__name__", endpoint.__class__.__name__) def replace_params(