Skip to content

test(api): bring the model config converter tests under strict pyrefly - #46357

Open
MindOfqc wants to merge 10 commits into
langgenius:mainfrom
MindOfqc:test/pyrefly-model-config-converter
Open

MindOfqc wants to merge 10 commits into
langgenius:mainfrom
MindOfqc:test/pyrefly-model-config-converter

Conversation

@MindOfqc

@MindOfqc MindOfqc commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Bring the model-config converter tests under strict Pyrefly and remove the test file's exclusion. Keep the credential and completion-parameter shapes in production code.
  • Allow the converter to accept a resolved provider model bundle while preserving its normal tenant-scoped resolution when no bundle is supplied. Exercise real provider configuration, LLM, plugin runtime, and client objects, with HTTP and Redis I/O kept offline. Cover a returned schema and an empty-stream miss.
  • Validate provider configuration through named, typed fields and pass the snapshot registry directly as Pydantic context. Preserve the default module registry, per-instance snapshots before normalization, and first-write behavior for each provider.

Validation

  • 118 focused tests passed across converter, provider configuration, and plugin runtime on a0542b34.
  • The registry regression fails when the injected context is ignored; the generated ProviderConfiguration JSON schema is byte-for-byte unchanged.
  • Strict Pyrefly is clean on the converter test file. The production file has the same 35 diagnostics as the previous head when checked separately with the strict unit-test configuration; none were added.
  • Ruff lint and format, and the no-new-casts, constructor-bypass, and test-double guards passed.

Annotate the fixtures, the `_factory` helper, and the twelve test functions so
`core/app/app_config/easy_ui_based_app/model_config/test_converter.py` can leave
the strict-mode exclusion list. Fixtures are annotated with what they actually
return (`SimpleNamespace`, `MagicMock`) instead of `None`, and the parametrised
expectations use the real `ModelStatus` enum and `type[Exception]`. No test
logic, assertion, or expected value is touched.

Verification:
- project-wide `pyrefly check` on `tests/unit_tests/pyrefly.toml`: 29 errors,
  all `missing-import` from packages absent in the local venv, zero warnings
  (`min-severity = "warn"`, so a warning alone fails `make type-check-core`).
- target file reports zero diagnostics.
- `pytest` on the file: 13 passed before and after.
- `ruff check` and `ruff format --check` pass.
@github-actions github-actions Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Nov 14, 2026
@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 88.03% 88.06% +0.03%
Strict coverage 87.67% 87.70% +0.03%
Typed symbols 98,662 98,685 +23
Untyped symbols 13,470 13,435 -35
Modules 4713 4713 0

@codecov

codecov Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.62%. Comparing base (b2cfc31) to head (a331a97).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #46357      +/-   ##
==========================================
- Coverage   90.62%   90.62%   -0.01%     
==========================================
  Files        6665     6665              
  Lines      415303   415317      +14     
  Branches    83292    83293       +1     
==========================================
+ Hits       376363   376369       +6     
- Misses      32556    32563       +7     
- Partials     6384     6385       +1     
Flag Coverage Δ
api 90.34% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.


def _factory(**kwargs):
def _factory(**kwargs: object) -> SimpleNamespace:
return SimpleNamespace(**kwargs)

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.

real class ?

@@ -63,14 +63,14 @@ def mock_provider_bundle(self):
# model type instance
model_type_instance = MagicMock()
model_schema = MagicMock()

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.

.

status: ModelStatus,
expected_exception: type[Exception],
) -> None:
mock_provider = MagicMock()

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.

.

@asukaminato0721 asukaminato0721 self-assigned this Nov 15, 2026
``ModelConfigConverter.convert`` resolved the tenant's provider graph itself, so any caller that
already held the resolved bundle had to re-resolve it, and the conversion could not be exercised
against real entities without patching ``create_plugin_provider_manager``.

Accept an optional ``provider_model_bundle`` and only fall back to resolving it from the tenant when
it is omitted; production callers are unchanged. The bundle's model type instance is typed by
``AIModel``, which already declares ``get_model_schema``, so the ``cast`` to ``LargeLanguageModel``
is dropped as well.

With the bundle injectable, the unit tests build the real ``EasyUIBasedAppConfig``,
``ProviderConfiguration``, ``LargeLanguageModel`` and ``ModelConfigWithCredentialsEntity`` instead of
``MagicMock``/``SimpleNamespace`` stand-ins, which also surfaced that the reported provider is the
full provider id rather than the bare provider name.
@github-actions github-actions Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Nov 15, 2026

@asukaminato0721 asukaminato0721 left a comment •

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.

2 inline suggestions at 639ff8024a, each from a rule learned from earlier reviews in this repository. Reply on a comment if it is wrong.

dify-codereview

@asukaminato0721 asukaminato0721 left a comment

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.

2 inline suggestions at 5bed6560ab, each from a rule learned from earlier reviews in this repository. Reply on a comment if it is wrong.

dify-codereview

provider_model_bundle=bundle,
)

expected_stop: list[str] = parameters.get("stop", [])

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.

Parse, don't validate (https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/): model this data once at the boundary as a pydantic model / dataclass / TypedDict (Model.model_validate(raw)), and pass the typed value on, instead of passing dicts around and re-checking keys and types where they are used. In a test, don't build this as an ad hoc dict[str, object]: define the shape (TypedDict / model) in the production code and import it here.

dify-codereview · parse-dont-validate Learned from an earlier review. Reply here if this is wrong.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in the production-owned shape: api/core/app/app_config/easy_ui_based_app/model_config/converter.py:22-32 defines CompletionParameters as a TypedDict (including provider-specific extra_items=object), and the test imports it at test_converter.py:19-23 and annotates the parameterized input at line 365. This is not an ad hoc dict[str, object] declaration in the test. I have also requested re-review of the updated head.

asukaminato0721

This comment was marked as outdated.

@asukaminato0721 asukaminato0721 added the waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address label Nov 16, 2026
Move the ProviderCredentials and CompletionParameters TypedDicts from the test
module into core/app/app_config/easy_ui_based_app/model_config/converter.py so
tests import the production-owned payload shapes (parse-don't-validate).
CompletionParameters uses extra_items=object so provider-specific parameters
pass through unchanged. Also replace the ad hoc dict[str, object]() registry
stand-in with the real registry type dict[str, list[ConfigurateMethod]]().

@asukaminato0721 asukaminato0721 left a comment

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.

1 inline suggestion at b11aa47ac4, from a rule learned from earlier reviews in this repository. Reply on the comment if it is wrong. 4 review threads are still unresolved.

dify-codereview

@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 16, 2026
@github-actions github-actions Bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Nov 19, 2026
@MindOfqc

Copy link
Copy Markdown
Contributor Author

I addressed the independent review finding in 84a1d11e716e958533e3dbd0c04860005d76a485: the stop sentinel in api/tests/unit_tests/core/app/app_config/easy_ui_based_app/model_config/test_converter.py:252,257 is restored to a true newline literal; no production code changed. Independent verification on the final head: target tests 14 passed/5 warnings, Ruff and format passed, strict target Pyrefly reported 0 diagnostics, and full review found no remaining code blocker. The quality gate's only blocking line is its false positive on the generated standard merge-commit subject; it also reports a 569-line size warning and one commit behind main, whose latest change is in unrelated web paths. The full diff has been independently reviewed. Could you please re-review this updated head?

@asukaminato0721 asukaminato0721 left a comment

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.

3 inline suggestions at 84a1d11e71, each from a rule learned from earlier reviews in this repository. Reply on a comment if it is wrong. 4 review threads are still unresolved.

dify-codereview

@asukaminato0721 asukaminato0721 removed the waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address label Nov 19, 2026
@asukaminato0721 asukaminato0721 added the waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address label Nov 19, 2026
@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 26, 2026
@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 27, 2026
asukaminato0721

This comment was marked as outdated.

@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up to the conflict review: merged current main into this branch at a331a971 without rewriting history. The converter keeps upstream's model-route binding as well as the existing typed bundle and registry behavior. GitHub now reports the branch mergeable. Locally, 124 converter, provider-configuration, and plugin-runtime tests passed; Ruff and the converter's strict Pyrefly check passed. CI is being checked on the new head.

@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XL This PR changes 500-999 lines, ignoring generated files. waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants