Skip to content

Commit

Permalink
Enforce ruff/flake8-pytest-style rules (PT) (#2236)
Browse files Browse the repository at this point in the history
* Apply ruff/flake8-pytest-style rule PT018

PT018 Assertion should be broken down into multiple parts

* Apply ruff/flake8-pytest-style rule PT007

PT007 Wrong values type in `@pytest.mark.parametrize` expected `list` of `tuple`

* Apply ruff/flake8-pytest-style rule PT006

PT006 Wrong type passed to first argument of
      `@pytest.mark.parametrize`; expected `tuple`

* Apply ruff/flake8-pytest-style rule PT003

PT003 `scope='function'` is implied in `@pytest.fixture()`

* Apply ruff/flake8-pytest-style rule PT001

PT001 Use `@pytest.fixture` over `@pytest.fixture()`

* Enforce ruff/flake8-pytest-style rules (PT)
  • Loading branch information
DimitriPapadopoulos authored Sep 25, 2024
1 parent c06fa23 commit 9f825e1
Show file tree
Hide file tree
Showing 20 changed files with 84 additions and 79 deletions.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ extend-select = [
"I", # isort
"ISC", # flake8-implicit-str-concat
"PGH", # pygrep-hooks
"PT", # flake8-pytest-style
"PYI", # flake8-pyi
"RSE", # flake8-raise
"RET", # flake8-return
Expand All @@ -221,6 +222,9 @@ extend-select = [
"UP", # pyupgrade
]
ignore = [
"PT004", # deprecated
"PT011", # TODO: apply this rule
"PT012", # TODO: apply this rule
"PYI013",
"RET505",
"RET506",
Expand Down
10 changes: 5 additions & 5 deletions src/zarr/testing/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ def get(self, store: S, key: str) -> Buffer:

raise NotImplementedError

@pytest.fixture(scope="function")
@pytest.fixture
def store_kwargs(self) -> dict[str, Any]:
return {"mode": "r+"}

@pytest.fixture(scope="function")
@pytest.fixture
async def store(self, store_kwargs: dict[str, Any]) -> Store:
return await self.store_cls.open(**store_kwargs)

Expand Down Expand Up @@ -97,7 +97,7 @@ def test_store_supports_listing(self, store: S) -> None:

@pytest.mark.parametrize("key", ["c/0", "foo/c/0.0", "foo/0/0"])
@pytest.mark.parametrize("data", [b"\x01\x02\x03\x04", b""])
@pytest.mark.parametrize("byte_range", (None, (0, None), (1, None), (1, 2), (None, 1)))
@pytest.mark.parametrize("byte_range", [None, (0, None), (1, None), (1, 2), (None, 1)])
async def test_get(
self, store: S, key: str, data: bytes, byte_range: None | tuple[int | None, int | None]
) -> None:
Expand Down Expand Up @@ -137,12 +137,12 @@ async def test_set_many(self, store: S) -> None:

@pytest.mark.parametrize(
"key_ranges",
(
[
[],
[("zarr.json", (0, 1))],
[("c/0", (0, 1)), ("zarr.json", (0, None))],
[("c/0/0", (0, 1)), ("c/0/1", (None, 2)), ("c/0/2", (0, 3))],
),
],
)
async def test_get_partial_values(
self, store: S, key_ranges: list[tuple[str, tuple[int | None, int | None]]]
Expand Down
12 changes: 6 additions & 6 deletions tests/v3/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,27 @@ async def store_path(tmpdir: LEGACY_PATH) -> StorePath:
return StorePath(store)


@pytest.fixture(scope="function")
@pytest.fixture
async def local_store(tmpdir: LEGACY_PATH) -> LocalStore:
return await LocalStore.open(str(tmpdir), mode="w")


@pytest.fixture(scope="function")
@pytest.fixture
async def remote_store(url: str) -> RemoteStore:
return await RemoteStore.open(url, mode="w")


@pytest.fixture(scope="function")
@pytest.fixture
async def memory_store() -> MemoryStore:
return await MemoryStore.open(mode="w")


@pytest.fixture(scope="function")
@pytest.fixture
async def zip_store(tmpdir: LEGACY_PATH) -> ZipStore:
return await ZipStore.open(str(tmpdir / "zarr.zip"), mode="w")


@pytest.fixture(scope="function")
@pytest.fixture
async def store(request: pytest.FixtureRequest, tmpdir: LEGACY_PATH) -> Store:
param = request.param
return await parse_store(param, str(tmpdir))
Expand All @@ -82,7 +82,7 @@ class AsyncGroupRequest:
attributes: dict[str, Any] = field(default_factory=dict)


@pytest.fixture(scope="function")
@pytest.fixture
async def async_group(request: pytest.FixtureRequest, tmpdir: LEGACY_PATH) -> AsyncGroup:
param: AsyncGroupRequest = request.param

Expand Down
22 changes: 11 additions & 11 deletions tests/v3/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from zarr.store.common import StorePath


@pytest.mark.parametrize("store", ("local", "memory", "zip"), indirect=["store"])
@pytest.mark.parametrize("zarr_format", (2, 3))
@pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"])
@pytest.mark.parametrize("zarr_format", [2, 3])
@pytest.mark.parametrize("exists_ok", [True, False])
@pytest.mark.parametrize("extant_node", ["array", "group"])
def test_array_creation_existing_node(
Expand Down Expand Up @@ -61,8 +61,8 @@ def test_array_creation_existing_node(
)


@pytest.mark.parametrize("store", ("local", "memory", "zip"), indirect=["store"])
@pytest.mark.parametrize("zarr_format", (2, 3))
@pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"])
@pytest.mark.parametrize("zarr_format", [2, 3])
def test_array_name_properties_no_group(
store: LocalStore | MemoryStore, zarr_format: ZarrFormat
) -> None:
Expand All @@ -72,8 +72,8 @@ def test_array_name_properties_no_group(
assert arr.basename is None


@pytest.mark.parametrize("store", ("local", "memory", "zip"), indirect=["store"])
@pytest.mark.parametrize("zarr_format", (2, 3))
@pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"])
@pytest.mark.parametrize("zarr_format", [2, 3])
def test_array_name_properties_with_group(
store: LocalStore | MemoryStore, zarr_format: ZarrFormat
) -> None:
Expand Down Expand Up @@ -123,7 +123,7 @@ def test_array_v3_fill_value_default(

@pytest.mark.parametrize("store", ["memory"], indirect=True)
@pytest.mark.parametrize(
"dtype_str,fill_value",
("dtype_str", "fill_value"),
[("bool", True), ("uint8", 99), ("float32", -99.9), ("complex64", 3 + 4j)],
)
def test_array_v3_fill_value(store: MemoryStore, fill_value: int, dtype_str: str) -> None:
Expand Down Expand Up @@ -201,8 +201,8 @@ async def test_array_v3_nan_fill_value(store: MemoryStore) -> None:
assert len([a async for a in store.list_prefix("/")]) == 0


@pytest.mark.parametrize("store", ("local",), indirect=["store"])
@pytest.mark.parametrize("zarr_format", (2, 3))
@pytest.mark.parametrize("store", ["local"], indirect=["store"])
@pytest.mark.parametrize("zarr_format", [2, 3])
async def test_serializable_async_array(
store: LocalStore | MemoryStore, zarr_format: ZarrFormat
) -> None:
Expand All @@ -219,8 +219,8 @@ async def test_serializable_async_array(
# TODO: uncomment the parts of this test that will be impacted by the config/prototype changes in flight


@pytest.mark.parametrize("store", ("local",), indirect=["store"])
@pytest.mark.parametrize("zarr_format", (2, 3))
@pytest.mark.parametrize("store", ["local"], indirect=["store"])
@pytest.mark.parametrize("zarr_format", [2, 3])
def test_serializable_sync_array(store: LocalStore, zarr_format: ZarrFormat) -> None:
expected = Array.create(
store=store, shape=(100,), chunks=(10,), zarr_format=zarr_format, dtype="i4"
Expand Down
4 changes: 2 additions & 2 deletions tests/v3/test_chunk_grids.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@


@pytest.mark.parametrize(
"shape", ((0,), (0,) * 2, (1, 2, 0, 4, 5), (10, 0), (10,), (100,) * 3, (1000000,), (10000,) * 2)
"shape", [(0,), (0,) * 2, (1, 2, 0, 4, 5), (10, 0), (10,), (100,) * 3, (1000000,), (10000,) * 2]
)
@pytest.mark.parametrize("itemsize", (1, 2, 4))
@pytest.mark.parametrize("itemsize", [1, 2, 4])
def test_guess_chunks(shape: tuple[int, ...], itemsize: int) -> None:
chunks = _guess_chunks(shape, itemsize)
chunk_size = np.prod(chunks) * itemsize
Expand Down
2 changes: 1 addition & 1 deletion tests/v3/test_codec_entrypoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
here = os.path.abspath(os.path.dirname(__file__))


@pytest.fixture()
@pytest.fixture
def set_path() -> Generator[None, None, None]:
sys.path.append(here)
zarr.registry._collect_entrypoints()
Expand Down
2 changes: 1 addition & 1 deletion tests/v3/test_codecs/test_blosc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from zarr.store.common import StorePath


@pytest.mark.parametrize("store", ("local", "memory"), indirect=["store"])
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
@pytest.mark.parametrize("dtype", ["uint8", "uint16"])
async def test_blosc_evolve(store: Store, dtype: str) -> None:
typesize = np.dtype(dtype).itemsize
Expand Down
16 changes: 8 additions & 8 deletions tests/v3/test_codecs/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_sharding_pickle() -> None:
pass


@pytest.mark.parametrize("store", ("local", "memory"), indirect=["store"])
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
@pytest.mark.parametrize("input_order", ["F", "C"])
@pytest.mark.parametrize("store_order", ["F", "C"])
@pytest.mark.parametrize("runtime_write_order", ["F", "C"])
Expand Down Expand Up @@ -117,7 +117,7 @@ async def test_order(
assert read_data.flags["C_CONTIGUOUS"]


@pytest.mark.parametrize("store", ("local", "memory"), indirect=["store"])
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
@pytest.mark.parametrize("input_order", ["F", "C"])
@pytest.mark.parametrize("runtime_write_order", ["F", "C"])
@pytest.mark.parametrize("runtime_read_order", ["F", "C"])
Expand Down Expand Up @@ -159,7 +159,7 @@ def test_order_implicit(
assert read_data.flags["C_CONTIGUOUS"]


@pytest.mark.parametrize("store", ("local", "memory"), indirect=["store"])
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
def test_open(store: Store) -> None:
spath = StorePath(store)
a = Array.create(
Expand Down Expand Up @@ -205,7 +205,7 @@ def test_morton() -> None:
]


@pytest.mark.parametrize("store", ("local", "memory"), indirect=["store"])
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
def test_write_partial_chunks(store: Store) -> None:
data = np.arange(0, 256, dtype="uint16").reshape((16, 16))
spath = StorePath(store)
Expand All @@ -220,7 +220,7 @@ def test_write_partial_chunks(store: Store) -> None:
assert np.array_equal(a[0:16, 0:16], data)


@pytest.mark.parametrize("store", ("local", "memory"), indirect=["store"])
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
async def test_delete_empty_chunks(store: Store) -> None:
data = np.ones((16, 16))
path = "delete_empty_chunks"
Expand All @@ -238,7 +238,7 @@ async def test_delete_empty_chunks(store: Store) -> None:
assert await store.get(f"{path}/c0/0", prototype=default_buffer_prototype()) is None


@pytest.mark.parametrize("store", ("local", "memory"), indirect=["store"])
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
async def test_dimension_names(store: Store) -> None:
data = np.arange(0, 256, dtype="uint16").reshape((16, 16))
path = "dimension_names"
Expand Down Expand Up @@ -272,7 +272,7 @@ async def test_dimension_names(store: Store) -> None:
assert "dimension_names" not in json.loads(zarr_json_buffer.to_bytes())


@pytest.mark.parametrize("store", ("local", "memory"), indirect=["store"])
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
def test_invalid_metadata(store: Store) -> None:
spath = StorePath(store, "invalid_metadata")
with pytest.raises(ValueError):
Expand Down Expand Up @@ -360,7 +360,7 @@ def test_invalid_metadata(store: Store) -> None:
)


@pytest.mark.parametrize("store", ("local", "memory"), indirect=["store"])
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
async def test_resize(store: Store) -> None:
data = np.zeros((16, 18), dtype="uint16")
path = "resize"
Expand Down
4 changes: 2 additions & 2 deletions tests/v3/test_codecs/test_endian.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .test_codecs import _AsyncArrayProxy


@pytest.mark.parametrize("store", ("local", "memory"), indirect=["store"])
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
@pytest.mark.parametrize("endian", ["big", "little"])
async def test_endian(store: Store, endian: Literal["big", "little"]) -> None:
data = np.arange(0, 256, dtype="uint16").reshape((16, 16))
Expand All @@ -32,7 +32,7 @@ async def test_endian(store: Store, endian: Literal["big", "little"]) -> None:
assert np.array_equal(data, readback_data)


@pytest.mark.parametrize("store", ("local", "memory"), indirect=["store"])
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
@pytest.mark.parametrize("dtype_input_endian", [">u2", "<u2"])
@pytest.mark.parametrize("dtype_store_endian", ["big", "little"])
async def test_endian_write(
Expand Down
2 changes: 1 addition & 1 deletion tests/v3/test_codecs/test_gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from zarr.store.common import StorePath


@pytest.mark.parametrize("store", ("local", "memory"), indirect=["store"])
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
def test_gzip(store: Store) -> None:
data = np.arange(0, 256, dtype="uint16").reshape((16, 16))

Expand Down
19 changes: 10 additions & 9 deletions tests/v3/test_codecs/test_sharding.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from .test_codecs import _AsyncArrayProxy, order_from_dim


@pytest.mark.parametrize("store", ("local", "memory", "zip"), indirect=["store"])
@pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"])
@pytest.mark.parametrize("index_location", ["start", "end"])
@pytest.mark.parametrize(
"array_fixture",
Expand Down Expand Up @@ -76,7 +76,7 @@ def test_sharding(


@pytest.mark.parametrize("index_location", ["start", "end"])
@pytest.mark.parametrize("store", ("local", "memory", "zip"), indirect=["store"])
@pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"])
@pytest.mark.parametrize(
"array_fixture",
[
Expand Down Expand Up @@ -126,7 +126,7 @@ def test_sharding_partial(
indirect=["array_fixture"],
)
@pytest.mark.parametrize("index_location", ["start", "end"])
@pytest.mark.parametrize("store", ("local", "memory", "zip"), indirect=["store"])
@pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"])
def test_sharding_partial_read(
store: Store, array_fixture: npt.NDArray[Any], index_location: ShardingCodecIndexLocation
) -> None:
Expand Down Expand Up @@ -163,7 +163,7 @@ def test_sharding_partial_read(
indirect=["array_fixture"],
)
@pytest.mark.parametrize("index_location", ["start", "end"])
@pytest.mark.parametrize("store", ("local", "memory", "zip"), indirect=["store"])
@pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"])
def test_sharding_partial_overwrite(
store: Store, array_fixture: npt.NDArray[Any], index_location: ShardingCodecIndexLocation
) -> None:
Expand Down Expand Up @@ -214,7 +214,7 @@ def test_sharding_partial_overwrite(
"inner_index_location",
["start", "end"],
)
@pytest.mark.parametrize("store", ("local", "memory", "zip"), indirect=["store"])
@pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"])
def test_nested_sharding(
store: Store,
array_fixture: npt.NDArray[Any],
Expand Down Expand Up @@ -247,7 +247,7 @@ def test_nested_sharding(
assert np.array_equal(data, read_data)


@pytest.mark.parametrize("store", ("local", "memory", "zip"), indirect=["store"])
@pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"])
def test_open_sharding(store: Store) -> None:
path = "open_sharding"
spath = StorePath(store, path)
Expand All @@ -272,7 +272,7 @@ def test_open_sharding(store: Store) -> None:
assert a.metadata == b.metadata


@pytest.mark.parametrize("store", ("local", "memory", "zip"), indirect=["store"])
@pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"])
def test_write_partial_sharded_chunks(store: Store) -> None:
data = np.arange(0, 16 * 16, dtype="uint16").reshape((16, 16))
spath = StorePath(store)
Expand All @@ -296,7 +296,7 @@ def test_write_partial_sharded_chunks(store: Store) -> None:
assert np.array_equal(a[0:16, 0:16], data)


@pytest.mark.parametrize("store", ("local", "memory", "zip"), indirect=["store"])
@pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"])
async def test_delete_empty_shards(store: Store) -> None:
if not store.supports_deletes:
pytest.skip("store does not support deletes")
Expand All @@ -323,7 +323,8 @@ async def test_delete_empty_shards(store: Store) -> None:
assert np.array_equal(data, await _AsyncArrayProxy(a)[:, :].get())
assert await store.get(f"{path}/c/1/0", prototype=default_buffer_prototype()) is None
chunk_bytes = await store.get(f"{path}/c/0/0", prototype=default_buffer_prototype())
assert chunk_bytes is not None and len(chunk_bytes) == 16 * 2 + 8 * 8 * 2 + 4
assert chunk_bytes is not None
assert len(chunk_bytes) == 16 * 2 + 8 * 8 * 2 + 4


def test_pickle() -> None:
Expand Down
6 changes: 3 additions & 3 deletions tests/v3/test_codecs/test_transpose.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@pytest.mark.parametrize("runtime_write_order", ["F", "C"])
@pytest.mark.parametrize("runtime_read_order", ["F", "C"])
@pytest.mark.parametrize("with_sharding", [True, False])
@pytest.mark.parametrize("store", ("local", "memory"), indirect=["store"])
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
async def test_transpose(
store: Store,
input_order: MemoryOrder,
Expand Down Expand Up @@ -69,7 +69,7 @@ async def test_transpose(
assert read_data.flags["C_CONTIGUOUS"]


@pytest.mark.parametrize("store", ("local", "memory"), indirect=["store"])
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
@pytest.mark.parametrize("order", [[1, 2, 0], [1, 2, 3, 0], [3, 2, 4, 0, 1]])
def test_transpose_non_self_inverse(store: Store, order: list[int]) -> None:
shape = [i + 3 for i in range(len(order))]
Expand All @@ -88,7 +88,7 @@ def test_transpose_non_self_inverse(store: Store, order: list[int]) -> None:
assert np.array_equal(data, read_data)


@pytest.mark.parametrize("store", ("local", "memory"), indirect=["store"])
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
def test_transpose_invalid(
store: Store,
) -> None:
Expand Down
Loading

0 comments on commit 9f825e1

Please sign in to comment.