Skip to content

Commit bb8eda1

Browse files
committed
Implement reset_rank class method to set rank to 0
1 parent 31c2391 commit bb8eda1

2 files changed

Lines changed: 6 additions & 12 deletions

File tree

src/docbuild/models/manifest.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,10 @@ def _increment_rank() -> int:
118118
rank: int = Field(default_factory=_increment_rank)
119119
translations: list[CategoryTranslation] = Field(default_factory=list)
120120

121-
# @model_validator(mode="before")
122-
# @classmethod
123-
# def prevent_manual_rank(cls, data: Any) -> Any:
124-
# """Prevent setting the rank attribute as it is set automatically."""
125-
# # If 'rank' is in the input data, it means the user tried to set it manually
126-
# if isinstance(data, dict) and "rank" in data:
127-
# raise ValueError(
128-
# "The 'rank' field is automatic and cannot be set manually."
129-
# )
130-
# return data
121+
@classmethod
122+
def reset_rank(cls: type[Self]) -> None:
123+
"""Reset the rank counter."""
124+
cls._current_rank = 0
131125

132126
@classmethod
133127
def from_xml_node(

tests/models/test_manifest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def test_category_from_xml_node() -> None:
160160
"""
161161
node = etree.fromstring(doc, parser=None)
162162
# Reset class variable for predictable rank
163-
Category._current_rank = 0
163+
Category.reset_rank()
164164
models = list(Category.from_xml_node(node))
165165

166166
assert len(models) == 4
@@ -181,7 +181,7 @@ def test_category_from_xml_node() -> None:
181181

182182
def test_category_rank() -> None:
183183
# Just to be sure, we reset the current rank:
184-
Category._current_rank = 0
184+
Category.reset_rank()
185185
for idx, i in enumerate(["A", "B", "C"], 1):
186186
cat = Category(id=i, translations=[])
187187
serizalized = cat.model_dump()

0 commit comments

Comments
 (0)