Skip to content

Load cryptography before the fake filesystem in tests - #735

Merged
quantconnect-kronos[bot] merged 1 commit into
masterfrom
kronos/bug-724-preload-cryptography-in-tests
Nov 17, 2026
Merged

quantconnect-kronos[bot] merged 1 commit into
masterfrom
kronos/bug-724-preload-cryptography-in-tests

Conversation

@quantconnect-kronos

Copy link
Copy Markdown
Contributor

Closes #724

What changed

tests/conftest.py now imports lean.components.util.encryption_helper at module level, so cryptography's native bindings are loaded before any pyfakefs fake filesystem starts.

Why

pyfakefs' dynamic patcher deletes from sys.modules every 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 loads cryptography.hazmat.bindings._rust; after that test pyfakefs unloads it. The next test re-imports it and gets ImportError: 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.py and test_push.py import 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.py and test_push_manager.py on their own: before 16 and 15 failed; after all pass.
  • A test that imports the encryption helper and derives a key with 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.
  • Every test file run on its own: all pass except tests/ui/test_app.py::test_command_process_runs_the_cli_as_a_child_process.
  • Full suite as CI runs it (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 released lean package installed in the local environment and fails the same way on master.

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>
@quantconnect-kronos quantconnect-kronos Bot added kronos:develop:working develop: in progress kronos:review Request review labels Nov 17, 2026
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added kronos:review:queued review: queued kronos:review:working review: in progress and removed kronos:review Request review kronos:review:queued review: queued labels Nov 17, 2026
@quantconnect-kronos-reviewer
quantconnect-kronos-reviewer Bot marked this pull request as ready for review November 17, 2026 15:58

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 base conftest.py, and 302 passed, 44 skipped with this one. The failures are the ImportError from 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 base conftest.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_helper or touches sys.modules, so loading it eagerly changes nothing else. The tests in test_pull.py and test_push.py already 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.

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added risk:low Low risk and removed kronos:review:working review: in progress labels Nov 17, 2026
@quantconnect-kronos quantconnect-kronos Bot added the kronos:review Request review label Nov 17, 2026
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added kronos:review:queued review: queued and removed kronos:review Request review labels Nov 17, 2026
@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot added kronos:review:working review: in progress and removed kronos:review:queued review: queued labels Nov 17, 2026

@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 base 391f59e; 302 passed, 44 skipped on this head.
  • Each affected file run independently: test_pull_manager.py 33 passed, 1 skipped; test_push_manager.py 35 passed, 3 skipped.
  • Full suite (python -m pytest -q --tb=short): 2825 passed, 81 skipped, 1 failed. The sole failure is tests/ui/test_app.py::test_command_process_runs_the_cli_as_a_child_process, reporting installed CLI version 1.0.233 instead of 0.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.

@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot removed the kronos:review:working review: in progress label Nov 17, 2026
@quantconnect-kronos
quantconnect-kronos Bot merged commit 6d9812a into master Nov 17, 2026
63 of 64 checks passed
@quantconnect-kronos
quantconnect-kronos Bot deleted the kronos/bug-724-preload-cryptography-in-tests branch November 17, 2026 16:09
@quantconnect-kronos quantconnect-kronos Bot removed the kronos:develop:working develop: in progress label Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk:low Low risk

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Utility test subset fails when encryption dependencies load after filesystem patching

0 participants