FileField writes into the Mapping given as a file tuple's 4th element #3789
Replies: 2 comments
|
Confirmed — all three behaviors reproduce on current The mechanics: the local if content_type is not None and not has_content_type_header:
headers["Content-Type"] = content_typeMeanwhile On (3), one addition to the report: the later files don't just lose their own content type — they silently render the first file's one: After the first field writes Workaround until this is fixed: give each file its own mapping ( Minimal fix suggestion: |
|
"Hi! I've implemented and tested the headers = dict(file_headers) fix locally along with the 3 regression tests (covering MappingProxyType, non-mutation of caller dict, and multi-file shared headers). All tests pass with full coverage and zero side-effects. I'm ready to open a PR for this — should I go ahead?" |
Uh oh!
There was an error while loading. Please reload this page.
FileTypesdeclares the 4th element of a file tuple asMapping[str, str]:but
FileField.__init__binds that element straight to its localheadersand then writes into it:Mappinghas no__setitem__, and the mapping belongs to the caller. Three things follow, all reproduced onmaster(b5addb6):1. A real
Mappingis rejected, though the annotation permits it.2. A caller's dict is written into.
3. Reusing one headers mapping across files sends the wrong content type. This is the one that bites silently. After the first file the mapping carries a
Content-Type, sohas_content_type_headeris true for every later file and their own content types are skipped:With three independent dicts the same upload is correct, so nothing about the files themselves is at fault.
The 3-tuple branch a few lines above already starts from a fresh
dict, so this looks like the 4-tuple branch simply not matching it rather than a deliberate choice.Fix would be one line —
headers = dict(file_headers)— and it also lets the# type: ignoreon that unpack go away, since the tuple element stops being assigned to adict-annotated name. I have that plus three regression tests ready if you would like a PR: the full suite gives the same 28 failures asmasteron this machine (network/server tests plus one Windows-specific one intest_multipart.py), with the three new tests passing.All reactions