Lazy-load TestCase to preventing pytest crashes.

Review Request #13324 — Created Oct. 9, 2023 and submitted

Information

Review Board
release-6.x

Reviewers

In Review Board 6, the pytest fixtures were moved into
reviewboard.testing.pytest_fixtures. This caused pytest to have to
import reviewboard.testing on load. The problem there is that this
module forward-imports reviewboard.testing.testcase.TestCase, and this
module depends on a working Django environment first. That causes a
crash for pytest help output or operations that don't require setting up
a test environment.

We now lazily-import TestCase in this module so that we only pull it
in when explicitly requested. This is done through a module-level
__getattr__ (available in all supported versions of Python).

A TYPE_CHECKING-level import is done to ensure that importers see the
right type for this class.

Unit tests pass on Python 3.8-3.11.

I can run pytest --help without a crash.

Summary ID
Lazy-load TestCase to preventing pytest crashes.
In Review Board 6, the pytest fixtures were moved into `reviewboard.testing.pytest_fixtures`. This caused pytest to have to import `reviewboard.testing` on load. The problem there is that this module forward-imports `reviewboard.testing.testcase.TestCase`, and this module depends on a working Django environment first. That causes a crash for pytest help output or operations that don't require setting up a test environment. We now lazily-import `TestCase` in this module so that we only pull it in when explicitly requested. This is done through a module-level `__getattr__` (available in all supported versions of Python). A `TYPE_CHECKING`-level import is done to ensure that importers see the right type for this class.
a8dc79456c089fe5441107cf894bdf6482adc9bb
Description From Last Updated

Can you add a module docstring while you're touching this?

daviddavid
david
  1. Ship It!
  2. reviewboard/testing/__init__.py (Diff revision 1)
     
     
    Show all issues

    Can you add a module docstring while you're touching this?

  3. 
      
chipx86
david
  1. Ship It!
  2. 
      
maubin
  1. Ship It!
  2. 
      
chipx86
Review request changed
Status:
Completed
Change Summary:
Pushed to release-6.x (5d91151)