Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing stream item in Python NDJsonProtocolReader when previous stream is empty #143

Closed
naegelejd opened this issue Mar 29, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@naegelejd
Copy link
Contributor

Using the Streams protocol:

Streams: !protocol
sequence:
intData: !stream
items: int
optionalIntData: !stream
items: int?
recordWithOptionalVectorData: !stream
items: RecordWithOptionalVector
fixedVector: !stream
items: int*3

When serialized, if one of these streams is empty and followed by a non-empty stream, the first element of the non-empty stream is accidentally discarded.

Example:

import test_model as tm

json = "test.json"
with tm.NDJsonStreamsWriter(json) as w:
    w.write_int_data(range(0))
    w.write_optional_int_data([1, 2, None, 4, 5, None, 7, 8, 9, 10])
    w.write_record_with_optional_vector_data([])
    w.write_fixed_vector(([1, 2, 3] for _ in range(4)))

with tm.NDJsonStreamsReader(json) as r:
    assert len(list(r.read_int_data())) == 0
    assert len(list(r.read_optional_int_data())) == 10
    assert len(list(r.read_record_with_optional_vector_data())) == 0
    assert (huh := len(list(r.read_fixed_vector()))) == 4, huh

Fails on the last line because it only reads 3 fixed_vectors, instead of the expected 4.

This was exposed by the same test described in #142 (mixing empty and non-empty streams in roundtrip test).

@naegelejd naegelejd self-assigned this Mar 29, 2024
naegelejd added a commit that referenced this issue Mar 29, 2024
naegelejd added a commit that referenced this issue Apr 2, 2024
Fix bugs that manifest when reading and writing empty protocol streams in Python.
@naegelejd naegelejd added the bug Something isn't working label Apr 2, 2024
@naegelejd
Copy link
Contributor Author

Resolved in #144

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant