Skip to content

Commit c254640

Browse files
committed
Improve type hinting
1 parent 382085e commit c254640

File tree

8 files changed

+268
-243
lines changed

8 files changed

+268
-243
lines changed

geoip2/_internal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Model(metaclass=ABCMeta):
1010
def __eq__(self, other: object) -> bool:
1111
return isinstance(other, self.__class__) and self.to_dict() == other.to_dict()
1212

13-
def __ne__(self, other) -> bool:
13+
def __ne__(self, other: object) -> bool:
1414
return not self.__eq__(other)
1515

1616
# pylint: disable=too-many-branches

geoip2/database.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""The database reader for MaxMind MMDB files."""
22

3+
from __future__ import annotations
4+
35
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
77

88
import maxminddb
99
from maxminddb import (
@@ -18,18 +18,23 @@
1818
import geoip2
1919
import geoip2.errors
2020
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
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

geoip2/errors.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Typed errors thrown by this library."""
22

3+
from __future__ import annotations
4+
35
import ipaddress
4-
from typing import Optional, Union
56

67

78
class GeoIP2Error(RuntimeError):
@@ -16,24 +17,24 @@ class GeoIP2Error(RuntimeError):
1617
class AddressNotFoundError(GeoIP2Error):
1718
"""The address you were looking up was not found."""
1819

19-
ip_address: Optional[str]
20+
ip_address: str | None
2021
"""The IP address used in the lookup. This is only available for database
2122
lookups.
2223
"""
23-
_prefix_len: Optional[int]
24+
_prefix_len: int | None
2425

2526
def __init__(
2627
self,
2728
message: str,
28-
ip_address: Optional[str] = None,
29-
prefix_len: Optional[int] = None,
29+
ip_address: str | None = None,
30+
prefix_len: int | None = None,
3031
) -> None:
3132
super().__init__(message)
3233
self.ip_address = ip_address
3334
self._prefix_len = prefix_len
3435

3536
@property
36-
def network(self) -> Optional[Union[ipaddress.IPv4Network, ipaddress.IPv6Network]]:
37+
def network(self) -> ipaddress.IPv4Network | ipaddress.IPv6Network | None:
3738
"""The network associated with the error.
3839
3940
In particular, this is the largest network where no address would be
@@ -42,7 +43,8 @@ def network(self) -> Optional[Union[ipaddress.IPv4Network, ipaddress.IPv6Network
4243
if self.ip_address is None or self._prefix_len is None:
4344
return None
4445
return ipaddress.ip_network(
45-
f"{self.ip_address}/{self._prefix_len}", strict=False,
46+
f"{self.ip_address}/{self._prefix_len}",
47+
strict=False,
4648
)
4749

4850

@@ -58,19 +60,19 @@ class HTTPError(GeoIP2Error):
5860
5961
"""
6062

61-
http_status: Optional[int]
63+
http_status: int | None
6264
"""The HTTP status code returned"""
63-
uri: Optional[str]
65+
uri: str | None
6466
"""The URI queried"""
65-
decoded_content: Optional[str]
67+
decoded_content: str | None
6668
"""The decoded response content"""
6769

6870
def __init__(
6971
self,
7072
message: str,
71-
http_status: Optional[int] = None,
72-
uri: Optional[str] = None,
73-
decoded_content: Optional[str] = None,
73+
http_status: int | None = None,
74+
uri: str | None = None,
75+
decoded_content: str | None = None,
7476
) -> None:
7577
super().__init__(message)
7678
self.http_status = http_status

0 commit comments

Comments
 (0)