Skip to content

Commit 5e99fcf

Browse files
authored
[gunicorn] Add precise return types for most of CommandHandlers methods (#15568)
1 parent 15f1aea commit 5e99fcf

File tree

1 file changed

+102
-12
lines changed

1 file changed

+102
-12
lines changed
Lines changed: 102 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,113 @@
11
from _typeshed import Incomplete
2+
from typing import Literal, TypedDict, type_check_only
3+
from typing_extensions import NotRequired
24

35
from gunicorn.arbiter import Arbiter
46

7+
@type_check_only
8+
class _Worker(TypedDict):
9+
pid: int
10+
age: int
11+
booted: bool
12+
last_heartbeat: float
13+
aborted: NotRequired[bool]
14+
apps: NotRequired[list[str]]
15+
16+
@type_check_only
17+
class _ShowWorkersReturnType(TypedDict):
18+
workers: list[_Worker]
19+
count: int
20+
21+
@type_check_only
22+
class _App(TypedDict):
23+
import_path: str
24+
worker_count: int | None
25+
current_workers: int
26+
worker_pids: list[int]
27+
28+
@type_check_only
29+
class _ShowDirtyReturnType(TypedDict):
30+
enabled: bool
31+
pid: int | None
32+
workers: list[_Worker]
33+
apps: list[_App]
34+
35+
@type_check_only
36+
class _ShowStatsReturnType(TypedDict):
37+
uptime: float | None
38+
pid: int
39+
workers_current: int
40+
workers_target: int
41+
workers_spawned: int
42+
workers_killed: int
43+
reloads: int
44+
dirty_arbiter_pid: int | None
45+
46+
@type_check_only
47+
class _ListenerInfo(TypedDict):
48+
address: str
49+
fd: int
50+
type: Literal["unix", "tcp", "tcp6", "unknown"]
51+
52+
@type_check_only
53+
class _ShowListenersReturnType(TypedDict):
54+
listeners: list[_ListenerInfo]
55+
count: int
56+
57+
@type_check_only
58+
class _WorkerAddReturnType(TypedDict):
59+
added: int
60+
previous: int
61+
total: int
62+
63+
@type_check_only
64+
class _WorkerRemovedReturnType(TypedDict):
65+
removed: int
66+
previous: int
67+
total: int
68+
69+
@type_check_only
70+
class _WorkerKillSucessReturnType(TypedDict):
71+
success: Literal[True]
72+
killed: int
73+
74+
@type_check_only
75+
class _WorkerKillFailedReturnType(TypedDict):
76+
success: Literal[False]
77+
error: str
78+
79+
@type_check_only
80+
class _ReloadReturnType(TypedDict):
81+
status: Literal["reloading"]
82+
83+
@type_check_only
84+
class _ReopenReturnType(TypedDict):
85+
status: Literal["reopening"]
86+
87+
@type_check_only
88+
class _ShutdownReturnType(TypedDict):
89+
status: Literal["shutting_down"]
90+
mode: str
91+
92+
@type_check_only
93+
class _HelpReturnType(TypedDict):
94+
commands: dict[str, str]
95+
596
class CommandHandlers:
697
arbiter: Arbiter
798
def __init__(self, arbiter: Arbiter) -> None: ...
8-
# TODO: Use TypedDict for next return types
9-
def show_workers(self) -> dict[str, Incomplete]: ...
10-
def show_dirty(self) -> dict[str, Incomplete]: ...
99+
def show_workers(self) -> _ShowWorkersReturnType: ...
100+
def show_dirty(self) -> _ShowDirtyReturnType: ...
11101
def show_config(self) -> dict[str, Incomplete]: ...
12-
def show_stats(self) -> dict[str, Incomplete]: ...
13-
def show_listeners(self) -> dict[str, Incomplete]: ...
14-
def worker_add(self, count: int = 1) -> dict[str, Incomplete]: ...
15-
def worker_remove(self, count: int = 1) -> dict[str, Incomplete]: ...
16-
def worker_kill(self, pid: int) -> dict[str, Incomplete]: ...
102+
def show_stats(self) -> _ShowStatsReturnType: ...
103+
def show_listeners(self) -> _ShowListenersReturnType: ...
104+
def worker_add(self, count: int = 1) -> _WorkerAddReturnType: ...
105+
def worker_remove(self, count: int = 1) -> _WorkerRemovedReturnType: ...
106+
def worker_kill(self, pid: int) -> _WorkerKillSucessReturnType | _WorkerKillFailedReturnType: ...
17107
def dirty_add(self, count: int = 1) -> dict[str, Incomplete]: ...
18108
def dirty_remove(self, count: int = 1) -> dict[str, Incomplete]: ...
19-
def reload(self) -> dict[str, Incomplete]: ...
20-
def reopen(self) -> dict[str, Incomplete]: ...
21-
def shutdown(self, mode: str = "graceful") -> dict[str, Incomplete]: ...
109+
def reload(self) -> _ReloadReturnType: ...
110+
def reopen(self) -> _ReopenReturnType: ...
111+
def shutdown(self, mode: str = "graceful") -> _ShutdownReturnType: ...
22112
def show_all(self) -> dict[str, Incomplete]: ...
23-
def help(self) -> dict[str, Incomplete]: ...
113+
def help(self) -> _HelpReturnType: ...

0 commit comments

Comments
 (0)