Skip to content

test(api): add Pyrefly-clean unit tests for DocumentContext - #45996

Merged
asukaminato0721 merged 1 commit into
langgenius:mainfrom
TINGyu123644:test/add-document-context-tests-v2
Nov 11, 2026
Merged

asukaminato0721 merged 1 commit into
langgenius:mainfrom
TINGyu123644:test/add-document-context-tests-v2

Conversation

@TINGyu123644

Copy link
Copy Markdown
Contributor

Summary

Re-submission of #45760, which I closed after Style Check / Python Style failed on dify's strict pyrefly type-check-core. GitHub does not allow reopening a PR whose head branch was force-pushed, so the type-correct rewrite is submitted as a new PR instead.

Purely additive: one new test file covering the previously uncovered core.rag.entities.context_entities.DocumentContext — 15 cases, 0 deletions.

Why the negative cases use model_validate

dify type-checks the test tree as well. Constructing a pydantic model with missing or mistyped arguments is a static error there, even though the behaviour under test is a ValidationError at runtime. Negative cases now pass their payload through Model.model_validate({...}), which goes through the very same pydantic validation path but takes an unvalidated mapping, so it does not trip the type gate. No # type: ignore is used. Two extra cases assert that model_validate and the constructor produce equal models.

Verification (local, Python 3.13)

  • pyrefly 1.4.1 check with this repo's [tool.pyrefly] config: 10 errors -> 0 diagnostics
  • pytest: 29 passed across both rewritten modules
  • ruff check + ruff format --check with api/.ruff.toml: clean
  • Rebased onto current main (8fb0e237), LF line endings, additive-only

AI-assisted test generation; assertions reflect the documented behaviour of the target models.

Covers core.rag.entities.context_entities.DocumentContext with 15 cases.
Negative cases use model_validate() because dify's strict type-check-core
also checks the test tree: constructing a pydantic model with missing or
mistyped arguments is a static error, while model_validate() exercises the
same runtime validation path without tripping the gate.
Verified locally: pyrefly 1.4.1 -> 0 diagnostics, pytest -> 29 passed,
ruff check + format -> clean. Purely additive (1 new file, 0 deletions).
@github-actions github-actions Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Nov 10, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 84.90% 84.90% +0.00%
Strict coverage 84.56% 84.56% +0.00%
Typed symbols 89,399 89,413 +14
Untyped symbols 15,968 15,968 0
Modules 4454 4455 +1

@codecov

codecov Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.10%. Comparing base (8fb0e23) to head (1101e43).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #45996      +/-   ##
==========================================
- Coverage   90.10%   90.10%   -0.01%     
==========================================
  Files        6279     6279              
  Lines      392410   392410              
  Branches    78562    78562              
==========================================
- Hits       353596   353591       -5     
- Misses      32725    32731       +6     
+ Partials     6089     6088       -1     
Flag Coverage Δ
api 89.98% <ø> (+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.

@asukaminato0721
asukaminato0721 added this pull request to the merge queue Nov 10, 2026
@asukaminato0721
asukaminato0721 removed this pull request from the merge queue due to a manual request Nov 10, 2026
@asukaminato0721
asukaminato0721 self-requested a review November 10, 2026 13:12
@asukaminato0721

Copy link
Copy Markdown
Contributor

#45614

@asukaminato0721

Copy link
Copy Markdown
Contributor

pyrefly-org/pyrefly#3390 I impl the check for model_validate so now llm don't need to write such case.

@TINGyu123644

Copy link
Copy Markdown
Contributor Author

Thanks for the pointer to #45614 — I checked it: it bumps pyrefly from >=1.4.0 to >=1.5.0-dev1 and fixes the new diagnostics it surfaces.

I re-verified the two test modules in this PR against pyrefly 1.5.0-dev.1, using this repo's [tool.pyrefly] config and api/.ruff.toml:

  • pyrefly check: 0 diagnostics (the original pre-fix version still produces 10 errors under the same version)
  • pytest: 29 passed
  • ruff check + ruff format --check: clean

So no code change was needed here. I deliberately did not force-push, to avoid dismissing your approval.

Following the same type-check thread, I did find that the two sibling test PRs from this batch (#45922 and #45924) were actually failing CI: they imported from api.core.rag.entities... instead of from core.rag.entities..., which produced a Pyrefly missing-import error and broke pytest collection. Both are fixed and re-pushed — verified under 1.5.0-dev.1 as 12 diagnostics -> 0, 46 tests passed, ruff clean.

If you would prefer these two PRs to land after #45614 for merge-order reasons, just say so and I'll hold. Thanks again for the review.

@asukaminato0721
asukaminato0721 added this pull request to the merge queue Nov 11, 2026
Merged via the queue into langgenius:main with commit 21d4df0 Nov 11, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants