Skip to content

Commit

Permalink
Remove content_type from Field
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnstrk committed Dec 11, 2024
1 parent f1e28d6 commit 0fc5e2d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 32 deletions.
16 changes: 5 additions & 11 deletions python_multipart/multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def finalize(self) -> None: ...
def close(self) -> None: ...

class FieldProtocol(_FormProtocol, Protocol):
def __init__(self, name: bytes, content_type: str | None = None) -> None: ...
def __init__(self, name: bytes) -> None: ...

def set_none(self) -> None: ...

Expand Down Expand Up @@ -226,10 +226,9 @@ class Field:
content_type: The value of the Content-Type header for this field.
"""

def __init__(self, name: bytes, content_type: str | None = None) -> None:
def __init__(self, name: bytes) -> None:
self._name = name
self._value: list[bytes] = []
self._content_type = content_type

# We cache the joined version of _value for speed.
self._cache = _missing
Expand Down Expand Up @@ -321,11 +320,6 @@ def value(self) -> bytes | None:
assert isinstance(self._cache, bytes) or self._cache is None
return self._cache

@property
def content_type(self) -> str | None:
"""This property returns the content_type value of the field."""
return self._content_type

def __eq__(self, other: object) -> bool:
if isinstance(other, Field):
return self.field_name == other.field_name and self.value == other.value
Expand Down Expand Up @@ -1601,7 +1595,7 @@ def on_field_name(data: bytes, start: int, end: int) -> None:
def on_field_data(data: bytes, start: int, end: int) -> None:
nonlocal f
if f is None:
f = FieldClass(b"".join(name_buffer), content_type=None)
f = FieldClass(b"".join(name_buffer))
del name_buffer[:]
f.write(data[start:end])

Expand All @@ -1611,7 +1605,7 @@ def on_field_end() -> None:
if f is None:
# If we get here, it's because there was no field data.
# We create a field, set it to None, and then continue.
f = FieldClass(b"".join(name_buffer), content_type=None)
f = FieldClass(b"".join(name_buffer))
del name_buffer[:]
f.set_none()

Expand Down Expand Up @@ -1700,7 +1694,7 @@ def on_headers_finished() -> None:
content_type_b = headers.get("content-type")
content_type = content_type_b.decode("latin-1") if content_type_b is not None else None
if file_name is None:
f_multi = FieldClass(field_name, content_type=content_type)
f_multi = FieldClass(field_name)
else:
f_multi = FileClass(
file_name, field_name, config=cast("FileConfig", self.config), content_type=content_type
Expand Down
21 changes: 0 additions & 21 deletions tests/test_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,27 +971,6 @@ def test_file_content_type_header(self) -> None:
actual_content_type = self.files[0].content_type
self.assertEqual(actual_content_type, expected_content_type)

def test_field_content_type_header(self) -> None:
"""
This test checks content-tpye for a field part are read and passed.
"""
# Load test data.
test_file = "single_field.http"
with open(os.path.join(http_tests_dir, test_file), "rb") as f:
test_data = f.read()

expected_content_type = None

# Create form parser.
self.make(boundary="----WebKitFormBoundaryTkr3kCBQlBe1nrhc")
self.f.write(test_data)
self.f.finalize()

# Assert that our field is here.
self.assertEqual(1, len(self.fields))
actual_content_type = self.fields[0].content_type
self.assertEqual(actual_content_type, expected_content_type)

def test_request_body_fuzz(self) -> None:
"""
This test randomly fuzzes the request body to ensure that no strange
Expand Down

0 comments on commit 0fc5e2d

Please sign in to comment.