Skip to content

Commit 8291aa8

Browse files
authored
Support both import names of PyPI package python-multipart. (#17932)
1 parent 1092a35 commit 8291aa8

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

changelog.d/17932.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Support new package name of PyPI package `python-multipart` 0.0.13 so that distro packagers do not need to work around name conflict with PyPI package `multipart`.

synapse/http/client.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
)
3737

3838
import attr
39-
import multipart
4039
import treq
4140
from canonicaljson import encode_canonical_json
4241
from netaddr import AddrFormatError, IPAddress, IPSet
@@ -93,6 +92,20 @@
9392
if TYPE_CHECKING:
9493
from synapse.server import HomeServer
9594

95+
# Support both import names for the `python-multipart` (PyPI) library,
96+
# which renamed its package name from `multipart` to `python_multipart`
97+
# in 0.0.13 (though supports the old import name for compatibility).
98+
# Note that the `multipart` package name conflicts with `multipart` (PyPI)
99+
# so we should prefer importing from `python_multipart` when possible.
100+
try:
101+
from python_multipart import MultipartParser
102+
103+
if TYPE_CHECKING:
104+
from python_multipart import multipart
105+
except ImportError:
106+
from multipart import MultipartParser # type: ignore[no-redef]
107+
108+
96109
logger = logging.getLogger(__name__)
97110

98111
outgoing_requests_counter = Counter("synapse_http_client_requests", "", ["method"])
@@ -1039,7 +1052,7 @@ def __init__(
10391052
self.deferred = deferred
10401053
self.boundary = boundary
10411054
self.max_length = max_length
1042-
self.parser: Optional[multipart.MultipartParser] = None
1055+
self.parser: Optional[MultipartParser] = None
10431056
self.multipart_response = MultipartResponse()
10441057
self.has_redirect = False
10451058
self.in_json = False
@@ -1097,12 +1110,12 @@ def on_part_data(data: bytes, start: int, end: int) -> None:
10971110
self.deferred.errback()
10981111
self.file_length += end - start
10991112

1100-
callbacks: "multipart.multipart.MultipartCallbacks" = {
1113+
callbacks: "multipart.MultipartCallbacks" = {
11011114
"on_header_field": on_header_field,
11021115
"on_header_value": on_header_value,
11031116
"on_part_data": on_part_data,
11041117
}
1105-
self.parser = multipart.MultipartParser(self.boundary, callbacks)
1118+
self.parser = MultipartParser(self.boundary, callbacks)
11061119

11071120
self.total_length += len(incoming_data)
11081121
if self.max_length is not None and self.total_length >= self.max_length:

0 commit comments

Comments
 (0)