Skip to content

Commit 3bc75db

Browse files
oschwaldclaude
andcommitted
Convert MODE constants to IntEnum
Convert the MODE_* constants to an IntEnum class for better type safety, IDE support, and more descriptive string representations. Changes: - Created Mode IntEnum with AUTO, MMAP_EXT, MMAP, FILE, MEMORY, FD members - Added comprehensive documentation for the Mode class and each member - Maintained backward compatibility by exporting old-style constants (MODE_AUTO, MODE_FILE, etc.) as aliases to enum members - Added __all__ to explicitly define the public API Benefits: - Better IDE autocomplete and type hints - Descriptive repr: <Mode.FILE: 4> instead of just 4 - Access to .name and .value attributes - Full backward compatibility (IntEnum is int-compatible) - Modern Python 3.10+ idiom - Inline documentation for each mode visible in source and IDEs Backward compatibility: All existing code continues to work unchanged. Both Mode.FILE and MODE_FILE can be used interchangeably, and all numeric comparisons work as before since IntEnum is int-compatible. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 2a2f6d4 commit 3bc75db

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

HISTORY.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ History
3131
including the ``node_byte_size`` and ``search_tree_size`` properties. Note:
3232
The C extension's Metadata class has always been readonly, so this change
3333
brings the pure Python implementation into consistency with the C extension.
34+
* MODE constants have been converted to an ``IntEnum`` (``maxminddb.const.Mode``).
35+
The old constants (``MODE_AUTO``, ``MODE_FILE``, etc.) remain available for
36+
backward compatibility and are now aliases to the enum members. This provides
37+
better IDE support and type safety while maintaining full backward
38+
compatibility. You can now use either ``Mode.FILE`` or ``MODE_FILE`` - both
39+
work identically. Since ``IntEnum`` is int-compatible, existing code using
40+
the constants will continue to work without modification.
3441

3542
2.8.2 (2025-07-25)
3643
++++++++++++++++++

maxminddb/const.py

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,47 @@
11
"""Constants used in the API."""
22

3-
MODE_AUTO = 0
4-
MODE_MMAP_EXT = 1
5-
MODE_MMAP = 2
6-
MODE_FILE = 4
7-
MODE_MEMORY = 8
8-
MODE_FD = 16
3+
from enum import IntEnum
4+
5+
6+
class Mode(IntEnum):
7+
"""Database open modes.
8+
9+
These modes control how the MaxMind DB file is opened and read.
10+
"""
11+
12+
AUTO = 0
13+
"""Try MODE_MMAP_EXT, MODE_MMAP, MODE_FILE in that order. Default mode."""
14+
15+
MMAP_EXT = 1
16+
"""Use the C extension with memory map."""
17+
18+
MMAP = 2
19+
"""Read from memory map. Pure Python."""
20+
21+
FILE = 4
22+
"""Read database as standard file. Pure Python."""
23+
24+
MEMORY = 8
25+
"""Load database into memory. Pure Python."""
26+
27+
FD = 16
28+
"""Database is a file descriptor, not a path. This mode implies MODE_MEMORY."""
29+
30+
31+
# Backward compatibility: export both enum members and old-style constants
32+
MODE_AUTO = Mode.AUTO
33+
MODE_MMAP_EXT = Mode.MMAP_EXT
34+
MODE_MMAP = Mode.MMAP
35+
MODE_FILE = Mode.FILE
36+
MODE_MEMORY = Mode.MEMORY
37+
MODE_FD = Mode.FD
38+
39+
__all__ = [
40+
"MODE_AUTO",
41+
"MODE_FD",
42+
"MODE_FILE",
43+
"MODE_MEMORY",
44+
"MODE_MMAP",
45+
"MODE_MMAP_EXT",
46+
"Mode",
47+
]

0 commit comments

Comments
 (0)