Skip to content

Commit 29a510f

Browse files
authored
[gunicorn] Use TypedDict for proxy_protocol_info (#16003)
1 parent dacba79 commit 29a510f

5 files changed

Lines changed: 25 additions & 21 deletions

File tree

stubs/gunicorn/gunicorn/asgi/parser.pyi

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ class ParseError(Exception): ...
99
class InvalidProxyLine(ParseError): ...
1010
class InvalidProxyHeader(ParseError): ...
1111

12+
@type_check_only
13+
class _ProxyProtocolInfo(TypedDict):
14+
proxy_protocol: Literal["TCP4", "TCP6", "UDP4", "UDP6"]
15+
client_addr: str
16+
client_port: int
17+
proxy_addr: str
18+
proxy_port: int
19+
20+
@type_check_only
21+
class _ProxyProtocolInfoUnknown(TypedDict):
22+
proxy_protocol: Literal["UNKNOWN", "LOCAL", "UNSPEC"]
23+
client_addr: None
24+
client_port: None
25+
proxy_addr: None
26+
proxy_port: None
27+
1228
PP_V2_SIGNATURE: Final[bytes]
1329
RFC9110_6_5_1_FORBIDDEN_TRAILER: Final[frozenset[bytes]]
1430

@@ -38,22 +54,6 @@ class UnsupportedTransferCoding(ParseError): ...
3854
class InvalidChunkSize(ParseError): ...
3955
class InvalidChunkExtension(ParseError): ...
4056

41-
@type_check_only
42-
class _ProxyProtocolInfo(TypedDict):
43-
proxy_protocol: Literal["TCP4", "TCP6", "UDP4", "UDP6"]
44-
client_addr: str
45-
client_port: int
46-
proxy_addr: str
47-
proxy_port: int
48-
49-
@type_check_only
50-
class _ProxyProtocolInfoUnknown(TypedDict):
51-
proxy_protocol: Literal["UNKNOWN", "LOCAL", "UNSPEC"]
52-
client_addr: None
53-
client_port: None
54-
proxy_addr: None
55-
proxy_port: None
56-
5757
class PythonProtocol:
5858
__slots__ = (
5959
"_on_message_begin",
@@ -149,7 +149,7 @@ class CallbackRequest:
149149
content_length: int
150150
chunked: bool
151151
must_close: bool
152-
proxy_protocol_info: dict[str, str | int | None] | None # TODO: Use TypedDict
152+
proxy_protocol_info: _ProxyProtocolInfo | _ProxyProtocolInfoUnknown | None
153153

154154
def __init__(self) -> None: ...
155155
@classmethod

stubs/gunicorn/gunicorn/asgi/uwsgi.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ from gunicorn.config import Config
66
from gunicorn.uwsgi.message import UWSGIRequest
77

88
from .._types import _AddressType
9+
from .parser import _ProxyProtocolInfo, _ProxyProtocolInfoUnknown
910

1011
class AsyncUWSGIRequest(UWSGIRequest):
1112
cfg: Config
@@ -26,7 +27,7 @@ class AsyncUWSGIRequest(UWSGIRequest):
2627
uwsgi_vars: dict[str, str]
2728
modifier1: int
2829
modifier2: int
29-
proxy_protocol_info: dict[str, str | int | None] | None # TODO: Use TypedDict
30+
proxy_protocol_info: _ProxyProtocolInfo | _ProxyProtocolInfoUnknown | None
3031
content_length: int
3132
chunked: bool
3233

stubs/gunicorn/gunicorn/http/message.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ from gunicorn.http.body import Body
88
from gunicorn.http.unreader import Unreader
99

1010
from .._types import _AddressType
11+
from ..asgi.parser import _ProxyProtocolInfo, _ProxyProtocolInfoUnknown
1112

1213
PP_V2_SIGNATURE: Final = b"\x0d\x0a\x0d\x0a\x00\x0d\x0a\x51\x55\x49\x54\x0a"
1314

@@ -66,7 +67,7 @@ class Request(Message):
6667
fragment: str | None
6768
limit_request_line: int
6869
req_number: int
69-
proxy_protocol_info: dict[str, str | int | None] | None # TODO: Use TypedDict
70+
proxy_protocol_info: _ProxyProtocolInfo | _ProxyProtocolInfoUnknown | None
7071

7172
def __init__(self, cfg: Config, unreader: Unreader, peer_addr: _AddressType, req_number: int = 1) -> None: ...
7273
def get_data(self, unreader: Unreader, buf: io.BytesIO, stop: bool = False) -> None: ...

stubs/gunicorn/gunicorn/http2/request.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ from gunicorn.config import Config
66
from gunicorn.http2.stream import HTTP2Stream
77

88
from .._types import _AddressType
9+
from ..asgi.parser import _ProxyProtocolInfo, _ProxyProtocolInfoUnknown
910

1011
class HTTP2Body:
1112
def __init__(self, data: ReadableBuffer) -> None: ...
@@ -33,7 +34,7 @@ class HTTP2Request:
3334
body: HTTP2Body
3435
must_close: bool
3536
req_number: int
36-
proxy_protocol_info: dict[str, str | int | None] | None # TODO: Use TypedDict
37+
proxy_protocol_info: _ProxyProtocolInfo | _ProxyProtocolInfoUnknown | None
3738
priority_weight: int
3839
priority_depends_on: int
3940

stubs/gunicorn/gunicorn/uwsgi/message.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ from gunicorn.http.body import Body
55
from gunicorn.http.unreader import Unreader
66

77
from .._types import _AddressType
8+
from ..asgi.parser import _ProxyProtocolInfo, _ProxyProtocolInfoUnknown
89

910
MAX_UWSGI_VARS: Final = 1000
1011

@@ -28,7 +29,7 @@ class UWSGIRequest:
2829
uwsgi_vars: dict[str, str]
2930
modifier1: int
3031
modifier2: int
31-
proxy_protocol_info: dict[str, str | int | None] | None # TODO: Use TypedDict
32+
proxy_protocol_info: _ProxyProtocolInfo | _ProxyProtocolInfoUnknown | None
3233

3334
def __init__(self, cfg: Config, unreader: Unreader, peer_addr: _AddressType, req_number: int = 1) -> None: ...
3435
def force_close(self) -> None: ...

0 commit comments

Comments
 (0)