Skip to content

Commit bb00a90

Browse files
committed
Minor style fixes and ignoring of particular lints
1 parent eecf1f7 commit bb00a90

File tree

6 files changed

+33
-52
lines changed

6 files changed

+33
-52
lines changed

examples/benchmark.py

100644100755
File mode changed.

maxminddb/decoder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(
2323
self,
2424
database_buffer: Union[FileBuffer, "mmap.mmap", bytes],
2525
pointer_base: int = 0,
26-
pointer_test: bool = False,
26+
pointer_test: bool = False, # noqa: FBT001, FBT002
2727
) -> None:
2828
"""Create a Decoder for a MaxMind DB.
2929

maxminddb/extension.pyi

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
# pylint: disable=E0601,E0602
42
from ipaddress import IPv4Address, IPv6Address
53
from os import PathLike
@@ -10,39 +8,25 @@ from typing_extensions import Self
108
from maxminddb.types import Record
119

1210
class Reader:
13-
14-
1511
closed: bool = ...
1612

1713
def __init__(
1814
self,
1915
database: AnyStr | int | PathLike | IO,
2016
mode: int = ...,
21-
) -> None:
22-
...
23-
24-
def close(self) -> None:
25-
...
26-
27-
def get(self, ip_address: str | IPv6Address | IPv4Address) -> Record | None:
28-
...
29-
17+
) -> None: ...
18+
def close(self) -> None: ...
19+
def get(self, ip_address: str | IPv6Address | IPv4Address) -> Record | None: ...
3020
def get_with_prefix_len(
3121
self,
3222
ip_address: str | IPv6Address | IPv4Address,
33-
) -> tuple[Record | None, int]:
34-
...
35-
36-
def metadata(self) -> Metadata:
37-
...
38-
23+
) -> tuple[Record | None, int]: ...
24+
def metadata(self) -> Metadata: ...
3925
def __enter__(self) -> Self: ...
40-
def __exit__(self, *args) -> None: ...
26+
def __exit__(self, *args) -> None: ... # noqa: ANN002
4127

4228
# pylint: disable=too-few-public-methods
4329
class Metadata:
44-
45-
4630
binary_format_major_version: int
4731
"""
4832
The major version number of the binary format used when creating the
@@ -92,5 +76,4 @@ class Metadata:
9276
The bit size of a record in the search tree.
9377
"""
9478

95-
def __init__(self, **kwargs: Any) -> None:
96-
...
79+
def __init__(self, **kwargs: Any) -> None: ... # noqa: ANN401

maxminddb/file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class FileBuffer:
1818
def __init__(self, database: str) -> None:
1919
"""Create FileBuffer."""
2020
# pylint: disable=consider-using-with
21-
self._handle = open(database, "rb")
21+
self._handle = open(database, "rb") # noqa: SIM115
2222
self._size = os.fstat(self._handle.fileno()).st_size
2323
if not hasattr(os, "pread"):
2424
self._lock = Lock()

maxminddb/reader.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# pylint: disable=invalid-name
77
mmap = None # type: ignore[assignment]
88

9+
import contextlib
910
import ipaddress
1011
import struct
1112
from collections.abc import Iterator
@@ -270,14 +271,13 @@ def _resolve_data_pointer(self, pointer: int) -> Record:
270271
return data
271272

272273
def close(self) -> None:
273-
"""Closes the MaxMind DB file and returns the resources to the system."""
274-
try:
275-
self._buffer.close() # type: ignore
276-
except AttributeError:
277-
pass
274+
"""Close the MaxMind DB file and returns the resources to the system."""
275+
with contextlib.suppress(AttributeError):
276+
self._buffer.close() # type: ignore[union-attr]
277+
278278
self.closed = True
279279

280-
def __exit__(self, *args) -> None:
280+
def __exit__(self, *_) -> None: # noqa: ANN002
281281
self.close()
282282

283283
def __enter__(self) -> "Reader":

tests/reader_test.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#!/usr/bin/env python
2-
31
import ipaddress
42
import multiprocessing
53
import os
@@ -179,13 +177,16 @@ def test_get_with_prefix_len(self) -> None:
179177
"tests/data/test-data/" + cast("str", test["file_name"]),
180178
self.mode,
181179
) as reader:
182-
(record, prefix_len) = reader.get_with_prefix_len(cast("str", test["ip"]))
180+
(record, prefix_len) = reader.get_with_prefix_len(
181+
cast("str", test["ip"]),
182+
)
183183

184184
self.assertEqual(
185185
prefix_len,
186186
test["expected_prefix_len"],
187-
f"expected prefix_len of {test['expected_prefix_len']} for {test['ip']}"
188-
+ f" in {test['file_name']} but got {prefix_len}",
187+
f"expected prefix_len of {test['expected_prefix_len']}"
188+
f" for {test['ip']}"
189+
f" in {test['file_name']} but got {prefix_len}",
189190
)
190191
self.assertEqual(
191192
record,
@@ -239,7 +240,10 @@ def test_iterator(self) -> None:
239240

240241
for record_size in [24, 28, 32]:
241242
for test in tests:
242-
f = f"tests/data/test-data/MaxMind-DB-test-{test['database']}-{record_size}.mmdb"
243+
f = (
244+
f"tests/data/test-data/MaxMind-DB-test-{test['database']}"
245+
f"-{record_size}.mmdb"
246+
)
243247
reader = open_database(f, self.mode)
244248
networks = [str(n) for (n, _) in reader]
245249
self.assertEqual(networks, test["expected"], f)
@@ -324,7 +328,7 @@ def test_no_extension_exception(self) -> None:
324328
"tests/data/test-data/MaxMind-DB-test-decoder.mmdb",
325329
MODE_MMAP_EXT,
326330
)
327-
maxminddb._extension = real_extension
331+
maxminddb._extension = real_extension # noqa: SLF001
328332

329333
def test_broken_database(self) -> None:
330334
reader = open_database(
@@ -453,10 +457,8 @@ def test_double_close(self) -> None:
453457
self.mode,
454458
)
455459
reader.close()
456-
self.assertIsNone(
457-
reader.close(),
458-
"Double close does not throw an exception", # type: ignore
459-
)
460+
# Check that calling close again doesn't raise an exception
461+
reader.close()
460462

461463
def test_closed_get(self) -> None:
462464
if self.mode in [MODE_MEMORY, MODE_FD]:
@@ -501,10 +503,6 @@ def test_closed(self) -> None:
501503
reader.close()
502504
self.assertEqual(reader.closed, True)
503505

504-
# XXX - Figure out whether we want to have the same behavior on both the
505-
# extension and the pure Python reader. If we do, the pure Python
506-
# reader will need to throw an exception or the extension will need
507-
# to keep the metadata in memory.
508506
def test_closed_metadata(self) -> None:
509507
reader = open_database(
510508
"tests/data/test-data/MaxMind-DB-test-decoder.mmdb",
@@ -533,26 +531,26 @@ def test_multiprocessing(self):
533531
def test_threading(self):
534532
self._check_concurrency(threading.Thread)
535533

536-
def _check_concurrency(self, worker_class) -> None:
534+
def _check_concurrency(self, worker_class) -> None: # noqa: ANN001
537535
reader = open_database(
538536
"tests/data/test-data/GeoIP2-Domain-Test.mmdb",
539537
self.mode,
540538
)
541539

542-
def lookup(pipe) -> None:
540+
def lookup(pipe) -> None: # noqa: ANN001
543541
try:
544542
for i in range(32):
545543
reader.get(self.ipf(f"65.115.240.{i}"))
546544
pipe.send(1)
547-
except:
545+
except: # noqa: E722
548546
pipe.send(0)
549547
finally:
550-
if worker_class is self.mp.Process: # type: ignore
548+
if worker_class is self.mp.Process:
551549
reader.close()
552550
pipe.close()
553551

554552
pipes = [self.mp.Pipe() for _ in range(32)]
555-
procs = [worker_class(target=lookup, args=(c,)) for (p, c) in pipes]
553+
procs = [worker_class(target=lookup, args=(c,)) for (_, c) in pipes]
556554
for proc in procs:
557555
proc.start()
558556
for proc in procs:

0 commit comments

Comments
 (0)