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
Provenance
- Commit
9f58afd- Tree
fbc931b- Author
Florimond Manca <florimond.manca@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
f1b3d74
2 files changed+21 added−18 removed
modified:httpx/_content_streams.py+20 added-17 removed
6 unmodified lines7 8 import httpcore9 10 from ._exceptions import StreamConsumed11-from ._types import RequestData, RequestFiles11+from ._types import FileContent, FileTypes, RequestData, RequestFiles12 from ._utils import (13 format_form_param,14 guess_content_type,15 peek_filelike_length,210 unmodified lines226 """227 A single file field item, within a multipart form field.228 """229 230- 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)232+233+ fileobj: FileContent234+235+ 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)245 246+ self.filename = filename247+ self.file = fileobj248+ self.content_type = content_type249+250 def get_length(self) -> int:251 headers = self.render_headers()252 253 if isinstance(self.file, (str, bytes)):52 unmodified lines306 yield self.render_headers()307 yield from self.render_data()308 309 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 lines318 )319 self.fields = list(self._iter_fields(data, files))320 321 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 linesmodified:setup.cfg+1 added-1 removed
9 unmodified lines10 combine_as_imports = True11 force_grid_wrap = 012 include_trailing_comma = True13 known_first_party = httpx,tests14-known_third_party = brotli,certifi,chardet,cryptography,hstspreload,httpcore,pytest,rfc3986,setuptools,sniffio,trio,trustme,urllib3,uvicorn14+known_third_party = brotli,certifi,chardet,cryptography,hstspreload,httpcore,pytest,rfc3986,setuptools,sniffio,trio,trustme,uvicorn15 line_length = 8816 multi_line_output = 317 18 [tool:pytest]3 unmodified lines