Skip to content

Commit 0a37adc

Browse files
Start enforcing bugbear rules (B) (#1602)
1 parent 4f8c4c5 commit 0a37adc

File tree

10 files changed

+27
-18
lines changed

10 files changed

+27
-18
lines changed

fsspec/caching.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
ClassVar,
1616
Generic,
1717
NamedTuple,
18+
Optional,
1819
OrderedDict,
1920
TypeVar,
2021
)
@@ -574,7 +575,7 @@ def __init__(
574575
blocksize: int,
575576
fetcher: Fetcher,
576577
size: int,
577-
data: dict[tuple[int, int], bytes] = {},
578+
data: Optional[dict[tuple[int, int], bytes]] = None,
578579
strict: bool = True,
579580
**_: Any,
580581
):
@@ -597,7 +598,7 @@ def __init__(
597598

598599
self.data = dict(zip(offsets, blocks))
599600
else:
600-
self.data = data
601+
self.data = {}
601602

602603
def _fetch(self, start: int | None, stop: int | None) -> bytes:
603604
if start is None:

fsspec/compression.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def _fetch_range(self, start, end):
139139
try:
140140
import snappy
141141

142-
snappy.compress
142+
snappy.compress(b"")
143143
# Snappy may use the .sz file extension, but this is not part of the
144144
# standard implementation.
145145
register_compression("snappy", SnappyFile, [])

fsspec/implementations/cached.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,6 @@ def __getattribute__(self, item):
425425
"clear_cache",
426426
"clear_expired_cache",
427427
"pop_from_cache",
428-
"_mkcache",
429428
"local_file",
430429
"_paths_from_path",
431430
"get_mapper",
@@ -438,9 +437,6 @@ def __getattribute__(self, item):
438437
"cache_size",
439438
"pipe_file",
440439
"pipe",
441-
"isdir",
442-
"isfile",
443-
"exists",
444440
"start_transaction",
445441
"end_transaction",
446442
}:

fsspec/implementations/tests/test_archive.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def temptar(data=None, mode="w", suffix=".tar"):
6565
fn = tempfile.mkstemp(suffix=suffix)[1]
6666
with tarfile.TarFile.open(fn, mode=mode) as t:
6767
touched = {}
68-
for name, data in data.items():
68+
for name, value in data.items():
6969
# Create directory hierarchy.
7070
# https://bugs.python.org/issue22208#msg225558
7171
if "/" in name and name not in touched:
@@ -78,8 +78,8 @@ def temptar(data=None, mode="w", suffix=".tar"):
7878

7979
# Add file content.
8080
info = tarfile.TarInfo(name=name)
81-
info.size = len(data)
82-
t.addfile(info, BytesIO(data))
81+
info.size = len(value)
82+
t.addfile(info, BytesIO(value))
8383

8484
try:
8585
yield fn

fsspec/implementations/tests/test_cached.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,7 @@ def test_cache_size(tmpdir, protocol):
12441244
# Remove cached file but leave cache metadata file
12451245
fs.pop_from_cache(afile)
12461246
if win and protocol == "filecache":
1247-
empty_cache_size < fs.cache_size()
1247+
assert empty_cache_size < fs.cache_size()
12481248
elif protocol != "simplecache":
12491249
assert empty_cache_size < fs.cache_size() < single_file_cache_size
12501250
else:

fsspec/tests/test_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,14 @@ def test_recursive_get_put(tmpdir, m):
168168
fs.get("test/", d, recursive=True)
169169
for file in ["one", "two", "nest/other"]:
170170
with open(f"{d}/{file}", "rb") as f:
171-
f.read() == b"data"
171+
assert f.read() == b"data"
172172

173173
# get to directory without slash
174174
d = tempfile.mkdtemp()
175175
fs.get("test", d, recursive=True)
176176
for file in ["test/one", "test/two", "test/nest/other"]:
177177
with open(f"{d}/{file}", "rb") as f:
178-
f.read() == b"data"
178+
assert f.read() == b"data"
179179

180180

181181
def test_pipe_cat(m):

fsspec/tests/test_core.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222

2323
@contextmanager
24-
def tempzip(data={}):
24+
def tempzip(data=None):
25+
data = data or {}
2526
f = tempfile.mkstemp(suffix="zip")[1]
2627
with zipfile.ZipFile(f, mode="w") as z:
2728
for k, v in data.items():
@@ -311,7 +312,7 @@ def test_open_file_write_with_special_characters(tmp_path, char, monkeypatch):
311312
f.write(expected_content * 2)
312313

313314
with open(file_path, "r") as f:
314-
f.read() == actual_content * 2
315+
assert f.read() == actual_content * 2
315316

316317
assert actual_content == expected_content
317318

fsspec/tests/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ def client(self):
381381
return mock
382382

383383
def func_2(self):
384-
assert False, "have to overwrite this"
384+
raise AssertionError("have to overwrite this")
385385

386386
def func_3(self):
387387
return "should succeed"

fsspec/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ def isfilelike(f: Any) -> TypeGuard[IO[bytes]]:
435435

436436
def get_protocol(url: str) -> str:
437437
url = stringify_path(url)
438-
parts = re.split(r"(\:\:|\://)", url, 1)
438+
parts = re.split(r"(\:\:|\://)", url, maxsplit=1)
439439
if len(parts) > 1:
440440
return parts[0]
441441
return "file"

pyproject.toml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ line-length = 88
152152
select = [
153153
# fix noqas in fsspec/implementations/http.py
154154
"ASYNC",
155-
# "B", enable in later PR
155+
"B",
156156
"I", # isort
157157
"C4",
158158
"G",
@@ -178,6 +178,17 @@ select = [
178178
"SIM101",
179179
]
180180
ignore = [
181+
# Loop control variable `loop` not used within loop body
182+
"B007",
183+
# Use of `functools.lru_cache` or `functools.cache` on methods can lead to memory leaks
184+
"B019",
185+
# Star-arg unpacking after a keyword argument is strongly discouraged
186+
"B026",
187+
# No explicit `stacklevel` keyword argument found
188+
"B028",
189+
# Within an `except` clause, raise exceptions with `raise ... from err` or
190+
# `raise ... from None` to distinguish them from errors in exception handling
191+
"B904",
181192
# Assigning lambda expression
182193
"E731",
183194
# Ambiguous variable names

0 commit comments

Comments
 (0)