Skip to content

Commit 7a2d03f

Browse files
authored
Merge pull request #191 from maxmind/greg/eng-1114
Release 5.0.0
2 parents fb453e2 + 5df245d commit 7a2d03f

15 files changed

+424
-244
lines changed

Diff for: HISTORY.rst

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
History
55
-------
66

7-
5.0.0
7+
5.0.1 (2025-01-28)
8+
++++++++++++++++++
9+
10+
* Allow ``ip_address`` in the ``Traits`` record to be ``None`` again. The
11+
primary use case for this is from the ``minfraud`` package.
12+
13+
5.0.0 (2025-01-28)
814
++++++++++++++++++
915

1016
* BREAKING: The ``raw`` attribute on the model classes has been replaced

Diff for: docs/conf.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python3
2-
# -*- coding: utf-8 -*-
32
#
43
# geoip2 documentation build configuration file, created by
54
# sphinx-quickstart on Tue Apr 9 13:34:57 2013.
@@ -12,8 +11,8 @@
1211
# All configuration values have a default; values that are commented out
1312
# serve to show the default.
1413

15-
import sys
1614
import os
15+
import sys
1716

1817
# If extensions (or modules to document with autodoc) are in another directory,
1918
# add these directories to sys.path here. If the directory is relative to the

Diff for: examples/benchmark.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#!/usr/bin/python
2-
# -*- coding: utf-8 -*-
32

4-
from __future__ import print_function
53

64
import argparse
7-
import geoip2.database
5+
import contextlib
86
import random
97
import socket
108
import struct
119
import timeit
1210

11+
import geoip2.database
12+
1313
parser = argparse.ArgumentParser(description="Benchmark maxminddb.")
1414
parser.add_argument("--count", default=250000, type=int, help="number of lookups")
1515
parser.add_argument("--mode", default=0, type=int, help="reader mode to use")
@@ -20,12 +20,10 @@
2020
reader = geoip2.database.Reader(args.file, mode=args.mode)
2121

2222

23-
def lookup_ip_address():
23+
def lookup_ip_address() -> None:
2424
ip = socket.inet_ntoa(struct.pack("!L", random.getrandbits(32)))
25-
try:
26-
record = reader.city(str(ip))
27-
except geoip2.errors.AddressNotFoundError:
28-
pass
25+
with contextlib.suppress(geoip2.errors.AddressNotFoundError):
26+
reader.city(str(ip))
2927

3028

3129
elapsed = timeit.timeit(

Diff for: geoip2/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# pylint:disable=C0111
22

33
__title__ = "geoip2"
4-
__version__ = "4.8.1"
4+
__version__ = "5.0.1"
55
__author__ = "Gregory Oschwald"
66
__license__ = "Apache License, Version 2.0"
77
__copyright__ = "Copyright (c) 2013-2025 MaxMind, Inc."

Diff for: geoip2/_internal.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
"""This package contains internal utilities"""
1+
"""This package contains internal utilities."""
22

33
# pylint: disable=too-few-public-methods
44
from abc import ABCMeta
5-
from typing import Any
65

76

87
class Model(metaclass=ABCMeta):
9-
"""Shared methods for MaxMind model classes"""
8+
"""Shared methods for MaxMind model classes."""
109

11-
def __eq__(self, other: Any) -> bool:
10+
def __eq__(self, other: object) -> bool:
1211
return isinstance(other, self.__class__) and self.to_dict() == other.to_dict()
1312

14-
def __ne__(self, other):
13+
def __ne__(self, other) -> bool:
1514
return not self.__eq__(other)
1615

1716
# pylint: disable=too-many-branches
18-
def to_dict(self):
19-
"""Returns a dict of the object suitable for serialization"""
17+
def to_dict(self) -> dict:
18+
"""Returns a dict of the object suitable for serialization."""
2019
result = {}
2120
for key, value in self.__dict__.items():
2221
if key.startswith("_"):

Diff for: geoip2/database.py

+33-21
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,41 @@
77

88
import inspect
99
import os
10-
from typing import Any, AnyStr, cast, IO, Optional, Sequence, Type, Union
10+
from collections.abc import Sequence
11+
from typing import IO, Any, AnyStr, Optional, Union, cast
1112

1213
import maxminddb
13-
1414
from maxminddb import (
1515
MODE_AUTO,
16-
MODE_MMAP,
17-
MODE_MMAP_EXT,
16+
MODE_FD,
1817
MODE_FILE,
1918
MODE_MEMORY,
20-
MODE_FD,
19+
MODE_MMAP,
20+
MODE_MMAP_EXT,
2121
)
2222

2323
import geoip2
24-
import geoip2.models
2524
import geoip2.errors
26-
from geoip2.types import IPAddress
25+
import geoip2.models
2726
from geoip2.models import (
2827
ASN,
28+
ISP,
2929
AnonymousIP,
3030
City,
3131
ConnectionType,
3232
Country,
3333
Domain,
3434
Enterprise,
35-
ISP,
3635
)
36+
from geoip2.types import IPAddress
3737

3838
__all__ = [
3939
"MODE_AUTO",
40-
"MODE_MMAP",
41-
"MODE_MMAP_EXT",
40+
"MODE_FD",
4241
"MODE_FILE",
4342
"MODE_MEMORY",
44-
"MODE_FD",
43+
"MODE_MMAP",
44+
"MODE_MMAP_EXT",
4545
"Reader",
4646
]
4747

@@ -135,9 +135,9 @@ def country(self, ip_address: IPAddress) -> Country:
135135
:returns: :py:class:`geoip2.models.Country` object
136136
137137
"""
138-
139138
return cast(
140-
Country, self._model_for(geoip2.models.Country, "Country", ip_address)
139+
Country,
140+
self._model_for(geoip2.models.Country, "Country", ip_address),
141141
)
142142

143143
def city(self, ip_address: IPAddress) -> City:
@@ -161,7 +161,9 @@ def anonymous_ip(self, ip_address: IPAddress) -> AnonymousIP:
161161
return cast(
162162
AnonymousIP,
163163
self._flat_model_for(
164-
geoip2.models.AnonymousIP, "GeoIP2-Anonymous-IP", ip_address
164+
geoip2.models.AnonymousIP,
165+
"GeoIP2-Anonymous-IP",
166+
ip_address,
165167
),
166168
)
167169

@@ -174,7 +176,8 @@ def asn(self, ip_address: IPAddress) -> ASN:
174176
175177
"""
176178
return cast(
177-
ASN, self._flat_model_for(geoip2.models.ASN, "GeoLite2-ASN", ip_address)
179+
ASN,
180+
self._flat_model_for(geoip2.models.ASN, "GeoLite2-ASN", ip_address),
178181
)
179182

180183
def connection_type(self, ip_address: IPAddress) -> ConnectionType:
@@ -188,7 +191,9 @@ def connection_type(self, ip_address: IPAddress) -> ConnectionType:
188191
return cast(
189192
ConnectionType,
190193
self._flat_model_for(
191-
geoip2.models.ConnectionType, "GeoIP2-Connection-Type", ip_address
194+
geoip2.models.ConnectionType,
195+
"GeoIP2-Connection-Type",
196+
ip_address,
192197
),
193198
)
194199

@@ -227,7 +232,8 @@ def isp(self, ip_address: IPAddress) -> ISP:
227232
228233
"""
229234
return cast(
230-
ISP, self._flat_model_for(geoip2.models.ISP, "GeoIP2-ISP", ip_address)
235+
ISP,
236+
self._flat_model_for(geoip2.models.ISP, "GeoIP2-ISP", ip_address),
231237
)
232238

233239
def _get(self, database_type: str, ip_address: IPAddress) -> Any:
@@ -247,19 +253,26 @@ def _get(self, database_type: str, ip_address: IPAddress) -> Any:
247253

248254
def _model_for(
249255
self,
250-
model_class: Union[Type[Country], Type[Enterprise], Type[City]],
256+
model_class: Union[type[Country], type[Enterprise], type[City]],
251257
types: str,
252258
ip_address: IPAddress,
253259
) -> Union[Country, Enterprise, City]:
254260
(record, prefix_len) = self._get(types, ip_address)
255261
return model_class(
256-
self._locales, ip_address=ip_address, prefix_len=prefix_len, **record
262+
self._locales,
263+
ip_address=ip_address,
264+
prefix_len=prefix_len,
265+
**record,
257266
)
258267

259268
def _flat_model_for(
260269
self,
261270
model_class: Union[
262-
Type[Domain], Type[ISP], Type[ConnectionType], Type[ASN], Type[AnonymousIP]
271+
type[Domain],
272+
type[ISP],
273+
type[ConnectionType],
274+
type[ASN],
275+
type[AnonymousIP],
263276
],
264277
types: str,
265278
ip_address: IPAddress,
@@ -278,5 +291,4 @@ def metadata(
278291

279292
def close(self) -> None:
280293
"""Closes the GeoIP2 database."""
281-
282294
self._db_reader.close()

Diff for: geoip2/errors.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ def __init__(
5353

5454
@property
5555
def network(self) -> Optional[Union[ipaddress.IPv4Network, ipaddress.IPv6Network]]:
56-
"""The network for the error"""
57-
56+
"""The network for the error."""
5857
if self.ip_address is None or self._prefix_len is None:
5958
return None
6059
return ipaddress.ip_network(f"{self.ip_address}/{self._prefix_len}", False)

0 commit comments

Comments
 (0)