Repository navigation
test: remove import-time module stubs that leak into later tests - #20926
Conversation
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]>
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configuration
You can disable this status message by setting the Use the checkbox below for a quick retry:
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. Comment |
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]>
|
LGTM |
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
Summary
test/unit_test/rag/test_is_english.pyandtest/unit_test/rag/test_remove_contents_table.pyadd stub modules tosys.moduleswhen pytest imports them. Each file adds a stub forcommon.token_utils,roman_numbers,word2number,cn2an,PIL,PIL.Imageandchardetwhen 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.pyhas the same defect. The file adds stubs for 12 modules, for examplerag,rag.nlp,PILandinfinity, when the real module is not loaded yet. It also puts its own copy ofdeepdoc.parser.html_parserintosys.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.xatf82b78a1c:0.27.xpytest test/unit_test/common/test_token_utils.pypytest test/unit_test/rag/test_is_english.py test/unit_test/common/test_token_utils.pypytest test/unit_test/rag/test_remove_contents_table.py test/unit_test/common/test_token_utils.pypytest 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.pypytest test/unit_test/deepdoc/parser/test_txt_parser.pypytest test/unit_test/deepdoc/parser/test_html_parser.py test/unit_test/deepdoc/parser/test_txt_parser.pyIn rows 2 and 3, the collection error is
ImportError: cannot import name 'get_encoder' from 'common.token_utils' (unknown location). The stubcommon.token_utilsholds only anum_tokens_from_stringthat returns 0. With that stub,naive_mergeputs all the text in one chunk. Thustest_semicolon_english_text_splits_under_the_unified_defaultfails 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.pyimportsdeepdoc.parser, and that import reaches the stubragmodule.The copy of
html_parserleaks too. Whentest_txt_parser.pyruns first and loads the real module,test_html_parser.pythen replacessys.modules["deepdoc.parser.html_parser"]with its copy. A later import ofRAGFlowHtmlParserthen gets the fake tokenizer. I checked this with a test file that comparessys.modules["deepdoc.parser.html_parser"].rag_tokenizerwithrag.nlp.rag_tokenizer. Run after the two files, the check fails on0.27.xwith_FakeTokenizerand passes on this branch.A full
test/unit_testrun 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. Thehtml_parsercopy 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
ragfiles are not necessary. Since #18986,common.token_utilsbuilds the tiktoken encoder on first use, so an import ofrag.nlpdoes not go to the network.is_englishandremove_contents_tabledo not count tokens, so these tests never build the encoder. CI installs the other stubbed packages withuv sync --group test.test_html_parser.pykeeps its stubs, but only while it loadshtml_parser.py. A direct import ofdeepdoc.parsermakes this file fail inpytest test/unit_test/deepdoc. In that run,test_epub_parser.pyloads first and leaves a stubinfinitymodule insys.modules. With a direct import, that run stops with 4 collection errors instead of 3.This PR makes these changes:
test_is_english.pyandtest_remove_contents_table.py: remove the stubs and importrag.nlpdirectly.test_find_codec.py: remove the line that deleted achardetstub 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 stubschardetat import time after this PR.test_html_parser.py: add the stubs withmock.patch.dict(sys.modules, ...)only while the file loadshtml_parser.py, and keep the loaded copy out ofsys.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.pyadds stubrag,rag.nlpandrag.utilsmodules when they are not loaded yet.pytest test/unit_test/rag --continue-on-collection-errorsloads that file before the 3ragtest files that this PR changes. That run gives 281 errors. These 3 files give 21 of these errors, because of the stubragandrag.nlpmodules. Every test in that run has the same outcome on0.27.xand on this branch. This PR does not change that conftest file ortest_epub_parser.py.Tests:
test_is_english.pypasses 5 tests,test_remove_contents_table.pypasses 15, andtest_find_codec.pypasses 19.test_token_utils.pyfails 29 tests withProxyError. This shows that the run could not download the BPE file.test_html_parser.pypasses 17 tests. I changedhtml_parser.pyto lowercase each split piece, andtest_oversized_english_block_preserves_original_textthen failed. So the test still checks the real parser code.pytest test/unit_test/deepdoc --continue-on-collection-errorsgives 6 failed, 478 passed and 15 errors before and after thetest_html_parser.pycommit, 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 on0.27.xand on this branch.run_tests.py -ipasses the same-Wand--config-fileflags. On my macOS machine, 7 tests fail and 3 files have a collection error, on0.27.xand on this branch. None of them is in a file that this PR changes. I setZHIPU_AI_API_KEYandSILICONFLOW_API_KEYto placeholder values. Without them,test/testcases/configs.pystops the run before the last 4 tests.ruff format --checkpasses, andruff checkreports no new findings.