Skip to content

fix(python): validate BFloat16.from_bytes length#6998

Merged
yanghua merged 3 commits into
lance-format:mainfrom
ddupg:fix/ddu-56
May 30, 2026
Merged

fix(python): validate BFloat16.from_bytes length#6998
yanghua merged 3 commits into
lance-format:mainfrom
ddupg:fix/ddu-56

Conversation

@ddupg
Copy link
Copy Markdown
Contributor

@ddupg ddupg commented May 29, 2026

Small fix for a missing return when BFloat16.from_bytes rejects non-2-byte input.

@github-actions github-actions Bot added bug Something isn't working python labels May 29, 2026
@ddupg ddupg marked this pull request as ready for review May 29, 2026 14:04
Copy link
Copy Markdown

@claude claude Bot left a comment

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

Copy link
Copy Markdown
Collaborator

@yanghua yanghua left a comment

Choose a reason for hiding this comment

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

If we can add some tests, e.g.

import pytest
from lance.arrow import BFloat16


def test_bf16_from_bytes_roundtrip():
    val = BFloat16(1.5)
    assert BFloat16.from_bytes(b"\xc0\x3f") == BFloat16(1.5)


@pytest.mark.parametrize("bad", [b"", b"\x00", b"\x00\x00\x00", b"\x00" * 4])
def test_bf16_from_bytes_invalid_length_raises(bad):
    with pytest.raises(ValueError, match="expected 2 bytes"):
        BFloat16.from_bytes(bad)

That would be better.

@ddupg
Copy link
Copy Markdown
Contributor Author

ddupg commented May 30, 2026

If we can add some tests, e.g.

import pytest
from lance.arrow import BFloat16


def test_bf16_from_bytes_roundtrip():
    val = BFloat16(1.5)
    assert BFloat16.from_bytes(b"\xc0\x3f") == BFloat16(1.5)


@pytest.mark.parametrize("bad", [b"", b"\x00", b"\x00\x00\x00", b"\x00" * 4])
def test_bf16_from_bytes_invalid_length_raises(bad):
    with pytest.raises(ValueError, match="expected 2 bytes"):
        BFloat16.from_bytes(bad)

That would be better.

Thanks for reviewing, added tests.

Copy link
Copy Markdown
Collaborator

@yanghua yanghua left a comment

Choose a reason for hiding this comment

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

LGTM

@yanghua yanghua merged commit a73b8e0 into lance-format:main May 30, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants