Skip to content

Commit cfd6eb3

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 <[email protected]>
1 parent cc49221 commit cfd6eb3

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
lines changed

maxminddb/decoder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
from __future__ import annotations
44

55
import struct
6+
from collections.abc import Callable
67
from typing import TYPE_CHECKING, ClassVar, cast
78

89
try:
910
import mmap
1011
except ImportError:
1112
mmap = None # type: ignore[assignment]
1213

13-
1414
from maxminddb.errors import InvalidDatabaseError
1515

1616
if TYPE_CHECKING:
@@ -118,7 +118,7 @@ def _decode_utf8_string(self, size: int, offset: int) -> tuple[str, int]:
118118
new_offset = offset + size
119119
return self._buffer[offset:new_offset].decode("utf-8"), new_offset
120120

121-
_type_decoder: ClassVar = {
121+
_type_decoder: ClassVar[dict[int, Callable[[Decoder, int, int], tuple[Record, int]]]] = {
122122
1: _decode_pointer,
123123
2: _decode_utf8_string,
124124
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)