fix: Make template override arg work correctly#522
fix: Make template override arg work correctly#522moehanabi wants to merge 1 commit intosgl-project:mainfrom
Conversation
There was a problem hiding this comment.
Code Review
This pull request corrects a logic error in the register method of the template registry, ensuring that templates can be correctly overridden when the override flag is set. The feedback suggests replacing the assert statement with a more robust if check and raising a ValueError to prevent the validation from being bypassed in optimized Python environments.
| assert ( | ||
| not override and name not in self.templates | ||
| override or name not in self.templates | ||
| ), f"Chat template for the model type {name} has already been registered" |
There was a problem hiding this comment.
Using assert for runtime validation is generally discouraged in Python because assertions can be globally disabled (e.g., via the -O flag), which would bypass this safety check. Since this registry logic is important for preventing accidental template overwrites, using an explicit if statement with a ValueError is more robust and follows standard Python practices for argument validation.
if not override and name in self.templates:
raise ValueError(f"Chat template for the model type {name} has already been registered")There was a problem hiding this comment.
Pull request overview
Fixes the TemplateRegistry.register(..., override=True) logic so callers can correctly replace an existing template entry without triggering the duplicate-registration assertion.
Changes:
- Corrected the duplicate-registration assertion to allow overrides when
override=True.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| assert ( | ||
| not override and name not in self.templates | ||
| override or name not in self.templates | ||
| ), f"Chat template for the model type {name} has already been registered" | ||
| self.templates[name] = template |
There was a problem hiding this comment.
This change fixes override=True, but there’s no unit test asserting that register(..., override=True) can overwrite an existing key (and that override=False rejects duplicates). Adding a small test would prevent regressions in this control-flow.
Motivation
not override and name not in self.templatesmake override arg work wrongModifications
Related Issues
Accuracy Test
Benchmark & Profiling
Checklist