Skip to content

Commit 448cdca

Browse files
committed
Consistently import annotations from future
And use them.
1 parent 0d9051c commit 448cdca

File tree

4 files changed

+34
-29
lines changed

4 files changed

+34
-29
lines changed

maxminddb/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Module for reading MaxMind DB files."""
22

3-
import os
4-
from typing import IO, AnyStr, Union, cast
3+
from __future__ import annotations
4+
5+
from typing import IO, TYPE_CHECKING, AnyStr, cast
56

67
from .const import (
78
MODE_AUTO,
@@ -14,6 +15,9 @@
1415
from .decoder import InvalidDatabaseError
1516
from .reader import Reader
1617

18+
if TYPE_CHECKING:
19+
import os
20+
1721
try:
1822
from . import extension as _extension
1923
except ImportError:
@@ -34,7 +38,7 @@
3438

3539

3640
def open_database(
37-
database: Union[AnyStr, int, os.PathLike, IO],
41+
database: AnyStr | int | os.PathLike | IO,
3842
mode: int = MODE_AUTO,
3943
) -> Reader:
4044
"""Open a MaxMind DB database.

maxminddb/reader.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Pure-Python reader for the MaxMind DB file format."""
22

3+
from __future__ import annotations
4+
35
try:
46
import mmap
57
except ImportError:
@@ -8,16 +10,20 @@
810
import contextlib
911
import ipaddress
1012
import struct
11-
from collections.abc import Iterator
1213
from 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

1616
from maxminddb.const import MODE_AUTO, MODE_FD, MODE_FILE, MODE_MEMORY, MODE_MMAP
1717
from maxminddb.decoder import Decoder
1818
from maxminddb.errors import InvalidDatabaseError
1919
from 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\xefMaxMind.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)

pyproject.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ ignore = [
7272
# Conflicts with D212
7373
"D213",
7474

75-
# Don't bother with future imports for type annotations
76-
"FA100",
77-
7875
# Magic numbers for HTTP status codes seem ok most of the time.
7976
"PLR2004",
8077

tests/reader_test.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
from __future__ import annotations
2+
13
import ipaddress
24
import multiprocessing
35
import os
46
import pathlib
57
import threading
68
import unittest
7-
from typing import Union, cast
9+
from typing import TYPE_CHECKING, cast
810
from unittest import mock
911

1012
import maxminddb
@@ -23,7 +25,9 @@
2325
MODE_MMAP,
2426
MODE_MMAP_EXT,
2527
)
26-
from maxminddb.reader import Reader
28+
29+
if TYPE_CHECKING:
30+
from maxminddb.reader import Reader
2731

2832

2933
def get_reader_from_file_descriptor(filepath: str, mode: int) -> Reader:
@@ -40,18 +44,15 @@ def get_reader_from_file_descriptor(filepath: str, mode: int) -> Reader:
4044

4145
class BaseTestReader(unittest.TestCase):
4246
mode: int
43-
reader_class: Union[
44-
type["maxminddb.extension.Reader"],
45-
type["maxminddb.reader.Reader"],
46-
]
47+
reader_class: type[maxminddb.extension.Reader | maxminddb.reader.Reader]
4748
use_ip_objects = False
4849

4950
# fork doesn't work on Windows and spawn would involve pickling the reader,
5051
# which isn't possible.
5152
if os.name != "nt":
5253
mp = multiprocessing.get_context("fork")
5354

54-
def ipf(self, ip: str) -> Union[ipaddress.IPv4Address, ipaddress.IPv6Address, str]:
55+
def ipf(self, ip: str) -> ipaddress.IPv4Address | ipaddress.IPv6Address | str:
5556
if self.use_ip_objects:
5657
return ipaddress.ip_address(ip)
5758
return ip
@@ -680,10 +681,7 @@ class TestExtensionReaderWithIPObjects(BaseTestReader):
680681
class TestAutoReader(BaseTestReader):
681682
mode = MODE_AUTO
682683

683-
reader_class: Union[
684-
type["maxminddb.extension.Reader"],
685-
type["maxminddb.reader.Reader"],
686-
]
684+
reader_class: type[maxminddb.extension.Reader | maxminddb.reader.Reader]
687685
if has_maxminddb_extension():
688686
reader_class = maxminddb.extension.Reader
689687
else:

0 commit comments

Comments
 (0)