Repository navigation
Improve the public type hints #470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
oschwald
wants to merge
13
commits into
main
Choose a base branch
from
greg/stf-1960
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+160
−26
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ed1b79f
Import InvalidDatabaseError from maxminddb.errors
oschwald cfbf57d
Export Mode from maxminddb
oschwald 9fe9520
Add a release note for the concrete Record types
oschwald 8e852ce
Add bytearray to Primitive
oschwald 6240558
Add type arguments to Reader.__iter__
oschwald 9fc18b1
Annotate the __exit__ arguments
oschwald 45374ed
Remove an unneeded type: ignore
oschwald 6d225a6
Add type aliases for the database argument
oschwald aeb8f3a
Accept only paths in the extension Reader stub
oschwald 92342e5
Accept any binary reader with MODE_FD
oschwald 0f24bc2
Document maxminddb.types
oschwald 2a8a5f1
Add static type checks for the public API
oschwald c362576
Keep Record subscripts working at runtime
oschwald File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # mypy: warn-unused-ignores | ||
| """Static type checks for the public API. | ||
|
|
||
| mypy checks this file in the lint environment. The code does not run. Each | ||
| type: ignore marks a line that must fail the type check. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| import gzip | ||
| import io | ||
| from ipaddress import IPv4Network, IPv6Network | ||
| from pathlib import Path | ||
|
|
||
| from typing_extensions import assert_type | ||
|
|
||
| import maxminddb | ||
| import maxminddb.extension | ||
| from maxminddb.types import Primitive, Record | ||
|
|
||
| reader = maxminddb.open_database("GeoIP2-City.mmdb") | ||
| assert_type(reader.get("1.1.1.1"), Record | None) | ||
| assert_type(reader.get_with_prefix_len("1.1.1.1"), tuple[Record | None, int]) | ||
| for network, record in reader: | ||
| assert_type(network, IPv4Network | IPv6Network) | ||
| assert_type(record, Record) | ||
| assert_type(reader.metadata().search_tree_size, int) | ||
| with maxminddb.open_database(Path("GeoIP2-City.mmdb")) as path_reader: | ||
| assert_type(path_reader, maxminddb.Reader) | ||
|
|
||
| # Record includes the bytearray that the C extension returns for the | ||
| # bytes type. | ||
| value: Record = bytearray(b"\x00") | ||
|
|
||
| # A TypeVar in Primitive would give Record members of type Any. mypy | ||
| # rejects Record[str] even then, so only this check catches it. | ||
| def check_not_generic(primitive: Primitive[str]) -> None: # type: ignore[type-arg] | ||
| pass | ||
|
|
||
| def check_mode_fd(gzip_file: gzip.GzipFile, text_file: io.TextIOWrapper) -> None: | ||
| maxminddb.open_database(gzip_file, maxminddb.Mode.FD) | ||
| maxminddb.open_database(text_file, maxminddb.Mode.FD) # type: ignore[arg-type] | ||
|
|
||
| extension_reader = maxminddb.extension.Reader("GeoIP2-City.mmdb") | ||
| for network, record in extension_reader: | ||
| assert_type(network, IPv4Network | IPv6Network) | ||
| assert_type(record, Record) | ||
| assert_type(extension_reader.metadata().node_byte_size, int) | ||
| maxminddb.extension.Reader(3) # type: ignore[arg-type] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Remove
intfromDatabaseSourceor support integer descriptors.open_database(3, Mode.FD)satisfies this new public type alias. MODE_FD selects the pure Python reader, which callsdatabase.read()and raisesAttributeErrorfor an integer. If MODE_FD requires a readable object, removeintfrom the alias and correct the file-descriptor wording inmaxminddb/__init__.py.🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agent reply on behalf of @oschwald.
Not changed.
intwas already in this union before this PR. An integer descriptor works withMODE_MMAP,MODE_FILEandMODE_MEMORY, becauseopen()accepts one, so removingintwould reject code that works. #471 (c02aefd) fixes the docstrings that call theMODE_FDargument a file descriptor. bd2e9cc in #471 makes the default mode raise aTypeErrorthat suggestsMODE_MMAPfor a descriptor when the C extension is installed.