Skip to content

Conversation

@SteffanWullems
Copy link

Changes proposed in this PR include:

This update introduces support for additional targets with read/write I/O capabilities,
including local storage, remote file systems, I/O buffers, and similar resources.

For example (using https://pypi.org/project/smart-open/):
`from smart_open import open as open_s3_file

TARGET_FILENAME="results.mgp"
S3_PATH = f"s3://some-aws-bucket/{TARGET_FILENAME}"

with open_s3_file( S3_PATH, "wb", transport_params={"client": s3client}) as fp:
msgpack_serialize_to_fileobj(fp, results)

with open_s3_file( S3_PATH, "rb", transport_params={"client": s3client}) as fp:
loaded_s3 = msgpack_deserialize_from_fileobj(fp)`

Signed-off-by: Steffan Wullems <129064935+SteffanWullems@users.noreply.github.com>
import warnings
from pathlib import Path
from typing import cast as cast_type
from typing import IO as mp_IO, Any as mp_Any, cast as cast_type
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does the prefix mp mean? Since you're naming it ByteIO later on, why not alias as ByteIO here? or just keep it as IO

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reason is IO and Any can clash

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also IO[Any] allows all IO streams

Copy link
Member

@mgovers mgovers Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but what does mp_ mean?

if name clashes are the only reason, please instead use module-private prefixes

Suggested change
from typing import IO as mp_IO, Any as mp_Any, cast as cast_type
from typing import IO as _IO, Any as _Any, cast as cast_type

@mgovers mgovers added the feature New feature or request label Dec 19, 2025
Comment on lines 803 to 807
def test_messagepack_round_trip_to_io(serialized_data):
data = to_msgpack(serialized_data)
input_data: Dataset = msgpack_deserialize(data)

io_buffer_data = BytesIO()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since the input type is IO[Any], we should test both BytesIO and TextIO.

EDIT: I implemented this and it fails (exception is raised during deserialization)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is if you set BytesIO it will not work for all bytes based streams.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd still prefer that over things that we say we support but in reality don't

@figueroa1395
Copy link
Member

@SteffanWullems Can you please sign your commits? I'll make a full review first thing on Monday morning.

Copy link
Member

@figueroa1395 figueroa1395 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, just some minor comments.

@@ -1,3 +1,4 @@
# SPDX-FileCopyrightText: 2025 Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this line? Where did it come from?



def msgpack_deserialize_from_stream(
stream: IO[bytes],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Here and elsewhere

Suggested change
stream: IO[bytes],
stream: BinaryIO,

Comment on lines +495 to +496
if stream is IO[Any]:
raise TypeError("Expected a stream.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is broken and does nothing. typing.IO is to be used only for type hinting and this check should always be false. I think it can be removed.

Comment on lines +521 to +522
if stream is IO[Any]:
raise TypeError("Expected a stream.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

Signed-off-by: Steffan <steffan.wullems@alliander.com>
Signed-off-by: Steffan <steffan.wullems@alliander.com>
Signed-off-by: Steffan <steffan.wullems@alliander.com>
Signed-off-by: Steffan <steffan.wullems@alliander.com>
@SteffanWullems SteffanWullems force-pushed the feature-for-more-IO-message-pack-targets branch from e4ce485 to 58b6896 Compare December 22, 2025 11:26
@nitbharambe
Copy link
Member

nitbharambe commented Dec 22, 2025

LGTM after resolution of comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants