Skip to content

Commit eda1f07

Browse files
committed
Improve type hints
1 parent d0da163 commit eda1f07

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

geoip2/_internal.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ 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):
13+
def __ne__(self, other) -> bool:
1414
return not self.__eq__(other)
1515

1616
# pylint: disable=too-many-branches
17-
def to_dict(self):
17+
def to_dict(self) -> dict:
1818
"""Returns a dict of the object suitable for serialization."""
1919
result = {}
2020
for key, value in self.__dict__.items():

geoip2/models.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from abc import ABCMeta
1717
from collections.abc import Sequence
1818
from typing import Optional, Union
19+
from ipaddress import IPv4Address, IPv6Address
1920

2021
import geoip2.records
2122
from geoip2._internal import Model
@@ -395,12 +396,9 @@ def __repr__(self) -> str:
395396
)
396397

397398
@property
398-
def ip_address(self):
399+
def ip_address(self) -> Union[IPv4Address, IPv6Address]:
399400
"""The IP address for the record."""
400-
if not isinstance(
401-
self._ip_address,
402-
(ipaddress.IPv4Address, ipaddress.IPv6Address),
403-
):
401+
if not isinstance(self._ip_address, (IPv4Address, IPv6Address)):
404402
self._ip_address = ipaddress.ip_address(self._ip_address)
405403
return self._ip_address
406404

geoip2/records.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
# pylint:disable=R0903
1313
from abc import ABCMeta
1414
from collections.abc import Sequence
15+
from ipaddress import IPv4Address, IPv6Address
1516
from typing import Optional, Union
1617

1718
from geoip2._internal import Model
19+
from geoip2.types import IPAddress
1820

1921

2022
class Record(Model, metaclass=ABCMeta):
@@ -841,7 +843,7 @@ class Traits(Record):
841843
autonomous_system_organization: Optional[str]
842844
connection_type: Optional[str]
843845
domain: Optional[str]
844-
_ip_address: Optional[str]
846+
_ip_address: IPAddress
845847
is_anonymous: bool
846848
is_anonymous_proxy: bool
847849
is_anonymous_vpn: bool
@@ -912,6 +914,8 @@ def __init__(
912914
self.static_ip_score = static_ip_score
913915
self.user_type = user_type
914916
self.user_count = user_count
917+
if ip_address is None:
918+
raise TypeError("ip_address must be defined")
915919
self._ip_address = ip_address
916920
if network is None:
917921
self._network = None
@@ -923,12 +927,9 @@ def __init__(
923927
self._prefix_len = prefix_len
924928

925929
@property
926-
def ip_address(self):
930+
def ip_address(self) -> Union[IPv4Address, IPv6Address]:
927931
"""The IP address for the record."""
928-
if not isinstance(
929-
self._ip_address,
930-
(ipaddress.IPv4Address, ipaddress.IPv6Address),
931-
):
932+
if not isinstance(self._ip_address, (IPv4Address, IPv6Address)):
932933
self._ip_address = ipaddress.ip_address(self._ip_address)
933934
return self._ip_address
934935

0 commit comments

Comments
 (0)