Skip to content

Commit 49a8927

Browse files
committed
Minor auto-fixes from ruff
1 parent 6047ee0 commit 49a8927

File tree

6 files changed

+43
-38
lines changed

6 files changed

+43
-38
lines changed

maxminddb/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Module for reading MaxMind DB files."""
2+
23
# pylint:disable=C0111
34
import os
45
from typing import IO, AnyStr, Union, cast

maxminddb/decoder.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Decoder for the MaxMind DB data section."""
22

33
import struct
4-
from typing import Dict, List, Tuple, Union, cast
4+
from typing import Union, cast
55

66
try:
77
# pylint: disable=unused-import
@@ -37,35 +37,35 @@ def __init__(
3737
self._buffer = database_buffer
3838
self._pointer_base = pointer_base
3939

40-
def _decode_array(self, size: int, offset: int) -> Tuple[List[Record], int]:
40+
def _decode_array(self, size: int, offset: int) -> tuple[list[Record], int]:
4141
array = []
4242
for _ in range(size):
4343
(value, offset) = self.decode(offset)
4444
array.append(value)
4545
return array, offset
4646

47-
def _decode_boolean(self, size: int, offset: int) -> Tuple[bool, int]:
47+
def _decode_boolean(self, size: int, offset: int) -> tuple[bool, int]:
4848
return size != 0, offset
4949

50-
def _decode_bytes(self, size: int, offset: int) -> Tuple[bytes, int]:
50+
def _decode_bytes(self, size: int, offset: int) -> tuple[bytes, int]:
5151
new_offset = offset + size
5252
return self._buffer[offset:new_offset], new_offset
5353

54-
def _decode_double(self, size: int, offset: int) -> Tuple[float, int]:
54+
def _decode_double(self, size: int, offset: int) -> tuple[float, int]:
5555
self._verify_size(size, 8)
5656
new_offset = offset + size
5757
packed_bytes = self._buffer[offset:new_offset]
5858
(value,) = struct.unpack(b"!d", packed_bytes)
5959
return value, new_offset
6060

61-
def _decode_float(self, size: int, offset: int) -> Tuple[float, int]:
61+
def _decode_float(self, size: int, offset: int) -> tuple[float, int]:
6262
self._verify_size(size, 4)
6363
new_offset = offset + size
6464
packed_bytes = self._buffer[offset:new_offset]
6565
(value,) = struct.unpack(b"!f", packed_bytes)
6666
return value, new_offset
6767

68-
def _decode_int32(self, size: int, offset: int) -> Tuple[int, int]:
68+
def _decode_int32(self, size: int, offset: int) -> tuple[int, int]:
6969
if size == 0:
7070
return 0, offset
7171
new_offset = offset + size
@@ -76,15 +76,15 @@ def _decode_int32(self, size: int, offset: int) -> Tuple[int, int]:
7676
(value,) = struct.unpack(b"!i", packed_bytes)
7777
return value, new_offset
7878

79-
def _decode_map(self, size: int, offset: int) -> Tuple[Dict[str, Record], int]:
80-
container: Dict[str, Record] = {}
79+
def _decode_map(self, size: int, offset: int) -> tuple[dict[str, Record], int]:
80+
container: dict[str, Record] = {}
8181
for _ in range(size):
8282
(key, offset) = self.decode(offset)
8383
(value, offset) = self.decode(offset)
84-
container[cast(str, key)] = value
84+
container[cast("str", key)] = value
8585
return container, offset
8686

87-
def _decode_pointer(self, size: int, offset: int) -> Tuple[Record, int]:
87+
def _decode_pointer(self, size: int, offset: int) -> tuple[Record, int]:
8888
pointer_size = (size >> 3) + 1
8989

9090
buf = self._buffer[offset : offset + pointer_size]
@@ -107,12 +107,12 @@ def _decode_pointer(self, size: int, offset: int) -> Tuple[Record, int]:
107107
(value, _) = self.decode(pointer)
108108
return value, new_offset
109109

110-
def _decode_uint(self, size: int, offset: int) -> Tuple[int, int]:
110+
def _decode_uint(self, size: int, offset: int) -> tuple[int, int]:
111111
new_offset = offset + size
112112
uint_bytes = self._buffer[offset:new_offset]
113113
return int.from_bytes(uint_bytes, "big"), new_offset
114114

115-
def _decode_utf8_string(self, size: int, offset: int) -> Tuple[str, int]:
115+
def _decode_utf8_string(self, size: int, offset: int) -> tuple[str, int]:
116116
new_offset = offset + size
117117
return self._buffer[offset:new_offset].decode("utf-8"), new_offset
118118

@@ -132,7 +132,7 @@ def _decode_utf8_string(self, size: int, offset: int) -> Tuple[str, int]:
132132
15: _decode_float,
133133
}
134134

135-
def decode(self, offset: int) -> Tuple[Record, int]:
135+
def decode(self, offset: int) -> tuple[Record, int]:
136136
"""Decode a section of the data section starting at offset.
137137
138138
Arguments:
@@ -156,7 +156,7 @@ def decode(self, offset: int) -> Tuple[Record, int]:
156156
(size, new_offset) = self._size_from_ctrl_byte(ctrl_byte, new_offset, type_num)
157157
return decoder(self, size, new_offset)
158158

159-
def _read_extended(self, offset: int) -> Tuple[int, int]:
159+
def _read_extended(self, offset: int) -> tuple[int, int]:
160160
next_byte = self._buffer[offset]
161161
type_num = next_byte + 7
162162
if type_num < 7:
@@ -179,7 +179,7 @@ def _size_from_ctrl_byte(
179179
ctrl_byte: int,
180180
offset: int,
181181
type_num: int,
182-
) -> Tuple[int, int]:
182+
) -> tuple[int, int]:
183183
size = ctrl_byte & 0x1F
184184
if type_num == 1 or size < 29:
185185
return size, offset

maxminddb/extension.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# pylint: disable=E0601,E0602
44
from ipaddress import IPv4Address, IPv6Address
55
from os import PathLike
6-
from typing import IO, Any, AnyStr, Optional, Tuple, Union
6+
from typing import IO, Any, AnyStr, Optional, Union
77

88
from maxminddb import MODE_AUTO
99
from maxminddb.types import Record
@@ -44,7 +44,7 @@ class Reader:
4444
def get_with_prefix_len(
4545
self,
4646
ip_address: Union[str, IPv6Address, IPv4Address],
47-
) -> Tuple[Optional[Record], int]:
47+
) -> tuple[Optional[Record], int]:
4848
"""Return a tuple with the record and the associated prefix length
4949
5050
Arguments:

maxminddb/reader.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
import ipaddress
1010
import struct
11+
from collections.abc import Iterator
1112
from ipaddress import IPv4Address, IPv6Address
1213
from os import PathLike
13-
from typing import IO, Any, AnyStr, Dict, Iterator, List, Optional, Tuple, Union
14+
from typing import IO, Any, AnyStr, Optional, Union
1415

1516
from maxminddb.const import MODE_AUTO, MODE_FD, MODE_FILE, MODE_MEMORY, MODE_MMAP
1617
from maxminddb.decoder import Decoder
@@ -22,8 +23,7 @@
2223

2324

2425
class Reader:
25-
"""
26-
A pure Python implementation of a reader for the MaxMind DB format. IP
26+
"""A pure Python implementation of a reader for the MaxMind DB format. IP
2727
addresses can be looked up using the ``get`` method.
2828
"""
2929

@@ -142,7 +142,7 @@ def get(self, ip_address: Union[str, IPv6Address, IPv4Address]) -> Optional[Reco
142142
def get_with_prefix_len(
143143
self,
144144
ip_address: Union[str, IPv6Address, IPv4Address],
145-
) -> Tuple[Optional[Record], int]:
145+
) -> tuple[Optional[Record], int]:
146146
"""Return a tuple with the record and the associated prefix length.
147147
148148
Arguments:
@@ -196,7 +196,7 @@ def _generate_children(self, node, depth, ip_acc) -> Iterator:
196196
right = self._read_node(node, 1)
197197
yield from self._generate_children(right, depth, ip_acc | 1)
198198

199-
def _find_address_in_tree(self, packed: bytearray) -> Tuple[int, int]:
199+
def _find_address_in_tree(self, packed: bytearray) -> tuple[int, int]:
200200
bit_count = len(packed) * 8
201201
node = self._start_node(bit_count)
202202
node_count = self._metadata.node_count
@@ -297,7 +297,7 @@ class Metadata:
297297
A string identifying the database type, e.g., "GeoIP2-City".
298298
"""
299299

300-
description: Dict[str, str]
300+
description: dict[str, str]
301301
"""
302302
A map from locales to text descriptions of the database.
303303
"""
@@ -309,7 +309,7 @@ class Metadata:
309309
both IPv4 and IPv6 lookups.
310310
"""
311311

312-
languages: List[str]
312+
languages: list[str]
313313
"""
314314
A list of locale codes supported by the databse.
315315
"""

maxminddb/types.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""Types representing database records."""
22

3-
from typing import AnyStr, Dict, List, Union
3+
from typing import AnyStr, Union
44

55
Primitive = Union[AnyStr, bool, float, int]
66
Record = Union[Primitive, "RecordList", "RecordDict"]
77

88

9-
class RecordList(List[Record]): # pylint: disable=too-few-public-methods
9+
class RecordList(list[Record]): # pylint: disable=too-few-public-methods
1010
"""RecordList is a type for lists in a database record."""
1111

1212

13-
class RecordDict(Dict[str, Record]): # pylint: disable=too-few-public-methods
13+
class RecordDict(dict[str, Record]): # pylint: disable=too-few-public-methods
1414
"""RecordDict is a type for dicts in a database record."""

tests/reader_test.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pathlib
77
import threading
88
import unittest
9-
from typing import Type, Union, cast
9+
from typing import Union, cast
1010
from unittest import mock
1111

1212
import maxminddb
@@ -43,8 +43,8 @@ def get_reader_from_file_descriptor(filepath, mode) -> Reader:
4343
class BaseTestReader(unittest.TestCase):
4444
mode: int
4545
reader_class: Union[
46-
Type["maxminddb.extension.Reader"],
47-
Type["maxminddb.reader.Reader"],
46+
type["maxminddb.extension.Reader"],
47+
type["maxminddb.reader.Reader"],
4848
]
4949
use_ip_objects = False
5050

@@ -482,10 +482,13 @@ def test_with_statement_close(self) -> None:
482482
reader = open_database(filename, self.mode)
483483
reader.close()
484484

485-
with self.assertRaisesRegex(
486-
ValueError,
487-
"Attempt to reopen a closed MaxMind DB",
488-
), reader:
485+
with (
486+
self.assertRaisesRegex(
487+
ValueError,
488+
"Attempt to reopen a closed MaxMind DB",
489+
),
490+
reader,
491+
):
489492
pass
490493

491494
def test_closed(self) -> None:
@@ -642,7 +645,8 @@ def _check_ip_v6(self, reader, file_name) -> None:
642645

643646
def has_maxminddb_extension() -> bool:
644647
return maxminddb.extension and hasattr(
645-
maxminddb.extension, "Reader"
648+
maxminddb.extension,
649+
"Reader",
646650
) # type: ignore
647651

648652

@@ -673,8 +677,8 @@ class TestAutoReader(BaseTestReader):
673677
mode = MODE_AUTO
674678

675679
reader_class: Union[
676-
Type["maxminddb.extension.Reader"],
677-
Type["maxminddb.reader.Reader"],
680+
type["maxminddb.extension.Reader"],
681+
type["maxminddb.reader.Reader"],
678682
]
679683
if has_maxminddb_extension():
680684
reader_class = maxminddb.extension.Reader

0 commit comments

Comments
 (0)