-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make UploadedFile class serializable (#679)
* Make UploadedFile class serializable Since we're inheriting from io.Bytes, this made it harder to serialize since we couldn't just make it a data class. So to workaround this we added custom encoder/decoder logic to handle UploadedFile specifically. We also needed to add a custom operator for DeepDiff. For whatever reason deep diff wasn't diffing it correctly, so instead we just perform a basical equality check here. Demos/tests have been updated to illustrate that UploadedFile is now serializable.
- Loading branch information
1 parent
9f94610
commit a2681d5
Showing
12 changed files
with
282 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import io | ||
|
||
|
||
# Store this class in a separate file so we can more easily reference | ||
# in dataclass utils. | ||
class UploadedFile(io.BytesIO): | ||
"""Uploaded file contents and metadata.""" | ||
|
||
def __init__( | ||
self, | ||
contents: bytes = b"", | ||
*, | ||
name: str = "", | ||
size: int = 0, | ||
mime_type: str = "", | ||
): | ||
super().__init__(contents) | ||
self._name = name | ||
self._size = size | ||
self._mime_type = mime_type | ||
|
||
@property | ||
def name(self): | ||
return self._name | ||
|
||
@property | ||
def size(self): | ||
return self._size | ||
|
||
@property | ||
def mime_type(self): | ||
return self._mime_type | ||
|
||
def __eq__(self, other): | ||
if isinstance(other, UploadedFile): | ||
return (self.getvalue(), self.name, self.size, self.mime_type) == ( | ||
other.getvalue(), | ||
other.name, | ||
other.size, | ||
other.mime_type, | ||
) | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.