2222"""
2323import asyncio
2424import inspect
25- from typing import Any , Awaitable , Callable , Iterator , Self
25+ from collections .abc import Callable , Coroutine , Iterator
26+ from typing import Any , Self , TypeAlias
2627
2728from starlette .requests import Request
2829from starlette .responses import Response
3536 'View' ,
3637)
3738
39+ ResponseType : TypeAlias = Coroutine [Any , Any , Response ]
3840
39- class _Route :
4041
41- def __init__ (self , ** kwargs ) -> None :
42+ class _Route :
43+ def __init__ (self , ** kwargs : Any ) -> None :
4244 self ._path : str = kwargs ['path' ]
43- self ._coro : Callable [[Any , Request ], Awaitable [ Response ] ] = kwargs ['coro' ]
45+ self ._coro : Callable [[Any , Request ], ResponseType ] = kwargs ['coro' ]
4446 self ._methods : list [str ] = kwargs ['methods' ]
4547 self ._prefix : bool = kwargs ['prefix' ]
4648
@@ -53,31 +55,29 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
5355 await response (scope , receive , send )
5456
5557
56- def route (path : str , / , * , methods : list [str ] | None = None , prefix : bool = True ) -> Callable [..., _Route ]:
58+ def route (path : str , / , * , methods : list [str ] = [ 'GET' ] , prefix : bool = True ) -> Callable [..., _Route ]:
5759 """Decorator which allows a coroutine to be turned into a `starlette.routing.Route` inside a `core.View`.
5860
5961 Parameters
6062 ----------
6163 path: str
6264 The path to this route. By default, the path is prefixed with the View class name.
6365 methods: list[str]
64- The allowed methods for this route. If this is None, this route will default to 'GET'. Defaults to None .
66+ The allowed methods for this route. Defaults to ``[ 'GET']`` .
6567 prefix: bool
6668 Whether the route path should be prefixed with the View class name. Defaults to True.
6769 """
6870
69- if methods is None :
70- methods = ['GET' ]
71-
72- def decorator (coro : Callable [[Any , Request ], Awaitable [Response ]]) -> _Route :
71+ def decorator (coro : Callable [[Any , Request ], ResponseType ]) -> _Route :
7372 if not asyncio .iscoroutinefunction (coro ):
74- raise RuntimeError ('Route must be a coroutine function.' )
73+ raise RuntimeError ('Route callback must be a coroutine function.' )
7574
76- disallowed : list [str ] = [" get" , " post" , " put" , " patch" , " delete" , " options" ]
75+ disallowed : list [str ] = [' get' , ' post' , ' put' , ' patch' , ' delete' , ' options' ]
7776 if coro .__name__ .lower () in disallowed :
78- raise ValueError (f'Route coroutine must not be named any: { ", " .join (disallowed )} ' )
77+ raise ValueError (f'Route callback function must not be named any: { ", " .join (disallowed )} ' )
7978
8079 return _Route (path = path , coro = coro , methods = methods , prefix = prefix )
80+
8181 return decorator
8282
8383
@@ -106,9 +106,9 @@ async def hello_endpoint(self, request: Request) -> Response:
106106
107107 __routes__ : list [Route ]
108108
109- def __new__ (cls , * args , ** kwargs ) -> Self :
110- self : Self = super ().__new__ (cls )
111- name : str = cls .__name__
109+ def __new__ (cls , * args : Any , ** kwargs : Any ) -> Self :
110+ self = super ().__new__ (cls )
111+ name = cls .__name__
112112
113113 self .__routes__ = []
114114
@@ -125,12 +125,9 @@ def __new__(cls, *args, **kwargs) -> Self:
125125 # Due to the way Starlette works, this allows us to have schema documentation...
126126 setattr (member , method , member ._coro )
127127
128- self .__routes__ .append (Route (
129- path = path ,
130- endpoint = member ,
131- methods = member ._methods ,
132- name = f'{ name } .{ member ._coro .__name__ } '
133- ))
128+ self .__routes__ .append (
129+ Route (path = path , endpoint = member , methods = member ._methods , name = f'{ name } .{ member ._coro .__name__ } ' )
130+ )
134131
135132 return self
136133
@@ -143,5 +140,5 @@ def __getitem__(self, index: int) -> Route:
143140 def __len__ (self ) -> int :
144141 return len (self .__routes__ )
145142
146- def __iter__ (self ) -> Iterator :
143+ def __iter__ (self ) -> Iterator [ Route ] :
147144 return iter (self .__routes__ )
0 commit comments