Skip to content

Commit 84e3534

Browse files
authored
Merge pull request #117 from maxmind/greg/pathlike
Fix type hints for database path
2 parents b9b2403 + 4514c8b commit 84e3534

File tree

7 files changed

+33
-15
lines changed

7 files changed

+33
-15
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- name: Install dependencies
2525
run: |
2626
python -m pip install --upgrade pip
27-
pip install --upgrade pylint black mypy voluptuous-stubs chardet==3.0.4 types-requests types-maxminddb
27+
pip install --upgrade pylint black maxminddb mypy voluptuous-stubs chardet==3.0.4 types-requests
2828
2929
- name: Install
3030
run: python setup.py install

HISTORY.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
History
44
-------
55

6+
4.3.0
7+
++++++++++++++++++
8+
9+
* Previously, the ``py.typed`` file was not being added to the source
10+
distribution. It is now explicitly specified in the manifest.
11+
* The type hints for the database file in the ``Reader`` constructor have
12+
been expanded to match those specified by ``maxmindb.open_database``. In
13+
particular, ``os.PathLike`` and ``IO`` have been added.
14+
* Corrected the type hint for the ``metadata()`` method on ``Reader``. It
15+
will return a ``maxminddb.extension.Metadata`` if the C extension is being
16+
used.
17+
618
4.2.0 (2021-05-12)
719
++++++++++++++++++
820

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
include HISTORY.rst README.rst LICENSE requirements.txt tests/*.py tests/data/test-data/*.mmdb
1+
include HISTORY.rst README.rst LICENSE geoip2/py.typed requirements.txt tests/*.py tests/data/test-data/*.mmdb
22
graft docs/html

README.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -394,11 +394,11 @@ Database Reader Exceptions
394394
--------------------------
395395

396396
If the database file does not exist or is not readable, the constructor will
397-
raise a ``FileNotFoundError``. If the IP address passed to a method is
398-
invalid, a ``ValueError`` will be raised. If the file is invalid or there is a
399-
bug in the reader, a ``maxminddb.InvalidDatabaseError`` will be raised with a
400-
description of the problem. If an IP address is not in the database, a
401-
``AddressNotFoundError`` will be raised.
397+
raise a ``FileNotFoundError`` or a ``PermissionError``. If the IP address passed
398+
to a method is invalid, a ``ValueError`` will be raised. If the file is invalid
399+
or there is a bug in the reader, a ``maxminddb.InvalidDatabaseError`` will be
400+
raised with a description of the problem. If an IP address is not in the
401+
database, a ``AddressNotFoundError`` will be raised.
402402

403403
Values to use for Database or Dictionary Keys
404404
---------------------------------------------

geoip2/database.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
66
"""
77
import inspect
8-
from typing import Any, cast, List, Optional, Type, Union
8+
import os
9+
from typing import Any, AnyStr, cast, IO, List, Optional, Type, Union
910

1011
import maxminddb
1112

@@ -59,13 +60,16 @@ class Reader:
5960
"""
6061

6162
def __init__(
62-
self, fileish: str, locales: Optional[List[str]] = None, mode: int = MODE_AUTO
63+
self,
64+
fileish: Union[AnyStr, int, os.PathLike, IO],
65+
locales: Optional[List[str]] = None,
66+
mode: int = MODE_AUTO,
6367
) -> None:
6468
"""Create GeoIP2 Reader.
6569
66-
:param fileish: The string path to the GeoIP2 database, or an existing
67-
file descriptor pointing to the database. Note that this latter
68-
usage is only valid when mode is MODE_FD.
70+
:param fileish: A path to the GeoIP2 database or an existing file
71+
descriptor pointing to the database. Note that a file descriptor
72+
is only valid when mode is MODE_FD.
6973
:param locales: This is list of locale codes. This argument will be
7074
passed on to record classes to use when their name properties are
7175
called. The default value is ['en'].
@@ -254,7 +258,9 @@ def _flat_model_for(
254258
record["prefix_len"] = prefix_len
255259
return model_class(record)
256260

257-
def metadata(self) -> maxminddb.reader.Metadata:
261+
def metadata(
262+
self,
263+
) -> Union[maxminddb.reader.Metadata, "maxminddb.extension.Metadata"]:
258264
"""The metadata for the open database.
259265
260266
:returns: :py:class:`maxminddb.reader.Metadata` object

geoip2/records.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Record(SimpleEquality, metaclass=ABCMeta):
1919
"""All records are subclasses of the abstract class ``Record``."""
2020

2121
def __repr__(self) -> str:
22-
args = ", ".join("%s=%r" % x for x in self.__dict__.items())
22+
args = ", ".join(f"{k}={v!r}" for k, v in self.__dict__.items())
2323
return f"{self.__module__}.{self.__class__.__name__}({args})"
2424

2525

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
aiohttp>=3.6.2,<4.0.0
2-
maxminddb>=2.0.0,<3.0.0
2+
maxminddb>=2.1.0,<3.0.0
33
requests>=2.24.0,<3.0.0
44
urllib3>=1.25.2,<2.0.0

0 commit comments

Comments
 (0)