Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions python/python/tests/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ def test_bf16_value():
assert not any(comparison for comparison in should_be_false)


def test_bf16_from_bytes_roundtrip():
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)


def test_bf16_repr():
data = [1.1, None, 3.4]
arr = bfloat16_array(data)
Expand Down
4 changes: 2 additions & 2 deletions python/src/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ impl BFloat16 {
#[classmethod]
fn from_bytes(_cls: &Bound<'_, PyType>, bytes: &[u8]) -> PyResult<Self> {
if bytes.len() != 2 {
PyValueError::new_err(format!(
return Err(PyValueError::new_err(format!(
"BFloat16::from_bytes: expected 2 bytes, got {}",
bytes.len()
));
)));
}
Ok(Self(bf16::from_bits(u16::from_ne_bytes([
bytes[0], bytes[1],
Expand Down
Loading