1
1
"""The database reader for MaxMind MMDB files."""
2
2
3
+ from __future__ import annotations
4
+
3
5
import 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
7
7
8
8
import maxminddb
9
9
from maxminddb import (
18
18
import geoip2
19
19
import geoip2 .errors
20
20
import 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
33
38
34
39
__all__ = [
35
40
"MODE_AUTO" ,
@@ -67,8 +72,8 @@ class Reader:
67
72
68
73
def __init__ (
69
74
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 ,
72
77
mode : int = MODE_AUTO ,
73
78
) -> None :
74
79
"""Create GeoIP2 Reader.
@@ -117,7 +122,7 @@ def __init__(
117
122
self ._db_type = self ._db_reader .metadata ().database_type
118
123
self ._locales = locales
119
124
120
- def __enter__ (self ) -> " Reader" :
125
+ def __enter__ (self ) -> Reader :
121
126
return self
122
127
123
128
def __exit__ (self , exc_type : None , exc_value : None , traceback : None ) -> None :
@@ -252,7 +257,9 @@ def isp(self, ip_address: IPAddress) -> ISP:
252
257
def _get (self , database_type : str , ip_address : IPAddress ) -> Any :
253
258
if database_type not in self ._db_type :
254
259
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
+ )
256
263
raise TypeError (
257
264
msg ,
258
265
)
@@ -268,10 +275,10 @@ def _get(self, database_type: str, ip_address: IPAddress) -> Any:
268
275
269
276
def _model_for (
270
277
self ,
271
- model_class : Union [ type [Country ], type [ Enterprise ], type [ City ] ],
278
+ model_class : type [City | Country | Enterprise ],
272
279
types : str ,
273
280
ip_address : IPAddress ,
274
- ) -> Union [ Country , Enterprise , City ] :
281
+ ) -> City | Country | Enterprise :
275
282
(record , prefix_len ) = self ._get (types , ip_address )
276
283
return model_class (
277
284
self ._locales ,
@@ -282,16 +289,10 @@ def _model_for(
282
289
283
290
def _flat_model_for (
284
291
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 ],
292
293
types : str ,
293
294
ip_address : IPAddress ,
294
- ) -> Union [ ConnectionType , ISP , AnonymousIP , Domain , ASN ] :
295
+ ) -> ConnectionType | ISP | AnonymousIP | Domain | ASN :
295
296
(record , prefix_len ) = self ._get (types , ip_address )
296
297
return model_class (ip_address , prefix_len = prefix_len , ** record )
297
298
0 commit comments