Repository: apache/arrow
File: python/pyarrow/tests/test_extension_type.py (Python)

Code before:
    assert arr.null_count == 1
    for i, u in enumerate(uuids):
        assert arr[i].as_py() == u

    # Test type inference for arrays
    arr = pa.array(uuids)
    assert arr.type == pa.uuid()
    for i, u in enumerate(uuids):
        assert arr[i].as_py() == u


@pytest.mark.parametrize("bytes_value,exc_type,match", [
    (b"0123456789abcde", pa.ArrowInvalid, "expected to be length 16 was 15"),
    (
        "0123456789abcdef", TypeError,
        "Expected uuid.UUID.bytes to return bytes, got 'str'"
    ),
    (None, TypeError, "Expected uuid.UUID.bytes to return bytes, got 'NoneType'"),
])
def test_uuid_bytes_property_not_bytes(bytes_value, exc_type, match):
    import uuid

    class BadUuid(uuid.UUID):
        @property
        def bytes(self):
            return bytes_value

    bad = BadUuid(uuid.uuid4().hex)
    with pytest.raises(exc_type, match=match):
        pa.array([bad], type=pa.uuid())
    with pytest.raises(exc_type, match=match):
        pa.scalar(bad, type=pa.uuid())


def test_uuid_bytes_property_raises():
    import uuid

    class BadUuid(uuid.UUID):
        @property
        def bytes(self):
            raise RuntimeError("broken")

    bad = BadUuid(uuid.uuid4().hex)
    with pytest.raises(RuntimeError, match="broken"):
        pa.array([bad], type=pa.uuid())


def test_tensor_type():
    tensor_type = pa.fixed_shape_tensor(pa.int8(), [2, 3])
    assert tensor_type.extension_name == "arrow.fixed_shape_tensor"
    assert tensor_type.storage_type == pa.list_(pa.int8(), 6)

Review comment (suggestion):
Perhaps overly careful:
```suggestion
    with pytest.raises(exc_type, match=match):
        pa.array([bad], type=pa.uuid())
    with pytest.raises(exc_type, match=match):
        pa.scalar(bad, type=pa.uuid())
    with pytest.raises(exc_type, match=match):
        pa.array([bad])
    with pytest.raises(exc_type, match=match):
        pa.scalar(bad)
```

Code after:
    # Test type inference for arrays
    arr = pa.array(uuids)
    assert arr.type == pa.uuid()
    for i, u in enumerate(uuids):
        assert arr[i].as_py() == u


@pytest.mark.parametrize("bytes_value,exc_type,match", [
    (b"0123456789abcde", pa.ArrowInvalid, "expected to be length 16 was 15"),
    (
        "0123456789abcdef", TypeError,
        "Expected uuid.UUID.bytes to return bytes, got 'str'"
    ),
    (None, TypeError, "Expected uuid.UUID.bytes to return bytes, got 'NoneType'"),
])
def test_uuid_bytes_property_not_bytes(bytes_value, exc_type, match):
    class BadUuid(UUID):
        @property
        def bytes(self):
            return bytes_value

    bad = BadUuid(uuid4().hex)
    with pytest.raises(exc_type, match=match):
        pa.array([bad], type=pa.uuid())
    with pytest.raises(exc_type, match=match):
        pa.scalar(bad, type=pa.uuid())
    with pytest.raises(exc_type, match=match):
        pa.array([bad])
    with pytest.raises(exc_type, match=match):
        pa.scalar(bad)


def test_uuid_bytes_property_raises():
    class BadUuid(UUID):
        @property
        def bytes(self):
            raise RuntimeError("broken")

    bad = BadUuid(uuid4().hex)
    with pytest.raises(RuntimeError, match="broken"):
        pa.array([bad], type=pa.uuid())
    with pytest.raises(RuntimeError, match="broken"):
        pa.scalar(bad, type=pa.uuid())
    with pytest.raises(RuntimeError, match="broken"):
        pa.array([bad])
    with pytest.raises(RuntimeError, match="broken"):
        pa.scalar(bad)


def test_tensor_type():
    tensor_type = pa.fixed_shape_tensor(pa.int8(), [2, 3])