Skip to content

fix: Make template override arg work correctly#522

Open
moehanabi wants to merge 1 commit intosgl-project:mainfrom
moehanabi:fix_template_assert
Open

fix: Make template override arg work correctly#522
moehanabi wants to merge 1 commit intosgl-project:mainfrom
moehanabi:fix_template_assert

Conversation

@moehanabi
Copy link
Copy Markdown
Contributor

@moehanabi moehanabi commented Apr 1, 2026

Motivation

not override and name not in self.templates make override arg work wrong

Modifications

Related Issues

Accuracy Test

Benchmark & Profiling

Checklist

Copilot AI review requested due to automatic review settings April 1, 2026 08:08
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines 61 to 63
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"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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")

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines 61 to 64
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
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants