Skip to content

Commit e577953

Browse files
committed
Improve type hints
1 parent 3bd7592 commit e577953

File tree

3 files changed

+38
-55
lines changed

3 files changed

+38
-55
lines changed

maxminddb/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# pylint:disable=C0111
22
import os
3+
from typing import AnyStr, IO, Union
34

45
import maxminddb.reader
56

67
try:
78
import maxminddb.extension
89
except ImportError:
9-
maxminddb.extension = None
10+
maxminddb.extension = None # type: ignore
1011

1112
from maxminddb.const import (
1213
MODE_AUTO,
@@ -17,14 +18,12 @@
1718
MODE_FD,
1819
)
1920
from maxminddb.decoder import InvalidDatabaseError
20-
from io import BufferedReader
2121
from maxminddb.reader import Reader
22-
from typing import Union
2322

2423

2524
def open_database(
26-
database: Union[str, BufferedReader], mode: int = MODE_AUTO
27-
) -> Reader:
25+
database: Union[AnyStr, int, os.PathLike, IO], mode: int = MODE_AUTO
26+
) -> Union[Reader, "maxminddb.extension.Reader"]:
2827
"""Open a Maxmind DB database
2928
3029
Arguments:

maxminddb/file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# pylint: disable=no-name-in-module
77
from multiprocessing import Lock
88
except ImportError:
9-
from threading import Lock
9+
from threading import Lock # type: ignore
1010

1111

1212
class FileBuffer(object):

maxminddb/reader.py

Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,34 @@
1111
import mmap
1212
except ImportError:
1313
# pylint: disable=invalid-name
14-
mmap = None
14+
mmap = None # type: ignore
1515

1616
import ipaddress
1717
import struct
18+
from ipaddress import IPv4Address, IPv6Address
19+
from os import PathLike
20+
from typing import Any, AnyStr, Dict, List, IO, Optional, Tuple, Union
1821

1922
from maxminddb.const import MODE_AUTO, MODE_MMAP, MODE_FILE, MODE_MEMORY, MODE_FD
2023
from maxminddb.decoder import Decoder
2124
from maxminddb.errors import InvalidDatabaseError
2225
from maxminddb.file import FileBuffer
23-
from io import BufferedReader
24-
from ipaddress import IPv4Address, IPv6Address
25-
from typing import Dict, List, Optional, Tuple, Union
26+
27+
Record = Union[
28+
str,
29+
Dict[str, str],
30+
Dict[
31+
str,
32+
Union[
33+
List[int],
34+
bytes,
35+
float,
36+
int,
37+
Dict[str, Dict[str, Union[List[int], str]]],
38+
str,
39+
],
40+
],
41+
]
2642

2743

2844
class Reader(object):
@@ -34,10 +50,11 @@ class Reader(object):
3450
_DATA_SECTION_SEPARATOR_SIZE = 16
3551
_METADATA_START_MARKER = b"\xAB\xCD\xEFMaxMind.com"
3652

37-
_ipv4_start = None
53+
_buffer: Union[bytes, FileBuffer, "mmap.mmap"]
54+
_ipv4_start: Optional[int] = None
3855

3956
def __init__(
40-
self, database: Union[str, BufferedReader], mode: int = MODE_AUTO
57+
self, database: Union[AnyStr, int, PathLike, IO], mode: int = MODE_AUTO
4158
) -> None:
4259
"""Reader for the MaxMind DB file format
4360
@@ -52,8 +69,9 @@ def __init__(
5269
* MODE_FD - the param passed via database is a file descriptor, not
5370
a path. This mode implies MODE_MEMORY.
5471
"""
72+
filename: Any
5573
if (mode == MODE_AUTO and mmap) or mode == MODE_MMAP:
56-
with open(database, "rb") as db_file:
74+
with open(database, "rb") as db_file: # type: ignore
5775
self._buffer = mmap.mmap(db_file.fileno(), 0, access=mmap.ACCESS_READ)
5876
self._buffer_size = self._buffer.size()
5977
filename = database
@@ -62,14 +80,14 @@ def __init__(
6280
self._buffer_size = self._buffer.size()
6381
filename = database
6482
elif mode == MODE_MEMORY:
65-
with open(database, "rb") as db_file:
83+
with open(database, "rb") as db_file: # type: ignore
6684
self._buffer = db_file.read()
6785
self._buffer_size = len(self._buffer)
6886
filename = database
6987
elif mode == MODE_FD:
70-
self._buffer = database.read()
71-
self._buffer_size = len(self._buffer)
72-
filename = database.name
88+
self._buffer = database.read() # type: ignore
89+
self._buffer_size = len(self._buffer) # type: ignore
90+
filename = database.name # type: ignore
7391
else:
7492
raise ValueError(
7593
"Unsupported open mode ({0}). Only MODE_AUTO, MODE_FILE, "
@@ -106,23 +124,7 @@ def metadata(self) -> Metadata:
106124

107125
def get(
108126
self, ip_address: Union[str, IPv6Address, IPv4Address, int]
109-
) -> Optional[
110-
Union[
111-
Dict[
112-
str,
113-
Union[
114-
List[int],
115-
bytes,
116-
float,
117-
int,
118-
Dict[str, Dict[str, Union[List[int], str]]],
119-
str,
120-
],
121-
],
122-
Dict[str, str],
123-
str,
124-
]
125-
]:
127+
) -> Optional[Record]:
126128
"""Return the record for the ip_address in the MaxMind DB
127129
128130
@@ -134,25 +136,7 @@ def get(
134136

135137
def get_with_prefix_len(
136138
self, ip_address: Union[str, IPv6Address, IPv4Address, int]
137-
) -> Union[
138-
Tuple[
139-
Dict[
140-
str,
141-
Union[
142-
List[int],
143-
bytes,
144-
float,
145-
int,
146-
Dict[str, Dict[str, Union[List[int], str]]],
147-
str,
148-
],
149-
],
150-
int,
151-
],
152-
Tuple[str, int],
153-
Tuple[Dict[str, str], int],
154-
Tuple[None, int],
155-
]:
139+
) -> Tuple[Optional[Record], int]:
156140
"""Return a tuple with the record and the associated prefix length
157141
158142
@@ -267,9 +251,9 @@ def _resolve_data_pointer(
267251
def close(self) -> None:
268252
"""Closes the MaxMind DB file and returns the resources to the system"""
269253
try:
270-
self._buffer.close()
254+
self._buffer.close() # type: ignore
271255
except AttributeError:
272-
...
256+
pass
273257
self.closed = True
274258

275259
def __exit__(self, *args) -> None:

0 commit comments

Comments
 (0)