Skip to content

Commit 06a0980

Browse files
committed
perf: improve type annotations
1 parent 3ced123 commit 06a0980

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

example/main.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@
1111

1212
# Optionally declares an abstraction
1313
class GreeterAbstraction:
14-
def greet(self):
14+
def greet(self) -> str:
1515
raise NotImplementedError()
1616

1717

1818
# Implement the abstraction and make it available to the injection system
1919
# using the @inject decorator
2020
@inject(alias=GreeterAbstraction)
2121
class GretterImplementation:
22-
def greet(self):
22+
def greet(self) -> str:
2323
return "Hello, world!"
2424

2525

2626
# It is also possible to implement without abstraction and make it available
2727
# to the injection system directly
2828
@inject()
2929
class SpanishGretterImplementation:
30-
def greet(self):
30+
def greet(self) -> str:
3131
return "Hola, mundo!"
3232

3333

@@ -39,7 +39,7 @@ class NestedGretterImplementation:
3939
def __init__(self, spanish_gretter: SpanishGretterImplementation) -> None:
4040
self.gretter = spanish_gretter
4141

42-
def greet(self):
42+
def greet(self) -> str:
4343
return self.gretter.greet()
4444

4545

fastapi_control/controller.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Dict,
88
List,
99
Optional,
10+
ParamSpec,
1011
Sequence,
1112
Set,
1213
Type,
@@ -49,7 +50,7 @@ def api_route(
4950
path: str,
5051
*,
5152
include_in_schema: bool = True,
52-
**kwargs,
53+
**kwargs: Dict[str, Any],
5354
) -> Callable[[DecoratedCallable], DecoratedCallable]:
5455
given_path = path
5556
path_no_slash = given_path[:-1] if given_path.endswith("/") else given_path
@@ -83,6 +84,8 @@ def get_router() -> APIControllerRouter:
8384

8485

8586
T = TypeVar("T", bound=APIController)
87+
ARG = ParamSpec("ARG")
88+
RET = TypeVar("RET")
8689

8790
SetIntStr = Set[Union[int, str]]
8891
DictIntStrAny = Dict[Union[int, str], Any]
@@ -124,44 +127,59 @@ class Config:
124127
arbitrary_types_allowed = True
125128

126129

127-
def get(path: str, **kwargs):
128-
def decorator(fn: Callable[..., Any]):
130+
def get(
131+
path: str,
132+
**kwargs: Dict[str, Any],
133+
) -> Callable[[Callable[ARG, RET]], Callable[ARG, RET]]:
134+
def decorator(fn: Callable[ARG, RET]):
129135
endpoint = RouteArgs(path=path, methods=["GET"], **kwargs)
130136
setattr(fn, ENDPOINT_KEY, endpoint)
131137
return fn
132138

133139
return decorator
134140

135141

136-
def post(path: str, **kwargs):
137-
def decorator(fn: Callable[..., Any]):
142+
def post(
143+
path: str,
144+
**kwargs: Dict[str, Any],
145+
) -> Callable[[Callable[ARG, RET]], Callable[ARG, RET]]:
146+
def decorator(fn: Callable[ARG, RET]):
138147
endpoint = RouteArgs(path=path, methods=["POST"], **kwargs)
139148
setattr(fn, ENDPOINT_KEY, endpoint)
140149
return fn
141150

142151
return decorator
143152

144153

145-
def put(path: str, **kwargs):
146-
def decorator(fn: Callable[..., Any]):
154+
def put(
155+
path: str,
156+
**kwargs: Dict[str, Any],
157+
) -> Callable[[Callable[ARG, RET]], Callable[ARG, RET]]:
158+
def decorator(fn: Callable[ARG, RET]):
147159
endpoint = RouteArgs(path=path, methods=["PUT"], **kwargs)
148160
setattr(fn, ENDPOINT_KEY, endpoint)
149161
return fn
150162

151163
return decorator
152164

153165

154-
def patch(path: str, **kwargs):
155-
def decorator(fn: Callable[..., Any]):
166+
def patch(
167+
path: str,
168+
**kwargs: Dict[str, Any],
169+
) -> Callable[[Callable[ARG, RET]], Callable[ARG, RET]]:
170+
def decorator(fn: Callable[ARG, RET]):
156171
endpoint = RouteArgs(path=path, methods=["PATCH"], **kwargs)
157172
setattr(fn, ENDPOINT_KEY, endpoint)
158173
return fn
159174

160175
return decorator
161176

162177

163-
def delete(path: str, **kwargs):
164-
def decorator(fn: Callable[..., Any]):
178+
def delete(
179+
path: str,
180+
**kwargs: Dict[str, Any],
181+
) -> Callable[[Callable[ARG, RET]], Callable[ARG, RET]]:
182+
def decorator(fn: Callable[ARG, RET]):
165183
endpoint = RouteArgs(path=path, methods=["DELETE"], **kwargs)
166184
setattr(fn, ENDPOINT_KEY, endpoint)
167185
return fn
@@ -272,7 +290,7 @@ def _controller(router: APIRouter, cls: Type[T]) -> Type[T]:
272290
return cls
273291

274292

275-
def _fix_endpoint_signature(cls: Type[Any], endpoint: Callable[..., Any]):
293+
def _fix_endpoint_signature(cls: Type[Any], endpoint: Callable[..., Any]) -> None:
276294
old_signature = signature(endpoint)
277295
old_parameters: List[Parameter] = list(old_signature.parameters.values())
278296
old_first_parameter = old_parameters[0]

0 commit comments

Comments
 (0)