Skip to content

Commit 9f825e1

Browse files
Enforce ruff/flake8-pytest-style rules (PT) (zarr-developers#2236)
* 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)
1 parent c06fa23 commit 9f825e1

20 files changed

+84
-79
lines changed

pyproject.toml

+4
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ extend-select = [
212212
"I", # isort
213213
"ISC", # flake8-implicit-str-concat
214214
"PGH", # pygrep-hooks
215+
"PT", # flake8-pytest-style
215216
"PYI", # flake8-pyi
216217
"RSE", # flake8-raise
217218
"RET", # flake8-return
@@ -221,6 +222,9 @@ extend-select = [
221222
"UP", # pyupgrade
222223
]
223224
ignore = [
225+
"PT004", # deprecated
226+
"PT011", # TODO: apply this rule
227+
"PT012", # TODO: apply this rule
224228
"PYI013",
225229
"RET505",
226230
"RET506",

src/zarr/testing/store.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ def get(self, store: S, key: str) -> Buffer:
3737

3838
raise NotImplementedError
3939

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

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

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

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

138138
@pytest.mark.parametrize(
139139
"key_ranges",
140-
(
140+
[
141141
[],
142142
[("zarr.json", (0, 1))],
143143
[("c/0", (0, 1)), ("zarr.json", (0, None))],
144144
[("c/0/0", (0, 1)), ("c/0/1", (None, 2)), ("c/0/2", (0, 3))],
145-
),
145+
],
146146
)
147147
async def test_get_partial_values(
148148
self, store: S, key_ranges: list[tuple[str, tuple[int | None, int | None]]]

tests/v3/conftest.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,27 @@ async def store_path(tmpdir: LEGACY_PATH) -> StorePath:
4949
return StorePath(store)
5050

5151

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

5656

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

6161

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

6666

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

7171

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

8484

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

tests/v3/test_array.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
from zarr.store.common import StorePath
1313

1414

15-
@pytest.mark.parametrize("store", ("local", "memory", "zip"), indirect=["store"])
16-
@pytest.mark.parametrize("zarr_format", (2, 3))
15+
@pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"])
16+
@pytest.mark.parametrize("zarr_format", [2, 3])
1717
@pytest.mark.parametrize("exists_ok", [True, False])
1818
@pytest.mark.parametrize("extant_node", ["array", "group"])
1919
def test_array_creation_existing_node(
@@ -61,8 +61,8 @@ def test_array_creation_existing_node(
6161
)
6262

6363

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

7474

75-
@pytest.mark.parametrize("store", ("local", "memory", "zip"), indirect=["store"])
76-
@pytest.mark.parametrize("zarr_format", (2, 3))
75+
@pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"])
76+
@pytest.mark.parametrize("zarr_format", [2, 3])
7777
def test_array_name_properties_with_group(
7878
store: LocalStore | MemoryStore, zarr_format: ZarrFormat
7979
) -> None:
@@ -123,7 +123,7 @@ def test_array_v3_fill_value_default(
123123

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

203203

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

221221

222-
@pytest.mark.parametrize("store", ("local",), indirect=["store"])
223-
@pytest.mark.parametrize("zarr_format", (2, 3))
222+
@pytest.mark.parametrize("store", ["local"], indirect=["store"])
223+
@pytest.mark.parametrize("zarr_format", [2, 3])
224224
def test_serializable_sync_array(store: LocalStore, zarr_format: ZarrFormat) -> None:
225225
expected = Array.create(
226226
store=store, shape=(100,), chunks=(10,), zarr_format=zarr_format, dtype="i4"

tests/v3/test_chunk_grids.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66

77
@pytest.mark.parametrize(
8-
"shape", ((0,), (0,) * 2, (1, 2, 0, 4, 5), (10, 0), (10,), (100,) * 3, (1000000,), (10000,) * 2)
8+
"shape", [(0,), (0,) * 2, (1, 2, 0, 4, 5), (10, 0), (10,), (100,) * 3, (1000000,), (10000,) * 2]
99
)
10-
@pytest.mark.parametrize("itemsize", (1, 2, 4))
10+
@pytest.mark.parametrize("itemsize", [1, 2, 4])
1111
def test_guess_chunks(shape: tuple[int, ...], itemsize: int) -> None:
1212
chunks = _guess_chunks(shape, itemsize)
1313
chunk_size = np.prod(chunks) * itemsize

tests/v3/test_codec_entrypoints.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
here = os.path.abspath(os.path.dirname(__file__))
1111

1212

13-
@pytest.fixture()
13+
@pytest.fixture
1414
def set_path() -> Generator[None, None, None]:
1515
sys.path.append(here)
1616
zarr.registry._collect_entrypoints()

tests/v3/test_codecs/test_blosc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from zarr.store.common import StorePath
1111

1212

13-
@pytest.mark.parametrize("store", ("local", "memory"), indirect=["store"])
13+
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
1414
@pytest.mark.parametrize("dtype", ["uint8", "uint16"])
1515
async def test_blosc_evolve(store: Store, dtype: str) -> None:
1616
typesize = np.dtype(dtype).itemsize

tests/v3/test_codecs/test_codecs.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def test_sharding_pickle() -> None:
5959
pass
6060

6161

62-
@pytest.mark.parametrize("store", ("local", "memory"), indirect=["store"])
62+
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
6363
@pytest.mark.parametrize("input_order", ["F", "C"])
6464
@pytest.mark.parametrize("store_order", ["F", "C"])
6565
@pytest.mark.parametrize("runtime_write_order", ["F", "C"])
@@ -117,7 +117,7 @@ async def test_order(
117117
assert read_data.flags["C_CONTIGUOUS"]
118118

119119

120-
@pytest.mark.parametrize("store", ("local", "memory"), indirect=["store"])
120+
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
121121
@pytest.mark.parametrize("input_order", ["F", "C"])
122122
@pytest.mark.parametrize("runtime_write_order", ["F", "C"])
123123
@pytest.mark.parametrize("runtime_read_order", ["F", "C"])
@@ -159,7 +159,7 @@ def test_order_implicit(
159159
assert read_data.flags["C_CONTIGUOUS"]
160160

161161

162-
@pytest.mark.parametrize("store", ("local", "memory"), indirect=["store"])
162+
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
163163
def test_open(store: Store) -> None:
164164
spath = StorePath(store)
165165
a = Array.create(
@@ -205,7 +205,7 @@ def test_morton() -> None:
205205
]
206206

207207

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

222222

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

240240

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

274274

275-
@pytest.mark.parametrize("store", ("local", "memory"), indirect=["store"])
275+
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
276276
def test_invalid_metadata(store: Store) -> None:
277277
spath = StorePath(store, "invalid_metadata")
278278
with pytest.raises(ValueError):
@@ -360,7 +360,7 @@ def test_invalid_metadata(store: Store) -> None:
360360
)
361361

362362

363-
@pytest.mark.parametrize("store", ("local", "memory"), indirect=["store"])
363+
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
364364
async def test_resize(store: Store) -> None:
365365
data = np.zeros((16, 18), dtype="uint16")
366366
path = "resize"

tests/v3/test_codecs/test_endian.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from .test_codecs import _AsyncArrayProxy
1212

1313

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

3434

35-
@pytest.mark.parametrize("store", ("local", "memory"), indirect=["store"])
35+
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
3636
@pytest.mark.parametrize("dtype_input_endian", [">u2", "<u2"])
3737
@pytest.mark.parametrize("dtype_store_endian", ["big", "little"])
3838
async def test_endian_write(

tests/v3/test_codecs/test_gzip.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from zarr.store.common import StorePath
88

99

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

tests/v3/test_codecs/test_sharding.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from .test_codecs import _AsyncArrayProxy, order_from_dim
2222

2323

24-
@pytest.mark.parametrize("store", ("local", "memory", "zip"), indirect=["store"])
24+
@pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"])
2525
@pytest.mark.parametrize("index_location", ["start", "end"])
2626
@pytest.mark.parametrize(
2727
"array_fixture",
@@ -76,7 +76,7 @@ def test_sharding(
7676

7777

7878
@pytest.mark.parametrize("index_location", ["start", "end"])
79-
@pytest.mark.parametrize("store", ("local", "memory", "zip"), indirect=["store"])
79+
@pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"])
8080
@pytest.mark.parametrize(
8181
"array_fixture",
8282
[
@@ -126,7 +126,7 @@ def test_sharding_partial(
126126
indirect=["array_fixture"],
127127
)
128128
@pytest.mark.parametrize("index_location", ["start", "end"])
129-
@pytest.mark.parametrize("store", ("local", "memory", "zip"), indirect=["store"])
129+
@pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"])
130130
def test_sharding_partial_read(
131131
store: Store, array_fixture: npt.NDArray[Any], index_location: ShardingCodecIndexLocation
132132
) -> None:
@@ -163,7 +163,7 @@ def test_sharding_partial_read(
163163
indirect=["array_fixture"],
164164
)
165165
@pytest.mark.parametrize("index_location", ["start", "end"])
166-
@pytest.mark.parametrize("store", ("local", "memory", "zip"), indirect=["store"])
166+
@pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"])
167167
def test_sharding_partial_overwrite(
168168
store: Store, array_fixture: npt.NDArray[Any], index_location: ShardingCodecIndexLocation
169169
) -> None:
@@ -214,7 +214,7 @@ def test_sharding_partial_overwrite(
214214
"inner_index_location",
215215
["start", "end"],
216216
)
217-
@pytest.mark.parametrize("store", ("local", "memory", "zip"), indirect=["store"])
217+
@pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"])
218218
def test_nested_sharding(
219219
store: Store,
220220
array_fixture: npt.NDArray[Any],
@@ -247,7 +247,7 @@ def test_nested_sharding(
247247
assert np.array_equal(data, read_data)
248248

249249

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

274274

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

298298

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

328329

329330
def test_pickle() -> None:

tests/v3/test_codecs/test_transpose.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
@pytest.mark.parametrize("runtime_write_order", ["F", "C"])
2020
@pytest.mark.parametrize("runtime_read_order", ["F", "C"])
2121
@pytest.mark.parametrize("with_sharding", [True, False])
22-
@pytest.mark.parametrize("store", ("local", "memory"), indirect=["store"])
22+
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
2323
async def test_transpose(
2424
store: Store,
2525
input_order: MemoryOrder,
@@ -69,7 +69,7 @@ async def test_transpose(
6969
assert read_data.flags["C_CONTIGUOUS"]
7070

7171

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

9090

91-
@pytest.mark.parametrize("store", ("local", "memory"), indirect=["store"])
91+
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
9292
def test_transpose_invalid(
9393
store: Store,
9494
) -> None:

0 commit comments

Comments
 (0)