Skip to content

Commit 9f87518

Browse files
oschwaldclaude
andcommitted
Improve type annotations for Record types and Decoder
Convert RecordList and RecordDict from class-based type definitions to explicit TypeAlias declarations. These types are only used for type annotations and were never instantiated as actual classes, so using TypeAlias makes their purpose more explicit and follows modern Python typing conventions. Add explicit type annotation to Decoder._type_decoder ClassVar. This provides better type checking for the decoder function map by fully specifying the callable signature. These are internal changes with no user-visible effects. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 53101d0 commit 9f87518

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

maxminddb/decoder.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010
except ImportError:
1111
mmap = None # type: ignore[assignment]
1212

13-
1413
from maxminddb.errors import InvalidDatabaseError
1514

1615
if TYPE_CHECKING:
16+
from collections.abc import Callable
17+
1718
from maxminddb.file import FileBuffer
1819
from maxminddb.types import Record
1920

21+
DecoderFunc = Callable[["Decoder", int, int], tuple[Record, int]]
22+
2023

2124
class Decoder:
2225
"""Decoder for the data section of the MaxMind DB."""
@@ -118,7 +121,7 @@ def _decode_utf8_string(self, size: int, offset: int) -> tuple[str, int]:
118121
new_offset = offset + size
119122
return self._buffer[offset:new_offset].decode("utf-8"), new_offset
120123

121-
_type_decoder: ClassVar = {
124+
_type_decoder: ClassVar[dict[int, DecoderFunc]] = {
122125
1: _decode_pointer,
123126
2: _decode_utf8_string,
124127
3: _decode_double,

maxminddb/types.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66

77
Primitive: TypeAlias = AnyStr | bool | float | int
88

9+
RecordList: TypeAlias = list["Record"]
10+
"""RecordList is a type for lists in a database record."""
911

10-
class RecordList(list["Record"]):
11-
"""RecordList is a type for lists in a database record."""
12-
13-
14-
class RecordDict(dict[str, "Record"]):
15-
"""RecordDict is a type for dicts in a database record."""
16-
12+
RecordDict: TypeAlias = dict[str, "Record"]
13+
"""RecordDict is a type for dicts in a database record."""
1714

1815
Record: TypeAlias = Primitive | RecordList | RecordDict

0 commit comments

Comments
 (0)