77
88import inspect
99import os
10- from typing import Any , AnyStr , cast , IO , Optional , Sequence , Type , Union
10+ from collections .abc import Sequence
11+ from typing import IO , Any , AnyStr , Optional , Union , cast
1112
1213import maxminddb
13-
1414from maxminddb import (
1515 MODE_AUTO ,
16- MODE_MMAP ,
17- MODE_MMAP_EXT ,
16+ MODE_FD ,
1817 MODE_FILE ,
1918 MODE_MEMORY ,
20- MODE_FD ,
19+ MODE_MMAP ,
20+ MODE_MMAP_EXT ,
2121)
2222
2323import geoip2
24- import geoip2 .models
2524import geoip2 .errors
26- from geoip2 .types import IPAddress
25+ import geoip2 .models
2726from geoip2 .models import (
2827 ASN ,
28+ ISP ,
2929 AnonymousIP ,
3030 City ,
3131 ConnectionType ,
3232 Country ,
3333 Domain ,
3434 Enterprise ,
35- ISP ,
3635)
36+ from geoip2 .types import IPAddress
3737
3838__all__ = [
3939 "MODE_AUTO" ,
40- "MODE_MMAP" ,
41- "MODE_MMAP_EXT" ,
40+ "MODE_FD" ,
4241 "MODE_FILE" ,
4342 "MODE_MEMORY" ,
44- "MODE_FD" ,
43+ "MODE_MMAP" ,
44+ "MODE_MMAP_EXT" ,
4545 "Reader" ,
4646]
4747
@@ -135,9 +135,9 @@ def country(self, ip_address: IPAddress) -> Country:
135135 :returns: :py:class:`geoip2.models.Country` object
136136
137137 """
138-
139138 return cast (
140- Country , self ._model_for (geoip2 .models .Country , "Country" , ip_address )
139+ Country ,
140+ self ._model_for (geoip2 .models .Country , "Country" , ip_address ),
141141 )
142142
143143 def city (self , ip_address : IPAddress ) -> City :
@@ -161,7 +161,9 @@ def anonymous_ip(self, ip_address: IPAddress) -> AnonymousIP:
161161 return cast (
162162 AnonymousIP ,
163163 self ._flat_model_for (
164- geoip2 .models .AnonymousIP , "GeoIP2-Anonymous-IP" , ip_address
164+ geoip2 .models .AnonymousIP ,
165+ "GeoIP2-Anonymous-IP" ,
166+ ip_address ,
165167 ),
166168 )
167169
@@ -174,7 +176,8 @@ def asn(self, ip_address: IPAddress) -> ASN:
174176
175177 """
176178 return cast (
177- ASN , self ._flat_model_for (geoip2 .models .ASN , "GeoLite2-ASN" , ip_address )
179+ ASN ,
180+ self ._flat_model_for (geoip2 .models .ASN , "GeoLite2-ASN" , ip_address ),
178181 )
179182
180183 def connection_type (self , ip_address : IPAddress ) -> ConnectionType :
@@ -188,7 +191,9 @@ def connection_type(self, ip_address: IPAddress) -> ConnectionType:
188191 return cast (
189192 ConnectionType ,
190193 self ._flat_model_for (
191- geoip2 .models .ConnectionType , "GeoIP2-Connection-Type" , ip_address
194+ geoip2 .models .ConnectionType ,
195+ "GeoIP2-Connection-Type" ,
196+ ip_address ,
192197 ),
193198 )
194199
@@ -227,7 +232,8 @@ def isp(self, ip_address: IPAddress) -> ISP:
227232
228233 """
229234 return cast (
230- ISP , self ._flat_model_for (geoip2 .models .ISP , "GeoIP2-ISP" , ip_address )
235+ ISP ,
236+ self ._flat_model_for (geoip2 .models .ISP , "GeoIP2-ISP" , ip_address ),
231237 )
232238
233239 def _get (self , database_type : str , ip_address : IPAddress ) -> Any :
@@ -247,19 +253,26 @@ def _get(self, database_type: str, ip_address: IPAddress) -> Any:
247253
248254 def _model_for (
249255 self ,
250- model_class : Union [Type [Country ], Type [Enterprise ], Type [City ]],
256+ model_class : Union [type [Country ], type [Enterprise ], type [City ]],
251257 types : str ,
252258 ip_address : IPAddress ,
253259 ) -> Union [Country , Enterprise , City ]:
254260 (record , prefix_len ) = self ._get (types , ip_address )
255261 return model_class (
256- self ._locales , ip_address = ip_address , prefix_len = prefix_len , ** record
262+ self ._locales ,
263+ ip_address = ip_address ,
264+ prefix_len = prefix_len ,
265+ ** record ,
257266 )
258267
259268 def _flat_model_for (
260269 self ,
261270 model_class : Union [
262- Type [Domain ], Type [ISP ], Type [ConnectionType ], Type [ASN ], Type [AnonymousIP ]
271+ type [Domain ],
272+ type [ISP ],
273+ type [ConnectionType ],
274+ type [ASN ],
275+ type [AnonymousIP ],
263276 ],
264277 types : str ,
265278 ip_address : IPAddress ,
@@ -278,5 +291,4 @@ def metadata(
278291
279292 def close (self ) -> None :
280293 """Closes the GeoIP2 database."""
281-
282294 self ._db_reader .close ()
0 commit comments