11"""The database reader for MaxMind MMDB files."""
22
3+ from __future__ import annotations
4+
35import inspect
4- import os
5- from collections .abc import Sequence
6- from typing import IO , Any , AnyStr , Optional , Union , cast
6+ from typing import IO , TYPE_CHECKING , Any , AnyStr , cast
77
88import maxminddb
99from maxminddb import (
1818import geoip2
1919import geoip2 .errors
2020import geoip2 .models
21- from geoip2 .models import (
22- ASN ,
23- ISP ,
24- AnonymousIP ,
25- AnonymousPlus ,
26- City ,
27- ConnectionType ,
28- Country ,
29- Domain ,
30- Enterprise ,
31- )
32- from geoip2 .types import IPAddress
21+
22+ if TYPE_CHECKING :
23+ import os
24+ from collections .abc import Sequence
25+
26+ from geoip2 .models import (
27+ ASN ,
28+ ISP ,
29+ AnonymousIP ,
30+ AnonymousPlus ,
31+ City ,
32+ ConnectionType ,
33+ Country ,
34+ Domain ,
35+ Enterprise ,
36+ )
37+ from geoip2 .types import IPAddress
3338
3439__all__ = [
3540 "MODE_AUTO" ,
@@ -67,8 +72,8 @@ class Reader:
6772
6873 def __init__ (
6974 self ,
70- fileish : Union [ AnyStr , int , os .PathLike , IO ] ,
71- locales : Optional [ Sequence [str ]] = None ,
75+ fileish : AnyStr | int | os .PathLike | IO ,
76+ locales : Sequence [str ] | None = None ,
7277 mode : int = MODE_AUTO ,
7378 ) -> None :
7479 """Create GeoIP2 Reader.
@@ -117,7 +122,7 @@ def __init__(
117122 self ._db_type = self ._db_reader .metadata ().database_type
118123 self ._locales = locales
119124
120- def __enter__ (self ) -> " Reader" :
125+ def __enter__ (self ) -> Reader :
121126 return self
122127
123128 def __exit__ (self , exc_type : None , exc_value : None , traceback : None ) -> None :
@@ -252,7 +257,9 @@ def isp(self, ip_address: IPAddress) -> ISP:
252257 def _get (self , database_type : str , ip_address : IPAddress ) -> Any :
253258 if database_type not in self ._db_type :
254259 caller = inspect .stack ()[2 ][3 ]
255- msg = f"The { caller } method cannot be used with the { self ._db_type } database"
260+ msg = (
261+ f"The { caller } method cannot be used with the { self ._db_type } database"
262+ )
256263 raise TypeError (
257264 msg ,
258265 )
@@ -268,10 +275,10 @@ def _get(self, database_type: str, ip_address: IPAddress) -> Any:
268275
269276 def _model_for (
270277 self ,
271- model_class : Union [ type [Country ], type [ Enterprise ], type [ City ] ],
278+ model_class : type [City | Country | Enterprise ],
272279 types : str ,
273280 ip_address : IPAddress ,
274- ) -> Union [ Country , Enterprise , City ] :
281+ ) -> City | Country | Enterprise :
275282 (record , prefix_len ) = self ._get (types , ip_address )
276283 return model_class (
277284 self ._locales ,
@@ -282,16 +289,10 @@ def _model_for(
282289
283290 def _flat_model_for (
284291 self ,
285- model_class : Union [
286- type [Domain ],
287- type [ISP ],
288- type [ConnectionType ],
289- type [ASN ],
290- type [AnonymousIP ],
291- ],
292+ model_class : type [Domain | ISP | ConnectionType | ASN | AnonymousIP ],
292293 types : str ,
293294 ip_address : IPAddress ,
294- ) -> Union [ ConnectionType , ISP , AnonymousIP , Domain , ASN ] :
295+ ) -> ConnectionType | ISP | AnonymousIP | Domain | ASN :
295296 (record , prefix_len ) = self ._get (types , ip_address )
296297 return model_class (ip_address , prefix_len = prefix_len , ** record )
297298
0 commit comments