Repository navigation
Load cryptography before the fake filesystem in tests - #735
quantconnect-kronos[bot] merged 1 commit into
Conversation
pyfakefs removes the modules first imported during a test from sys.modules once the test ends. cryptography's native bindings can only be initialized once per process, so the next test importing them failed. The full suite hid this because other test modules import the encryption helper during collection; running a subset such as tests/components/util failed 32 pull/push manager tests. Closes #724 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Approving. Test-only change: tests/conftest.py now imports the encryption helper at module level, so cryptography's native bindings load before any fake filesystem starts. This gives the result #724 asks for, using the approach it suggests, and leaves production imports untouched.
Verified locally (Python 3.11, pyfakefs 5.10.2, cryptography 43.0.0):
python -m pytest tests/components/util -q: 32 failed, 270 passed, 44 skipped with the baseconftest.py, and 302 passed, 44 skipped with this one. The failures are theImportErrorfrom the issue.- Every one of the 81 test files run on its own with this change: all pass except
tests/ui/test_app.py::test_command_process_runs_the_cli_as_a_child_process. That test fails the same way with the baseconftest.py(['lean 1.0.233'] == ['lean 0.0.0-dev']): it reads the version of a released package installed locally, so this change didn't cause it. - No test patches or reloads
lean.components.util.encryption_helperor touchessys.modules, so loading it eagerly changes nothing else. The tests intest_pull.pyandtest_push.pyalready loaded it during collection, which is why the full suite passed before.
CI: every test job passes on the head commit, on all operating systems and Python versions.
Affected repositories: none. The change is confined to the test setup.
Risk: risk:low. It changes four lines of test setup, is trivially reversible and touches no shipped code. The evidence is green CI plus the before and after runs above.
There was a problem hiding this comment.
Approving: no findings. The test-only preload addresses #724 without changing production imports. It runs before the fake-filesystem fixtures, keeping cryptography's native bindings out of pyfakefs' per-test module cleanup. The helper's module-level code introduces no filesystem access or application initialization.
Independent verification on Python 3.11, pyfakefs 5.10.2 and cryptography 43.0.0:
python -m pytest tests/components/util -q: 32 failed, 270 passed, 44 skipped on base391f59e; 302 passed, 44 skipped on this head.- Each affected file run independently:
test_pull_manager.py33 passed, 1 skipped;test_push_manager.py35 passed, 3 skipped. - Full suite (
python -m pytest -q --tb=short): 2825 passed, 81 skipped, 1 failed. The sole failure istests/ui/test_app.py::test_command_process_runs_the_cli_as_a_child_process, reporting installed CLI version1.0.233instead of0.0.0-dev. I reproduced the identical failure on base; this change did not cause it. - All CI test jobs pass on the reviewed head across the OS/Python matrix.
No other repositories need updates. End-to-end product testing is not needed because neither shipped code nor product behavior changes.
Retaining risk:low: a four-line, easily reverted test-setup change, backed by a reproduced before/after result and green CI.
Closes #724
What changed
tests/conftest.pynow importslean.components.util.encryption_helperat module level, so cryptography's native bindings are loaded before any pyfakefs fake filesystem starts.Why
pyfakefs' dynamic patcher deletes from
sys.modulesevery module first imported while a test's fake filesystem is active. The first pull/push manager test imports the encryption helper lazily (pull_manager.py:185,push_manager.py:167), which loadscryptography.hazmat.bindings._rust; after that test pyfakefs unloads it. The next test re-imports it and getsImportError: PyO3 modules compiled for CPython 3.8 or older may only be initialized once per interpreter process, which the managers catch and log, so the expected calls never happen.The full suite hides this because
tests/commands/cloud/test_pull.pyandtest_push.pyimport the encryption helper during collection, before any fake filesystem starts. The conftest import makes every subset behave the same way. Test-only change: production imports are untouched.Testing
Python 3.11, pyfakefs 5.10.2, cryptography 43.0.0:
python -m pytest tests/components/util -q: before 32 failed, 270 passed, 44 skipped; after 302 passed, 44 skipped.test_pull_manager.pyandtest_push_manager.pyon their own: before 16 and 15 failed; after all pass.PBKDF2HMAC(lazily imported in the helper) passes on three runs in a row, so the modules still loaded inside tests reload fine once the native bindings stay loaded.tests/ui/test_app.py::test_command_process_runs_the_cli_as_a_child_process.python -m pytest -s -rs): 2825 passed, 81 skipped, 1 failed. The failure is that same test (['lean 1.0.233'] == ['lean 0.0.0-dev']). It is caused by a releasedleanpackage installed in the local environment and fails the same way onmaster.