11"""Pure-Python reader for the MaxMind DB file format."""
22
3+ from __future__ import annotations
4+
35try :
46 import mmap
57except ImportError :
810import contextlib
911import ipaddress
1012import struct
11- from collections .abc import Iterator
1213from ipaddress import IPv4Address , IPv6Address
13- from os import PathLike
14- from typing import IO , Any , AnyStr , Optional , Union
14+ from typing import IO , TYPE_CHECKING , Any , AnyStr
1515
1616from maxminddb .const import MODE_AUTO , MODE_FD , MODE_FILE , MODE_MEMORY , MODE_MMAP
1717from maxminddb .decoder import Decoder
1818from maxminddb .errors import InvalidDatabaseError
1919from maxminddb .file import FileBuffer
20- from maxminddb .types import Record
20+
21+ if TYPE_CHECKING :
22+ from collections .abc import Iterator
23+ from os import PathLike
24+ from types import Self
25+
26+ from maxminddb .types import Record
2127
2228_IPV4_MAX_NUM = 2 ** 32
2329
@@ -31,16 +37,16 @@ class Reader:
3137 _DATA_SECTION_SEPARATOR_SIZE = 16
3238 _METADATA_START_MARKER = b"\xab \xcd \xef MaxMind.com"
3339
34- _buffer : Union [ bytes , FileBuffer , "mmap.mmap" ]
40+ _buffer : bytes | FileBuffer | "mmap.mmap" # noqa: UP037
3541 _buffer_size : int
3642 closed : bool
3743 _decoder : Decoder
38- _metadata : " Metadata"
44+ _metadata : Metadata
3945 _ipv4_start : int
4046
4147 def __init__ (
4248 self ,
43- database : Union [ AnyStr , int , PathLike , IO ] ,
49+ database : AnyStr | int | PathLike | IO ,
4450 mode : int = MODE_AUTO ,
4551 ) -> None :
4652 """Reader for the MaxMind DB file format.
@@ -133,11 +139,11 @@ def __init__(
133139 ipv4_start = node
134140 self ._ipv4_start = ipv4_start
135141
136- def metadata (self ) -> " Metadata" :
142+ def metadata (self ) -> Metadata :
137143 """Return the metadata associated with the MaxMind DB file."""
138144 return self ._metadata
139145
140- def get (self , ip_address : Union [ str , IPv6Address , IPv4Address ] ) -> Optional [ Record ] :
146+ def get (self , ip_address : str | IPv6Address | IPv4Address ) -> Record | None :
141147 """Return the record for the ip_address in the MaxMind DB.
142148
143149 Arguments:
@@ -149,8 +155,8 @@ def get(self, ip_address: Union[str, IPv6Address, IPv4Address]) -> Optional[Reco
149155
150156 def get_with_prefix_len (
151157 self ,
152- ip_address : Union [ str , IPv6Address , IPv4Address ] ,
153- ) -> tuple [Optional [ Record ] , int ]:
158+ ip_address : str | IPv6Address | IPv4Address ,
159+ ) -> tuple [Record | None , int ]:
154160 """Return a tuple with the record and the associated prefix length.
155161
156162 Arguments:
@@ -279,7 +285,7 @@ def close(self) -> None:
279285 def __exit__ (self , * _ ) -> None : # noqa: ANN002
280286 self .close ()
281287
282- def __enter__ (self ) -> "Reader" :
288+ def __enter__ (self ) -> Self :
283289 if self .closed :
284290 msg = "Attempt to reopen a closed MaxMind DB"
285291 raise ValueError (msg )
0 commit comments