Skip to content

test: remove import-time module stubs that leak into later tests - #20926

Merged
yuzhichang merged 3 commits into
infiniflow:0.27.xfrom
devYRPauli:fix/is-english-test-stub-leak
Oct 20, 2026
Merged

yuzhichang merged 3 commits into
infiniflow:0.27.xfrom
devYRPauli:fix/is-english-test-stub-leak

Conversation

@devYRPauli

@devYRPauli devYRPauli commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

test/unit_test/rag/test_is_english.py and test/unit_test/rag/test_remove_contents_table.py add stub modules to sys.modules when pytest imports them. Each file adds a stub for common.token_utils, roman_numbers, word2number, cn2an, PIL, PIL.Image and chardet when the real module is not loaded yet. The files never remove the stubs. A test file that pytest collects later in the same run then imports a stub instead of the real module. CodeRabbit flagged this leak in a review comment on my #15489, which added the first of these files: #15489 (comment)

test/unit_test/deepdoc/parser/test_html_parser.py has the same defect. The file adds stubs for 12 modules, for example rag, rag.nlp, PIL and infinity, when the real module is not loaded yet. It also puts its own copy of deepdoc.parser.html_parser into sys.modules, and that copy has a fake tokenizer. My #16052 added that file, and CodeRabbit flagged the leak there too: #16052 (comment)

Results, with 0.27.x at f82b78a1c:

command 0.27.x this branch
pytest test/unit_test/common/test_token_utils.py 41 passed 41 passed
pytest test/unit_test/rag/test_is_english.py test/unit_test/common/test_token_utils.py collection error, 0 tests run 46 passed
pytest test/unit_test/rag/test_remove_contents_table.py test/unit_test/common/test_token_utils.py collection error, 0 tests run 56 passed
pytest test/unit_test/rag/test_is_english.py test/unit_test/rag/test_remove_contents_table.py test/unit_test/rag/test_find_codec.py test/unit_test/rag/nlp/test_default_delimiter_constant.py 1 failed, 43 passed 44 passed
pytest test/unit_test/deepdoc/parser/test_txt_parser.py 8 passed 8 passed
pytest test/unit_test/deepdoc/parser/test_html_parser.py test/unit_test/deepdoc/parser/test_txt_parser.py collection error, 0 tests run 25 passed

In rows 2 and 3, the collection error is ImportError: cannot import name 'get_encoder' from 'common.token_utils' (unknown location). The stub common.token_utils holds only a num_tokens_from_string that returns 0. With that stub, naive_merge puts all the text in one chunk. Thus test_semicolon_english_text_splits_under_the_unified_default fails in row 4.

In the last row, the collection error is ModuleNotFoundError: No module named 'rag.utils'; 'rag' is not a package. test_txt_parser.py imports deepdoc.parser, and that import reaches the stub rag module.

The copy of html_parser leaks too. When test_txt_parser.py runs first and loads the real module, test_html_parser.py then replaces sys.modules["deepdoc.parser.html_parser"] with its copy. A later import of RAGFlowHtmlParser then gets the fake tokenizer. I checked this with a test file that compares sys.modules["deepdoc.parser.html_parser"].rag_tokenizer with rag.nlp.rag_tokenizer. Run after the two files, the check fails on 0.27.x with _FakeTokenizer and passes on this branch.

A full test/unit_test run shows no failure from these stubs. Files that pytest collects earlier load the real modules, and the stub code does not replace a loaded module. The html_parser copy replaces the loaded module in that run too, but no test outcome changes. The leak shows when a run collects one of these files before any file that loads the real modules. A run of selected files can do this.

The stubs in the two rag files are not necessary. Since #18986, common.token_utils builds the tiktoken encoder on first use, so an import of rag.nlp does not go to the network. is_english and remove_contents_table do not count tokens, so these tests never build the encoder. CI installs the other stubbed packages with uv sync --group test.

test_html_parser.py keeps its stubs, but only while it loads html_parser.py. A direct import of deepdoc.parser makes this file fail in pytest test/unit_test/deepdoc. In that run, test_epub_parser.py loads first and leaves a stub infinity module in sys.modules. With a direct import, that run stops with 4 collection errors instead of 3.

This PR makes these changes:

  • test_is_english.py and test_remove_contents_table.py: remove the stubs and import rag.nlp directly.
  • test_find_codec.py: remove the line that deleted a chardet stub before each test, and its comment. My Fix: find_codec discards confident encoding detection and returns a garbage codec #15793 added that line to work around this leak. No test file stubs chardet at import time after this PR.
  • test_html_parser.py: add the stubs with mock.patch.dict(sys.modules, ...) only while the file loads html_parser.py, and keep the loaded copy out of sys.modules. The fake tokenizer then stays inside this file.

Other test files also leave import-time stubs in sys.modules. For example, test/unit_test/rag/advanced_rag/knowlege_compile/conftest.py adds stub rag, rag.nlp and rag.utils modules when they are not loaded yet. pytest test/unit_test/rag --continue-on-collection-errors loads that file before the 3 rag test files that this PR changes. That run gives 281 errors. These 3 files give 21 of these errors, because of the stub rag and rag.nlp modules. Every test in that run has the same outcome on 0.27.x and on this branch. This PR does not change that conftest file or test_epub_parser.py.

Tests:

  • test_is_english.py passes 5 tests, test_remove_contents_table.py passes 15, and test_find_codec.py passes 19.
  • The first two files still pass with no cached tiktoken BPE file and with the HTTP and HTTPS proxies set to a closed port. In the same run, test_token_utils.py fails 29 tests with ProxyError. This shows that the run could not download the BPE file.
  • test_html_parser.py passes 17 tests. I changed html_parser.py to lowercase each split piece, and test_oversized_english_block_preserves_original_text then failed. So the test still checks the real parser code.
  • pytest test/unit_test/deepdoc --continue-on-collection-errors gives 6 failed, 478 passed and 15 errors before and after the test_html_parser.py commit, with the same failures.
  • python -m pytest test/unit_test -W ignore::SyntaxWarning --config-file pyproject.toml --continue-on-collection-errors: every test has the same outcome on 0.27.x and on this branch. run_tests.py -i passes the same -W and --config-file flags. On my macOS machine, 7 tests fail and 3 files have a collection error, on 0.27.x and on this branch. None of them is in a file that this PR changes. I set ZHIPU_AI_API_KEY and SILICONFLOW_API_KEY to placeholder values. Without them, test/testcases/configs.py stops the run before the last 4 tests.
  • ruff format --check passes, and ruff check reports no new findings.

devYRPauli and others added 2 commits October 13, 2026 19:16
test_is_english.py and test_remove_contents_table.py put stub modules
into sys.modules when pytest imports them, and never remove them. A
test that pytest collects later in the same run then imports a stub
instead of the real module. For example, this run stops at collection:

    pytest test/unit_test/rag/test_is_english.py \
        test/unit_test/common/test_token_utils.py

    ImportError: cannot import name 'get_encoder' from
    'common.token_utils' (unknown location)

The stubs are not necessary. Since infiniflow#18986, common.token_utils builds
the tiktoken encoder on first use, so an import of rag.nlp does not go
to the network. The other stubbed packages are project dependencies.
is_english and remove_contents_table do not count tokens.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
test_find_codec.py deleted any chardet stub before each test, because
test_is_english.py and test_remove_contents_table.py stubbed chardet at
import time. The previous commit removes those stubs, so the workaround
and its comment no longer apply.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: bf2d264a-4570-4e5d-a329-7dd95086e652

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

test_html_parser.py put stub modules into sys.modules when pytest
imports it, and never removed them. It also registered its own copy of
deepdoc.parser.html_parser, with a fake tokenizer, in sys.modules. A
test file that pytest imports later in the same run then gets a stub or
the fake tokenizer. For example, this run stops at collection:

    pytest test/unit_test/deepdoc/parser/test_html_parser.py \
        test/unit_test/deepdoc/parser/test_txt_parser.py

    ModuleNotFoundError: No module named 'rag.utils'; 'rag' is not a
    package

The test now adds the stubs with mock.patch.dict only while it loads
html_parser.py, and it keeps the loaded module out of sys.modules.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@devYRPauli devYRPauli changed the title test(rag): remove import-time module stubs that leak into later tests test: remove import-time module stubs that leak into later tests Oct 14, 2026
@wangq8
wangq8 requested a review from xugangqiang October 19, 2026 09:53
@xugangqiang xugangqiang added the ci Continue Integration label Oct 20, 2026
@xugangqiang

Copy link
Copy Markdown
Contributor

LGTM

@codecov

codecov Bot commented Oct 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 38.01%. Comparing base (d3ebbdd) to head (0b29b5b).
⚠️ Report is 28 commits behind head on 0.27.x.

Additional details and impacted files
@@            Coverage Diff             @@
##           0.27.x   #20926      +/-   ##
==========================================
+ Coverage   37.97%   38.01%   +0.04%     
==========================================
  Files          54       54              
  Lines       15527    15543      +16     
  Branches      125      125              
==========================================
+ Hits         5896     5909      +13     
- Misses       9603     9606       +3     
  Partials       28       28              

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@yuzhichang
yuzhichang merged commit 323da69 into infiniflow:0.27.x Oct 20, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants