Repository navigation
fix: enhance model list retrieval to fallback on built-in models when dynamic list is empty #7929
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # -*- coding: utf-8 -*- | ||
| from django.test import SimpleTestCase | ||
|
|
||
| from models_provider.impl.tencent_model_provider.tencent_model_provider import TencentModelProvider | ||
|
|
||
|
|
||
| class TencentModelProviderTests(SimpleTestCase): | ||
| @classmethod | ||
| def setUpClass(cls): | ||
| super().setUpClass() | ||
| cls.provider = TencentModelProvider() | ||
|
|
||
| def test_stt_model_type_is_exposed(self): | ||
| model_types = {item.get("value") for item in self.provider.get_model_type_list()} | ||
| self.assertIn("STT", model_types) | ||
|
|
||
| def test_stt_default_credential_without_model_name(self): | ||
| # model_form 在未选择基础模型(model_name 为空)时应返回默认 STT 凭证, | ||
| # 否则 create-model 流程会以“模型不支持”报 500。 | ||
| credential = self.provider.get_model_credential("STT", "") | ||
| self.assertIsNotNone(credential) | ||
| self.assertTrue(credential.to_form_list()) | ||
|
|
||
| def test_wand_asr_v1_uses_tokenhub_credential(self): | ||
| from models_provider.impl.tencent_model_provider.credential.tokenhub_stt import ( | ||
| TencentTokenhubSTTModelCredential, | ||
| ) | ||
|
|
||
| credential = self.provider.get_model_credential("STT", "wand-asr-v1") | ||
| self.assertIsInstance(credential, TencentTokenhubSTTModelCredential) | ||
| fields = {item.get("field") for item in credential.to_form_list()} | ||
| self.assertIn("api_key", fields) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Overall, the added tests are syntactically fine and cover the intended Tencent STT credential paths. Main concerns are test isolation, strictness of assertions, and Django test class choice. Potential issues / suggestions
Suggested refactor sketch# -*- coding: utf-8 -*-
from django.test import SimpleTestCase
from models_provider.impl.tencent_model_provider.tencent_model_provider import TencentModelProvider
from models_provider.impl.tencent_model_provider.credential.tokenhub_stt import (
TencentTokenhubSTTModelCredential,
)
class TencentModelProviderTests(SimpleTestCase):
def setUp(self):
self.provider = TencentModelProvider()
def test_stt_model_type_is_exposed(self):
model_types = {item.get("value") for item in self.provider.get_model_type_list()}
self.assertIn("STT", model_types)
def test_stt_default_credential_with_empty_model_name(self):
credential = self.provider.get_model_credential("STT", "")
self.assertIsNotNone(credential)
# Assert the expected default credential type as well.
self.assertTrue(credential.to_form_list())
def test_wand_asr_v1_uses_tokenhub_credential(self):
credential = self.provider.get_model_credential("STT", "wand-asr-v1")
self.assertIsNotNone(credential)
self.assertIsInstance(credential, TencentTokenhubSTTModelCredential)
fields = {item["field"] for item in credential.to_form_list()}
self.assertIn("api_key", fields)No blocking syntax issues were found. The most important checks are whether |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at this diff, here are the observations:
Potential Issues
Behavior change / semantic shift: Previously, when
manage is not None, the function always returned the dynamic list (possibly empty) immediately. Now it falls through to the builtin list whendynamic_listis empty andallow_builtin_fallbackis True. This changes behavior for existing callers and should be verified against intended contract.Control flow asymmetry between branches:
if manage is not None:branch does NOT return[]whennot allow_builtin_fallback— instead it returnsdynamic_list(which may be empty), effectively matching. That's fine.elif not allow_builtin_fallback:returns[]only whenmanage is None. But ifmanage is Noneandallow_builtin_fallbackis True, it silently falls through to builtin. Confirm that's intended — a failing/absent dynamic manage will now silently use builtins even when the caller asked for "no fallback" indirectly via the credential path. Slight inconsistency:not allow_builtin_fallbacksemantics differ depending on whethermanagewas constructed.Comment language/style: The inline comment is in Chinese while the file/answers convention appears consistent — verify project style guide (some repos require English or i18n comments). Also the comment says "交由视图继续走缓存/内置兜底" which describes caller behavior — this couples the helper to view-layer decisions; consider documenting in docstring instead.
Redundant nested check:
if dynamic_list or not allow_builtin_fallback: return dynamic_listfollowed by falling out of theif manage is not Noneblock is correct, but the readability could be improved with an earlyreturn []explicitly for the fallthrough-non-fallback case to make intent obvious.Optimization / Readability Suggestions
builtin = self.get_model_info_manage().get_model_list_by_model_type(model_type)once for reuse.allow_builtin_fallbackprecisely (does it apply whenmanage is None? when dynamic list is empty?).manage present/absent×allow_builtin_fallback True/False, plusdynamic_list empty/non-empty.Minor
get_model_list_by_model_type— ensure it's guaranteed to be list-like (orshort-circuit depends on truthiness;Nonewould behave as empty, likely fine but worth confirming).credential=Nonetruncated; confirm signature unchanged elsewhere.