From 28c3b12f9f8b23f99ae0fe2179c468f7dcc39cba Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Tue, 28 Jan 2025 15:01:11 -0800 Subject: [PATCH] Allow Traits ip_address to be None again --- HISTORY.rst | 6 ++++++ geoip2/records.py | 17 ++++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 2654c8b..cac8b4f 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -4,6 +4,12 @@ History ------- +5.0.1 (2025-01-28) +++++++++++++++++++ + +* Allow ``ip_address`` in the ``Traits`` record to be ``None`` again. The + primary use case for this is from the ``minfraud`` package. + 5.0.0 (2025-01-28) ++++++++++++++++++ diff --git a/geoip2/records.py b/geoip2/records.py index 0c3e90f..f8ed399 100644 --- a/geoip2/records.py +++ b/geoip2/records.py @@ -843,7 +843,7 @@ class Traits(Record): autonomous_system_organization: Optional[str] connection_type: Optional[str] domain: Optional[str] - _ip_address: IPAddress + _ip_address: Optional[IPAddress] is_anonymous: bool is_anonymous_proxy: bool is_anonymous_vpn: bool @@ -914,8 +914,6 @@ def __init__( self.static_ip_score = static_ip_score self.user_type = user_type self.user_count = user_count - if ip_address is None: - raise TypeError("ip_address must be defined") self._ip_address = ip_address if network is None: self._network = None @@ -927,11 +925,16 @@ def __init__( self._prefix_len = prefix_len @property - def ip_address(self) -> Union[IPv4Address, IPv6Address]: + def ip_address(self) -> Optional[Union[IPv4Address, IPv6Address]]: """The IP address for the record.""" - if not isinstance(self._ip_address, (IPv4Address, IPv6Address)): - self._ip_address = ipaddress.ip_address(self._ip_address) - return self._ip_address + ip_address = self._ip_address + if ip_address is None: + return None + + if not isinstance(ip_address, (IPv4Address, IPv6Address)): + ip_address = ipaddress.ip_address(ip_address) + self._ip_address = ip_address + return ip_address @property def network(self) -> Optional[Union[ipaddress.IPv4Network, ipaddress.IPv6Network]]: