Skip to content
Myr

Tighten multipart implementation types (#975)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Florimond Manca · committed by GitHub · parent f1b3d74 · Browse files · 2 files+21 added−18 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+21 added−18 removed

  • other:+21 added−18 removed

2 files+21 added−18 removed

modified:httpx/_content_streams.py+20 added-17 removed
6 unmodified lines
78import httpcore910from ._exceptions import StreamConsumed11from ._types import RequestData, RequestFiles11from ._types import FileContent, FileTypes, RequestData, RequestFiles12from ._utils import (13 format_form_param,14 guess_content_type,15 peek_filelike_length,
210 unmodified lines
226 """227 A single file field item, within a multipart form field.228 """229230 def __init__(231 self,232 name: str,233 value: typing.Union[typing.IO[str], typing.IO[bytes], tuple],234 ) -> None:230 def __init__(self, name: str, value: FileTypes) -> None:231 self.name = name236 if not isinstance(value, tuple):237 self.filename = Path(str(getattr(value, "name", "upload"))).name238 self.file: typing.Union[typing.IO[str], typing.IO[bytes]] = value239 self.content_type = guess_content_type(self.filename)232233 fileobj: FileContent234235 if isinstance(value, tuple):236 try:237 filename, fileobj, content_type = value # type: ignore238 except ValueError:239 filename, fileobj = value # type: ignore240 content_type = guess_content_type(filename)241 else:241 self.filename = value[0]242 self.file = value[1]243 self.content_type = (244 value[2] if len(value) > 2 else guess_content_type(self.filename)245 )242 filename = Path(str(getattr(value, "name", "upload"))).name243 fileobj = value244 content_type = guess_content_type(filename)245246 self.filename = filename247 self.file = fileobj248 self.content_type = content_type249250 def get_length(self) -> int:251 headers = self.render_headers()252253 if isinstance(self.file, (str, bytes)):
52 unmodified lines
306 yield self.render_headers()307 yield from self.render_data()308309 def __init__(307 self, data: typing.Mapping, files: typing.Mapping, boundary: bytes = None310 self, data: typing.Mapping, files: RequestFiles, boundary: bytes = None311 ) -> None:312 if boundary is None:313 boundary = binascii.hexlify(os.urandom(16))314
3 unmodified lines
318 )319 self.fields = list(self._iter_fields(data, files))320321 def _iter_fields(319 self, data: typing.Mapping, files: typing.Mapping322 self, data: typing.Mapping, files: RequestFiles323 ) -> typing.Iterator[typing.Union["FileField", "DataField"]]:324 for name, value in data.items():325 if isinstance(value, list):326 for item in value:
77 unmodified lines
modified:setup.cfg+1 added-1 removed
9 unmodified lines
10combine_as_imports = True11force_grid_wrap = 012include_trailing_comma = True13known_first_party = httpx,tests14known_third_party = brotli,certifi,chardet,cryptography,hstspreload,httpcore,pytest,rfc3986,setuptools,sniffio,trio,trustme,urllib3,uvicorn14known_third_party = brotli,certifi,chardet,cryptography,hstspreload,httpcore,pytest,rfc3986,setuptools,sniffio,trio,trustme,uvicorn15line_length = 8816multi_line_output = 31718[tool:pytest]
3 unmodified lines