Skip to content

test(api): enable strict typing for jieba keyword table handler tests - #45320

Merged
asukaminato0721 merged 5 commits into
langgenius:mainfrom
lianbie:fix/jieba-strict-typing
Nov 4, 2026
Merged

asukaminato0721 merged 5 commits into
langgenius:mainfrom
lianbie:fix/jieba-strict-typing

Conversation

@lianbie

@lianbie lianbie commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Bring api/tests/unit_tests/core/rag/datasource/keyword/jieba/test_jieba_keyword_table_handler.py under strict Pyrefly checking and remove its entry from api/tests/unit_tests/pyrefly.toml, continuing #24645 (same pattern as #43883, #43875 and #43798).

The module was still excluded from strict checking. Strict Pyrefly (1.4.0) reports 19 diagnostics on it, all fixed here:

Diagnostic Count Fix
unannotated-return 10 -> None on the eight tests, _DummyTFIDF.__init__ and _install_fake_jieba_modules
implicit-any-parameter 2 monkeypatch: pytest.MonkeyPatch, **kwargs: object
missing-attribute 7 document the intentional fake-module attribute assignments with # pyrefly: ignore[missing-attribute]

The last group is by design: the tests install a fake jieba.analyse with types.ModuleType, then supply default_tfidf, TFIDF and analyse for the handler to discover. A ModuleType declares none of those, so the assignment is correct at runtime and the suppression states why. typing.Any is not used, per the banned-API rule.

No assertion, fixture, import list (apart from the dropped Any) or test behavior changes.

What changed

  • api/tests/unit_tests/core/rag/datasource/keyword/jieba/test_jieba_keyword_table_handler.py: annotations and the seven scoped suppressions above.
  • api/tests/unit_tests/pyrefly.toml: remove the single exclusion line.

Validation

  • pyrefly check --config=tests/unit_tests/pyrefly.toml --use-ignore-files=false --disable-project-excludes-heuristics=true tests/unit_tests/core/rag/datasource/keyword/jieba/test_jieba_keyword_table_handler.py: 19 diagnostics before, 0 after (pyrefly 1.4.0).
  • The eight tests in the module pass with the edited file.
  • ruff check and ruff format --check pass on the changed file.
  • No production code is touched, so no runtime behavior can change.

Contributes to #24645

Annotate the test declarations and the fake jieba module attributes so that
tests/unit_tests/core/rag/datasource/keyword/jieba/test_jieba_keyword_table_handler.py
passes strict pyrefly, then drop its entry from the unit-test exclusion list.

No assertion, fixture or test behavior changes; the seven
missing-attribute diagnostics come from assigning attributes on a plain
types.ModuleType used to impersonate jieba.analyse.

Verified with pyrefly 1.4.0: 19 diagnostics before, 0 after; the eight
tests in the module pass; ruff check and ruff format pass.

Contributes to langgenius#24645
@github-actions github-actions Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Nov 2, 2026
@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 83.19% 83.20% +0.01%
Strict coverage 82.84% 82.85% +0.01%
Typed symbols 85,935 85,943 +8
Untyped symbols 17,438 17,429 -9
Modules 4378 4378 0

@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-11-04 07:10:21.954836148 +0000
+++ /tmp/pyrefly_pr.txt	2026-11-04 07:10:10.482801959 +0000
@@ -4338,20 +4338,6 @@
   --> tests/unit_tests/core/rag/data_post_processor/test_data_post_processor.py:29:29
 ERROR Argument `dict[str, str]` is not assignable to parameter `reranking_model` with type `RerankingModelDict | None` in function `core.rag.data_post_processor.data_post_processor.DataPostProcessor._get_rerank_model_instance` [bad-argument-type]
    --> tests/unit_tests/core/rag/data_post_processor/test_data_post_processor.py:192:33
-ERROR Object of class `ModuleType` has no attribute `analyse` [missing-attribute]
-  --> tests/unit_tests/core/rag/datasource/keyword/jieba/test_jieba_keyword_table_handler.py:32:5
-ERROR Object of class `ModuleType` has no attribute `default_tfidf` [missing-attribute]
-  --> tests/unit_tests/core/rag/datasource/keyword/jieba/test_jieba_keyword_table_handler.py:46:5
-ERROR Object of class `ModuleType` has no attribute `default_tfidf` [missing-attribute]
-  --> tests/unit_tests/core/rag/datasource/keyword/jieba/test_jieba_keyword_table_handler.py:58:5
-ERROR Object of class `ModuleType` has no attribute `TFIDF` [missing-attribute]
-  --> tests/unit_tests/core/rag/datasource/keyword/jieba/test_jieba_keyword_table_handler.py:63:5
-ERROR Object of class `ModuleType` has no attribute `default_tfidf` [missing-attribute]
-  --> tests/unit_tests/core/rag/datasource/keyword/jieba/test_jieba_keyword_table_handler.py:74:5
-ERROR Object of class `ModuleType` has no attribute `TFIDF` [missing-attribute]
-  --> tests/unit_tests/core/rag/datasource/keyword/jieba/test_jieba_keyword_table_handler.py:81:5
-ERROR Object of class `ModuleType` has no attribute `default_tfidf` [missing-attribute]
-  --> tests/unit_tests/core/rag/datasource/keyword/jieba/test_jieba_keyword_table_handler.py:92:5
 ERROR Argument `str` is not assignable to parameter `doc_form` with type `IndexStructureType | SQLCoreOperations[IndexStructureType]` in function `models.dataset.Document.__init__` [bad-argument-type]
    --> tests/unit_tests/core/rag/datasource/test_retrieval_service.py:148:18
 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]

@codecov

codecov Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.84%. Comparing base (d1e859d) to head (14706e0).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #45320      +/-   ##
==========================================
- Coverage   89.84%   89.84%   -0.01%     
==========================================
  Files        6242     6242              
  Lines      391154   391154              
  Branches    79217    79217              
==========================================
- Hits       351445   351432      -13     
- Misses      33607    33618      +11     
- Partials     6102     6104       +2     
Flag Coverage Δ
api 89.64% <ø> (-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.

The reviewer asked to replace the fake TFIDF double with a real class. The
tests now subclass jieba.analyse.TFIDF, so the handler exercises the
production IDF table and the stop_words attribute it writes, and only the
tag list is chosen by the test. The fake module objects keep the real
attributes the analyse package exposes, and the loader branches are covered
with the real TFIDF class.

pyrefly strict check: 0 diagnostics. pytest: 6 passed. ruff check and
ruff format pass.
@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 2, 2026
@lianbie

lianbie commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

@asukaminato0721 I addressed the two things you asked for, in 20a2767:

  • Replaced _DummyTFIDF with _RecordingTFIDF(TFIDF), a subclass of the real jieba.analyse.TFIDF. The real TFIDF.__init__ runs, so the IDF table is built and stop_words exists; only extract_tags returns a test-chosen keyword list.
  • The loader branches now assert against the real class (isinstance(handler._tfidf, TFIDF)), instead of the two ad-hoc subclasses the previous revision used.

On the new head: strict pyrefly is 0 diagnostics for this file, the 6 tests pass, and ruff check / ruff format are clean.

@GYJ99 thanks for flagging the overlap with #45347 on that other PR — agreed this should land as one change, and #45320 already uses the real TFIDF rather than Any.

Also noting for maintainers: every workflow on 20a2767 is action_required, and I cannot approve runs for the upstream repo from my fork account — CI will need one maintainer click to get going. (The previous revision, e703597, had all workflows green.)

lianbie and others added 2 commits November 3, 2026 09:57
ruff's isort rule (I001) groups third-party and first-party imports; the
`jieba` import sat above the `core` imports and the check failed on the
changed file. Move it into the third-party block so `ruff check` passes
with api/.ruff.toml.

No behavior change.
@lianbie

lianbie commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

Friendly follow-up on review: the requested change is in and the branch is now clean.

Addressed: _DummyTFIDF is replaced by _RecordingTFIDF(TFIDF), a subclass of the real jieba.analyse.TFIDF — the production __init__ runs, so the IDF table is built and stop_words exists; only extract_tags returns a test-chosen keyword list. The loader branches assert with isinstance(handler._tfidf, TFIDF) against the real class. Replied in the inline thread as well.

Formatting: autofix picked up my file in d0e9b6a and moved from jieba.analyse import TFIDF into the third-party block, so ruff check / format / import ordering now match the repo config.

One note on the Pyrefly report, in case it is read as a regression: Type coverage 83.11% -> 82.89% is not from this file. The diff shows seven missing-attribute diagnostics removed from test_jieba_keyword_table_handler.py (the file is also removed from tests/unit_tests/pyrefly.toml). The added diagnostics in the same report are in test_app_config_manager.py, test_generator.py, test_session_store.py and similar files that this PR does not touch; they come from base-branch drift between the two runs.

Full CI still shows action_required on every workflow for d0e9b6a — that needs a maintainer approval to run, which I cannot grant from my fork account. The previous revision (953de927) had all workflows green apart from autofix.ci, which is what produced d0e9b6a.

Could you take another look when you have a moment? Thanks.

@asukaminato0721 asukaminato0721 self-assigned this Nov 4, 2026
@asukaminato0721
asukaminato0721 added this pull request to the merge queue Nov 4, 2026
Merged via the queue into langgenius:main with commit 4d953b6 Nov 4, 2026
51 checks passed
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