test(api): bring the billing service tests under strict pyrefly #46407
Pyrefly Diff
<details>
<summary>base → PR</summary>
--- /tmp/pyrefly_base.txt 2026-11-26 14:03:52.266339810 +0000
+++ /tmp/pyrefly_pr.txt 2026-11-26 14:03:41.254266297 +0000
@@ -5067,8 +5067,6 @@
--> tests/unit_tests/services/retention/workflow_run/test_restore_archived_workflow_run.py:760:16
ERROR `in` is not supported between `Literal['not found']` and `None` [not-iterable]
--> tests/unit_tests/services/retention/workflow_run/test_restore_archived_workflow_run.py:894:16
-ERROR Argument `str` is not assignable to parameter `freeze_type` with type `Literal['', 'email_domain_suspended', 'freeze']` in function `services.billing_client.saas.CheckAccountInFreezeReply.model_construct` [bad-argument-type]
- --> tests/unit_tests/services/test_billing_service.py:373:87
ERROR Argument `Literal['normal']` is not assignable to parameter `status` with type `ConversationStatus | SQLCoreOperations[ConversationStatus]` in function `models.model.Conversation.__init__` [bad-argument-type]
--> tests/unit_tests/services/test_clear_free_plan_tenant_expired_logs.py:117:16
ERROR Object of class `object` has no attribute `id` [missing-attribute]
</details>
Pyrefly Type Coverage
| Metric | Base | PR | Delta |
| --- | ---: | ---: | ---: |
| Type coverage | 87.92% | 87.94% | +0.03% |
| Strict coverage | 87.58% | 87.61% | +0.03% |
| Typed symbols | 97,241 | 97,282 | +41 |
| Untyped symbols | 13,415 | 13,389 | -26 |
| Modules | 4672 | 4672 | 0 |
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 90.52%. Comparing base (
df05396) to head (cb631cb).:warning: Report is 4 commits behind head on main.
<details><summary>Additional details and impacted files</summary>
@@ Coverage Diff @@
## main #46407 +/- ##
==========================================
- Coverage 90.53% 90.52% -0.01%
==========================================
Files 6608 6608
Lines 410585 410691 +106
Branches 82396 82394 -2
==========================================
+ Hits 371706 371781 +75
- Misses 32489 32512 +23
- Partials 6390 6398 +8
| Flag | Coverage Δ | |
|---|---|---|
| api | 90.20% <100.00%> (-0.02%) | :arrow_down: |
Flags with carried forward coverage won't be shown. Click here to find out more.
</details>
:umbrella: View full report in Codecov by Harness.
:loudspeaker: Have feedback on the report? Share it here.
<details><summary> :rocket: New features to boost your workflow: </summary>
</details>
can we just fix the origin code?
Requested changes.
Fixed at the origin as suggested — and since saas.py is generated, the fix belongs in the generator.
CheckAccountInFreezeReply.freeze_type is rendered from a proto enum, but the set is open: a dify-saas deployment older or newer than the committed snapshot can send a member the spec never listed. The only consumer, BillingService.get_email_freeze_type, already handles that with case _, so the literal did not protect anything — it only moved the failure from that fallback into pydantic, and transport.py logs validation failures to Sentry while salvaging with model_construct. So every reply from an older server was both noise and a validation bypass.
generate_billing_client.py now carries an explicit allowlist of open enum fields, and a check_open_enum_fields pass fails generation if an entry stops naming an enum field or vanishes from the snapshot. saas.py declares str, the snapshot JSON is untouched, and the _freeze_reply helper is gone — the test builds a real model through validation, including the "unexpected" case, which is the point of the change.
Verified: generate_billing_client.py --check exits 0, git diff --stat saas.py is non-empty, strict pyrefly stays at the 29 missing-import baseline with no new warnings, the production project is unchanged at its 3174/32 baseline, test_billing_service.py passes 30 and the billing suites pass 101, ruff is clean, and all seven check_no_new_* guards exit 0.
Rather than patching this in the test, change the production code to take the dependency as a parameter (pass the session / account / service in), so the test hands in a real one and needs no patch.
<sub>dify-codereview · tests-real-dependencies (inject-dependency) Learned from an earlier review. Reply here if this is wrong.</sub>
<!-- dify-review:rule=tests-real-dependencies;option=inject-dependency;key=f625a8b44eabc765 -->
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.
<sub>dify-codereview · parse-dont-validate Learned from an earlier review. Reply here if this is wrong.</sub>
<!-- dify-review:rule=parse-dont-validate;key=dab7a851a5a94507 -->
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.
<sub>dify-codereview · parse-dont-validate Learned from an earlier review. Reply here if this is wrong.</sub>
<!-- dify-review:rule=parse-dont-validate;key=a3985bb615d08f9d -->
3 inline suggestions at 13c3a9611c, each from a rule learned from earlier reviews in this repository. Reply on a comment if it is wrong.
<sub>dify-codereview</sub>
<!-- dify-review:head=13c3a9611c4003ed1332895a22b621d00569f424 -->
Nothing to flag at 086e07e11d.
<sub>dify-codereview</sub>
<!-- dify-review:head=086e07e11d9395f1c830e47823a943b77315268e -->
why this changed from literal?
Requested changes.
freezeType is an open proto enum, not a closed one. A dify-saas deployment older or newer than this committed spec snapshot can return a value the spec never listed. With Literal[...], pydantic would reject that reply with a ValidationError even though the consumer can still read and act on it — turning a harmless new enum value into a spurious error in Sentry.
The consumer of CheckAccountInFreezeReply.freeze_type already matches with a case _ fallback, so widening to str loses no safety. The change is made in the generator (api/dev/generate_billing_client.py), not by hand-editing the generated saas.py (api/AGENTS.md forbids editing generated files): a minimal OPEN_ENUM_FIELDS allowlist keyed by (schema, wire name) renders just this one field as str. The allowlist comment says to keep it minimal and to drop the entry once the upstream spec removes the enum.
Every other enum field stays Literal — only fields whose consumer already handles unknown values are widened.
This branch has conflicts with main: please rebase onto the latest main. Checked 086e07e11d.
<sub>dify-codereview</sub>
<!-- dify-review:head=086e07e11d9395f1c830e47823a943b77315268e;conflict=086e07e11d9395f1c830e47823a943b77315268e -->
This branch has conflicts with main: please rebase onto the latest main. Checked 8ff6a7d255.
<sub>dify-codereview</sub>
<!-- dify-review:head=8ff6a7d255bae62ac24bdc415129704999f337ea;conflict=8ff6a7d255bae62ac24bdc415129704999f337ea -->
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.
<sub>dify-codereview · parse-dont-validate Learned from an earlier review. Reply here if this is wrong.</sub>
<!-- dify-review:rule=parse-dont-validate;key=ac9d3fa06a7a24cd -->
1 inline suggestion at be40ea9da3, from a rule learned from earlier reviews in this repository. Reply on the comment if it is wrong. 1 review thread is still unresolved.
<sub>dify-codereview</sub>
<!-- dify-review:head=be40ea9da3fa714b830a2b802df971b9fc9500c7 -->
Updated in cb631cbe. The allowlist check now parses components.schemas as a typed envelope using the existing schema and property models. Invalid envelopes raise GenerationError, while rendering still uses the original JSON. All 59 focused tests passed, and --check confirmed the generated files are unchanged.
asukaminato0721
MindOfqc
Part of the pyrefly strict-mode migration: this file leaves
project-excludes.api/tests/unit_tests/pyrefly.tomlnow carries real annotations, and the remaining diagnostics it exposed were fixed rather than suppressed. Nopyrefly: ignore/type: ignore/cast/typing.Anywas added.Verification:
pyrefly checkontests/unit_tests/pyrefly.tomlreports 29 errors (all pre-existingmissing-importfor optional packages) and 0 warnings with 0 diagnostics left in the target file;pytestpasses match the pre-change baseline;ruff checkandruff format --checkpass.