Skip to content

Commit e77182d

Browse files
oschwaldclaude
andcommitted
Make Metadata class readonly/immutable
Add frozen=True to the Metadata dataclass to make all attributes readonly after creation. This prevents accidental modification of metadata that represents the immutable properties of a database file. Changes: - Added frozen=True to @DataClass decorator - Updated HISTORY.rst to document the breaking change - Noted that C extension Metadata has always been readonly Benefits: - Consistent behavior between pure Python and C extension - Prevents bugs from accidental metadata modification - Better represents the immutable nature of database metadata Breaking change: Attempting to modify Metadata attributes after creation will now raise an AttributeError. This brings the pure Python implementation into consistency with the C extension, which has always had readonly attributes. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 6c7f4c3 commit e77182d

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

HISTORY.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ History
2323
instances, and future-proofing for Python 3.14's InterpreterPoolExecutor.
2424
Requested by R. Christian McDonald in GitHub #105.
2525
* **BREAKING**: The pure Python ``maxminddb.reader.Metadata`` class has been
26-
converted to a dataclass. The ``__repr__`` format has changed from
27-
``maxminddb.reader.Metadata(...)`` to ``Metadata(...)``. If you depend on
28-
the exact string representation of the Metadata object, you will need to
29-
update your code. All functionality remains the same, including the
30-
``node_byte_size`` and ``search_tree_size`` properties. The C extension's
31-
Metadata class is unchanged.
26+
converted to a frozen dataclass. The ``__repr__`` format has changed from
27+
``maxminddb.reader.Metadata(...)`` to ``Metadata(...)``. More importantly,
28+
all Metadata attributes are now readonly and cannot be modified after
29+
creation. If you were modifying metadata attributes after object creation,
30+
you will need to update your code. All functionality remains the same,
31+
including the ``node_byte_size`` and ``search_tree_size`` properties. Note:
32+
The C extension's Metadata class has always been readonly, so this change
33+
brings the pure Python implementation into consistency with the C extension.
3234

3335
2.8.2 (2025-07-25)
3436
++++++++++++++++++

maxminddb/reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ def __enter__(self) -> Self:
308308
return self
309309

310310

311-
@dataclass(kw_only=True)
311+
@dataclass(kw_only=True, frozen=True)
312312
class Metadata:
313313
"""Metadata for the MaxMind DB reader."""
314314

0 commit comments

Comments
 (0)