Skip to content

test(api): annotate quota, plugin factory and cleaner tests for strict pyrefly - #45766

Merged
asukaminato0721 merged 6 commits into
langgenius:mainfrom
shi-felix:pyrefly-strict-20261107-03
Nov 8, 2026
Merged

asukaminato0721 merged 6 commits into
langgenius:mainfrom
shi-felix:pyrefly-strict-20261107-03

Conversation

@shi-felix

@shi-felix shi-felix commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Continue the strict-Pyrefly rollout for the api test suite. This batch brings 3 test
module(s) back under strict checking (part of #24645).

What changed

  • Add the missing return annotations (-> None, or -> object for helpers that return a
    value) to the test functions in:
    • api/tests/unit_tests/core/app/llm/test_quota.py
    • api/tests/unit_tests/core/plugin/impl/test_model_runtime_factory.py
    • api/tests/unit_tests/core/rag/cleaner/test_clean_processor.py
  • Remove the corresponding 3 entries from api/tests/unit_tests/pyrefly.toml.

No production code changes and no runtime behaviour changes.

  • Add line-level # pyrefly: ignore comments on the exact lines where CI's strict check
    (which installs the full dev dependency set) still reports pre-existing diagnostics in
    these modules. Those diagnostics were hidden while the files sat on the exclude list;
    suppressing them at the reported lines keeps the strict diagnostic set unchanged once
    the excludes are removed. No runtime behaviour changes.

Verification

  • pyrefly check <file> -c pyrefly.toml --summary=none (pyrefly 1.4.0, Python 3.13):
    0 diagnostics for every file in this batch.
  • Full strict check with the same config after removing the excludes: the diagnostic set
    is unchanged from the base-line (0 new diagnostics).
  • ruff format --check clean on all touched files.

The local interpreter does not have all of dify's third-party packages installed, so
missing-import diagnostics are environmental and were excluded from the comparison;
CI installs the full dev dependency set.

  • CI Style Check / Python Style (make type-check-core): strict checks pass with the
    excludes removed (follow-up commits address the diagnostics reported on the first run).

…t pyrefly

Files: api/tests/unit_tests/core/app/llm/test_quota.py, api/tests/unit_tests/core/plugin/impl/test_model_runtime_factory.py, api/tests/unit_tests/core/rag/cleaner/test_clean_processor.py
Added 24 return annotations; dropped 3 pyrefly excludes.
Verified: pyrefly 1.4.0 strict single-file check 0 diagnostics, full strict check unchanged, ruff format --check clean.
@github-actions github-actions Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Nov 7, 2026
@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 84.33% 84.35% +0.02%
Strict coverage 83.99% 84.01% +0.02%
Typed symbols 88,159 88,184 +25
Untyped symbols 16,447 16,425 -22
Modules 4426 4426 0

@codecov

codecov Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.02%. Comparing base (f73b180) to head (a8bc43a).
⚠️ Report is 22 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #45766   +/-   ##
=======================================
  Coverage   90.01%   90.02%           
=======================================
  Files        6265     6265           
  Lines      391789   391831   +42     
  Branches    78435    78439    +4     
=======================================
+ Hits       352679   352731   +52     
+ Misses      33020    33011    -9     
+ Partials     6090     6089    -1     
Flag Coverage Δ
api 89.81% <ø> (+<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.

…-enabled tests

The full-dependency CI strict check reports type diagnostics that already existed in these modules while they were on the exclude list. Mark those exact lines so removing the excludes does not change the diagnostic set. Adds 7 line-level pyrefly suppressions across 2 files. No runtime behaviour changes.
@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-11-08 11:18:29.348471226 +0000
+++ /tmp/pyrefly_pr.txt	2026-11-08 11:18:18.024436128 +0000
@@ -3782,16 +3782,6 @@
    --> tests/unit_tests/core/app/layers/test_pause_state_persist_layer.py:157:12
 ERROR Argument `list[FromClause]` is not assignable to parameter `tables` with type `Sequence[Table] | None` in function `sqlalchemy.sql.schema.MetaData.create_all` [bad-argument-type]
   --> tests/unit_tests/core/app/layers/test_trigger_post_layer.py:33:66
-ERROR Object of class `FromClause` has no attribute `insert` [missing-attribute]
-   --> tests/unit_tests/core/app/llm/test_quota.py:508:13
-ERROR Object of class `FromClause` has no attribute `insert` [missing-attribute]
-   --> tests/unit_tests/core/app/llm/test_quota.py:732:13
-ERROR No matching overload found for function `dict.__init__` called with arguments: (Sequence[Row[tuple[str, int | None]]]) [no-matching-overload]
-   --> tests/unit_tests/core/app/llm/test_quota.py:789:32
-ERROR Object of class `FromClause` has no attribute `update` [missing-attribute]
-   --> tests/unit_tests/core/app/llm/test_quota.py:800:13
-ERROR Object of class `FromClause` has no attribute `insert` [missing-attribute]
-   --> tests/unit_tests/core/app/llm/test_quota.py:846:13
 ERROR Argument `Literal[core.app.entities.app_invoke_entities.InvokeFrom.WEB_APP]` is not assignable to parameter `invoke_from` with type `models.enums.InvokeFrom | SQLCoreOperations[models.enums.InvokeFrom | None] | None` in function `models.model.Message.__init__` [bad-argument-type]
   --> tests/unit_tests/core/app/task_pipeline/test_based_generate_task_pipeline.py:40:21
 ERROR `SimpleNamespace` is not assignable to upper bound `AppGenerateEntity` of type variable `AppGenerateEntityT` [bad-specialization]
@@ -4046,10 +4036,6 @@
    --> tests/unit_tests/core/plugin/impl/test_model.py:637:22
 ERROR Key `type` is not defined in TypedDict `MultimodalRerankInput` [bad-typed-dict-key]
    --> tests/unit_tests/core/plugin/impl/test_model.py:637:23
-ERROR Argument `_FakeModelRuntime` is not assignable to parameter `runtime` with type `ModelRuntime` in function `core.plugin.impl.model_runtime_factory.create_model_type_instance` [bad-argument-type]
-   --> tests/unit_tests/core/plugin/impl/test_model_runtime_factory.py:408:17
-ERROR Argument `_FakeModelRuntime` is not assignable to parameter `runtime` with type `ModelRuntime` in function `core.plugin.impl.model_runtime_factory.create_model_type_instance` [bad-argument-type]
-   --> tests/unit_tests/core/plugin/impl/test_model_runtime_factory.py:429:21
 ERROR No matching overload found for function `typing.MutableMapping.update` called with arguments: (object) [no-matching-overload]
   --> tests/unit_tests/core/plugin/impl/test_oauth.py:72:33
 ERROR Object of class `NoneType` has no attribute `model` [missing-attribute]

@asukaminato0721 asukaminato0721 self-assigned this Nov 7, 2026
Replace the bare `# pyrefly: ignore` comments with code-specific suppressions, using the exact error kinds reported by the strict-pyrefly CI check (check-run annotation titles), so each comment silences only the diagnostic it was added for.

7 lines across 2 files: bad-argument-type x2, missing-attribute x4, no-matching-overload x1.
No runtime behaviour changes; the strict diagnostic set is unchanged.
Clear five of the seven `# pyrefly: ignore` comments in this module.

`Model.__table__` access: SQLAlchemy never declares `__table__` in its
type layer, so `Provider.__table__.insert()` cannot type-check. The
`insert()` / `update()` constructors accept an ORM entity directly —
their parameter is `_DMLTableArgument`, which includes `Type[Any]` — so
the three inserts and the one update now pass the mapped class itself:

- `insert(TenantCreditPool)` / `insert(Provider)`
- `update(Provider).where(...).values(...)`

Row to dict: `dict(result.all())` tripped `no-matching-overload` because
`Row` is not a `tuple` to the checker. `result.tuples().all()` is the
existing idiom in this repository (three uses in
`tests/unit_tests/services/rbac/test___init__.py`), and dify pins
SQLAlchemy 2.0.49, where `Result.tuples()` is not deprecated.

I ran both forms against a real SQLite engine with SQLAlchemy 2.1.8
before pushing: the insert, the update and the dict conversion all return
the expected rows (`{'a': 13, 'b': 20}`), i.e. same behaviour as before.

The remaining two suppressions in
`core/plugin/impl/test_model_runtime_factory.py` are not touched here:
`create_model_type_instance` takes the aggregated `ModelRuntime`
protocol, whose members are `@abstractmethod`, so `_FakeModelRuntime`
cannot satisfy it without implementing all eleven capabilities.

Verified locally: `python -m py_compile`; `ruff format --check` clean;
`ruff check` error set identical to the PR head baseline.
Replace the Core `insert(Model)` / `update(Model)` statements with ORM
entities added to the `sqlite_session` fixture, which is bound to the same
SQLite engine as `sqlite_engine`:

- `TenantCreditPool(...)` / `Provider(...)` build the rows. `id` is a
  mapped column declared `init=False`, so the fixed ids are assigned after
  construction -- the same pattern already used in
  `commands/test_system.py` and `controllers/console/app/test_message.py`.
- The follow-up `update(Provider)` becomes attribute assignment plus a
  commit.
- `quota_type` now passes `models.enums.ProviderQuotaType`, the enum the
  column is declared with, instead of the differently-typed
  `core.entities.ProviderQuotaType`. Both members carry the same `"free"`
  value, so the stored value is unchanged.

`_session` is annotated `-> Session` rather than `-> object`; it returns
`sessionmaker()`, whose type is known.

No behaviour change: the same rows are committed to the same engine before
the production code runs.

Verified locally: `python -m py_compile`; `ruff format --check` clean;
`ruff check` error set identical to the PR head baseline. The tests
themselves are exercised by CI (API Unit Tests).
@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 8, 2026
…unking`

hit the 20s `pytest-timeout` inside an SSL read on the previous run
(1 failed, 13621 passed). It is unrelated to this diff, and a fork PR
cannot re-run jobs, so this empty commit re-triggers the workflows.
This was referenced Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants