1111 import mmap
1212except ImportError :
1313 # pylint: disable=invalid-name
14- mmap = None
14+ mmap = None # type: ignore
1515
1616import ipaddress
1717import 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
1922from maxminddb .const import MODE_AUTO , MODE_MMAP , MODE_FILE , MODE_MEMORY , MODE_FD
2023from maxminddb .decoder import Decoder
2124from maxminddb .errors import InvalidDatabaseError
2225from 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
2844class Reader (object ):
@@ -34,10 +50,11 @@ class Reader(object):
3450 _DATA_SECTION_SEPARATOR_SIZE = 16
3551 _METADATA_START_MARKER = b"\xAB \xCD \xEF MaxMind.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