Skip to content

Refactor: hoist conftest lazy imports that have no rationale#722

Merged
ChaoWao merged 1 commit intohw-native-sys:mainfrom
ChaoWao:refactor/lift-conftest-lazy-imports
May 8, 2026
Merged

Refactor: hoist conftest lazy imports that have no rationale#722
ChaoWao merged 1 commit intohw-native-sys:mainfrom
ChaoWao:refactor/lift-conftest-lazy-imports

Conversation

@ChaoWao
Copy link
Copy Markdown
Collaborator

@ChaoWao ChaoWao commented May 8, 2026

Summary

Follow-up cleanup to #721. Scanning the two files touched by that PR
turned up seven function-local imports each carrying # noqa: PLC0415
for no technical reason — the modules they import are pure Python, no
side effects, no circular risk, and no startup-cost impact. They were
just style debt.

Hoist these to the top of conftest.py:

  • simpler_setup.parallel_scheduler (used in 4 functions)
  • simpler_setup.pto_isa.ensure_pto_isa_root
  • simpler_setup.scene_test.clear_compile_cache
  • stdlib faulthandler

Net diff: -5 lines, all # noqa: PLC0415 of the removed imports gone.

Lazy imports kept (intentional)

  • from simpler.worker import Worker (2 fixture sites): simpler.worker
    pulls the nanobind C extension _task_interface. Hoisting would slow
    every pytest startup, including tests/ut paths that never construct
    a ChipWorker.
  • import xdist: try / except ImportError probe for an optional
    dep — the whole point of inline import is to handle the failure.

The full evaluation of every # noqa in conftest.py and
simpler_setup/parallel_scheduler.py, with reasoning per line, is on
the PR conversation upstream.

Side benefit

The session-timeout SIGALRM handler installed in #721 used to run
from simpler_setup import parallel_scheduler inside the signal
context. Doing imports inside a signal handler can deadlock on Python's
import lock if the module hasn't been loaded yet (rare, but possible
when the timeout fires before the dispatcher has reached
_dispatch_test_phases). The handler now only references an
already-loaded module attribute. Comment added explaining the
constraint.

Testing

  • ruff check clean (verified locally; pre-commit ran on commit)
  • CI green on the simulation runners (no behavior change expected)

Seven `from simpler_setup.* import ...` / `import faulthandler` lines
inside individual functions were carrying `# noqa: PLC0415` for no
technical reason — the modules they imported are pure Python with no
side effects, no circular import risk, and no significant import cost.
Move them to the top of the file:

- `simpler_setup.parallel_scheduler` (used in 4 spots)
- `simpler_setup.pto_isa.ensure_pto_isa_root`
- `simpler_setup.scene_test.clear_compile_cache`
- stdlib `faulthandler`

Lazy imports that *do* have a reason are kept:

- `simpler.worker.Worker` (2 spots) — pulls in the nanobind C extension
  `_task_interface`; hoisting would slow every pytest startup including
  paths that never need a ChipWorker.
- `xdist` — `try`/`except ImportError` probe for an optional dep; the
  whole point of importing inline is to handle the import failure.

A side benefit: the SIGALRM handler no longer does a
`from simpler_setup import parallel_scheduler` inside a signal context,
which can deadlock on Python's import lock when the module has not been
loaded yet. The handler now only references an already-loaded module
attribute. Comment added at the handler explaining the constraint.
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors conftest.py by hoisting several local imports, including faulthandler, parallel_scheduler, and clear_compile_cache, to the module level. This change is intended to prevent potential deadlocks that can occur when importing modules within signal handlers. I have no feedback to provide.

@ChaoWao ChaoWao merged commit 558900d into hw-native-sys:main May 8, 2026
14 checks passed
@ChaoWao ChaoWao deleted the refactor/lift-conftest-lazy-imports branch May 8, 2026 09:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant