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

Code before:
        "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)
    assert tensor_type.shape == [2, 3]
    assert tensor_type.dim_names is None
    assert tensor_type.permutation is None

    tensor_type = pa.fixed_shape_tensor(pa.float64(), [2, 2, 3],
                                        permutation=[0, 2, 1])
    assert tensor_type.extension_name == "arrow.fixed_shape_tensor"
    assert tensor_type.storage_type == pa.list_(pa.float64(), 12)
    assert tensor_type.shape == [2, 2, 3]
    assert tensor_type.dim_names is None
    assert tensor_type.permutation == [0, 2, 1]

    tensor_type = pa.fixed_shape_tensor(pa.bool_(), [2, 2, 3],
                                        dim_names=['C', 'H', 'W'])
    assert tensor_type.extension_name == "arrow.fixed_shape_tensor"

Review comment (bug):
Similar as above:
```suggestion
    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)
```

Code after:
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])
    assert tensor_type.extension_name == "arrow.fixed_shape_tensor"
    assert tensor_type.storage_type == pa.list_(pa.int8(), 6)
    assert tensor_type.shape == [2, 3]
    assert tensor_type.dim_names is None
    assert tensor_type.permutation is None

    tensor_type = pa.fixed_shape_tensor(pa.float64(), [2, 2, 3],
                                        permutation=[0, 2, 1])
    assert tensor_type.extension_name == "arrow.fixed_shape_tensor"
    assert tensor_type.storage_type == pa.list_(pa.float64(), 12)
    assert tensor_type.shape == [2, 2, 3]
    assert tensor_type.dim_names is None
    assert tensor_type.permutation == [0, 2, 1]

    tensor_type = pa.fixed_shape_tensor(pa.bool_(), [2, 2, 3],