fix: allow uploading a file from an in-memory stream - #148
Open
LukasGold wants to merge 1 commit into
Open
Conversation
Closes #140. - express.py: replace the unreachable isinstance(source, IO) check with a duck-typed one; typing.IO is not runtime-checkable - InMemoryController: drop the __init__ that assigned stream before the model was initialised and overwrote a caller-supplied stream - default the stream to BytesIO, matching the byte-oriented get/put - declare IO in the upload_file / osw_upload_file signatures
Contributor
Release previewMerging this PR would release v2.0.3 (current: Changelog preview (truncated)## v2.0.3 (2026-08-28)
### Bug Fixes
- Allow uploading a file from an in-memory stream
([`64d99d6`](https://github.com/OpenSemanticLab/osw-python/commit/64d99d66ea45bc56bdaa6d12f2502df0bbbaf88c))
### Testing
- Rename oold.py to oold_test.py so its tests are collected
([`20072a9`](https://github.com/OpenSemanticLab/osw-python/commit/20072a9249cd97126a222c62a70f84e0433343ef))
Preview via python-semantic-release and conventional commits. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #140.
Uploading from an open file-like object was documented but never worked. Two
independent bugs, both on the path from
osw_upload_file(source=<stream>)toInMemoryController.Changes
src/osw/express.py: replace theisinstance(source, IO)branch with aduck-typed
hasattr(source, "read")check.typing.IOis notruntime-checkable, so that
isinstancewas alwaysFalseand a stream sourcesilently fell through to the path-handling branch.
src/osw/controller/file/memory.py: drop the__init__that assignedself.stream = StringIO()beforesuper().__init__(**kwargs). It ran beforethe pydantic model was initialised and overwrote any caller-supplied stream.
BytesIOviaField(default_factory=BytesIO), matchingthe byte-oriented
get/put(shutil.copyfileobj).StringIOwas atext/bytes mismatch with both.
IOin theOswExpress.upload_file/osw_upload_filesignatures anddocstrings, which previously accepted only
strandPath.Verification
Both bugs were reproduced against the unfixed code before fixing: the
isinstancebug fails 1 of the new tests, the__init__bug fails 4.tests/test_in_memory_upload.pyadds 5 tests covering a caller-supplied streamsurviving construction, the empty-buffer default,
putcopying into the stream,a stream being wrapped in an
InMemoryControlleron upload, and a non-file-likesource being rejected.
Full unit suite: 282 passed, 1 skipped.
@SimonStier for review.