Skip to content

Commit 5efb443

Browse files
authored
fix: replace deprecated typing.ByteString with Union[bytes, bytearray, memoryview] (#575)
typing.ByteString is deprecated since Python 3.9 and will be removed in Python 3.14. Replace all usages in proxy_protocol.py with the explicit Union[bytes, bytearray, memoryview] type annotation, which is compatible with Python 3.9+ and correctly represents the same set of buffer-protocol types. Closes #549 Signed-off-by: Cocoon-Break <86288566+lingxiu58@users.noreply.github.com>
1 parent b73dc49 commit 5efb443

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

aiosmtpd/proxy_protocol.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from enum import IntEnum
1010
from functools import partial
1111
from ipaddress import IPv4Address, IPv6Address, ip_address
12-
from typing import Any, ByteString, Dict, Optional, Protocol, Tuple, Union
12+
from typing import Any, Dict, Optional, Protocol, Tuple, Union
1313

1414
import attr
1515
from public import public
@@ -165,7 +165,7 @@ def same_attribs(self, _raises: bool = False, **kwargs) -> bool:
165165
@classmethod
166166
def parse(
167167
cls,
168-
data: ByteString,
168+
data: Union[bytes, bytearray, memoryview],
169169
partial_ok: bool = True,
170170
strict: bool = False,
171171
) -> Tuple[Dict[str, Any], Dict[str, int]]:
@@ -179,7 +179,7 @@ def parse(
179179
rslt: Dict[str, Any] = {}
180180
tlv_loc: Dict[str, int] = {}
181181

182-
def _pars(chunk: ByteString, *, offset: int) -> None:
182+
def _pars(chunk: Union[bytes, bytearray, memoryview], *, offset: int) -> None:
183183
i = 0
184184
while i < len(chunk):
185185
typ = chunk[i]
@@ -218,7 +218,7 @@ def _pars(chunk: ByteString, *, offset: int) -> None:
218218

219219
@classmethod
220220
def from_raw(
221-
cls, raw: ByteString, strict: bool = False
221+
cls, raw: Union[bytes, bytearray, memoryview], strict: bool = False
222222
) -> Optional["ProxyTLV"]:
223223
"""
224224
Parses raw bytes for TLV Vectors, decode them and giving them human-readable
@@ -265,7 +265,7 @@ class ProxyData:
265265
dst_addr: Optional[EndpointAddress] = _anoinit(default=None)
266266
src_port: Optional[int] = _anoinit(default=None)
267267
dst_port: Optional[int] = _anoinit(default=None)
268-
rest: ByteString = _anoinit(default=b"")
268+
rest: Union[bytes, bytearray, memoryview] = _anoinit(default=b"")
269269
"""
270270
Rest of PROXY Protocol data following UNKNOWN (v1) or UNSPEC (v2), or containing
271271
undecoded TLV (v2). If the latter, you can use the ProxyTLV class to parse the
@@ -340,7 +340,7 @@ def __bool__(self) -> bool:
340340
# Reference: https://github.com/haproxy/haproxy/blob/v2.3.0/doc/proxy-protocol.txt
341341

342342

343-
async def _get_v1(reader: AsyncReader, initial: ByteString = b"") -> ProxyData:
343+
async def _get_v1(reader: AsyncReader, initial: Union[bytes, bytearray, memoryview] = b"") -> ProxyData:
344344
proxy_data = ProxyData(version=1)
345345
proxy_data.whole_raw = bytearray(initial)
346346

@@ -424,7 +424,7 @@ async def get_ap(matcher: "re.Pattern[str]") -> str:
424424
return proxy_data
425425

426426

427-
async def _get_v2(reader: AsyncReader, initial: ByteString = b"") -> ProxyData:
427+
async def _get_v2(reader: AsyncReader, initial: Union[bytes, bytearray, memoryview] = b"") -> ProxyData:
428428
proxy_data = ProxyData(version=2)
429429
whole_raw = bytearray()
430430

0 commit comments

Comments
 (0)