test-backend: Cast test runner to the custom Runner type.

Mypy does not know the acccurate return type of `get_runner` that is
determined by the `TEST_RUNNER` setting. We need to cast it to the
correct type to use methods like `get_shallow_tested_templates`. Note
that we import conditionally to avoid adding runtime dependency on
`zerver`.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
This commit is contained in:
Zixuan James Li 2022-07-21 21:06:45 -04:00 committed by Tim Abbott
parent 0ed590594d
commit f88530575a
1 changed files with 9 additions and 2 deletions

View File

@ -8,9 +8,14 @@ import shlex
import subprocess import subprocess
import sys import sys
import tempfile import tempfile
from typing import Iterator, List from typing import TYPE_CHECKING, Iterator, List, Type, cast
from unittest import mock from unittest import mock
if TYPE_CHECKING:
# This script does not have access to the zerver module during runtime.
# We can only import this when type checking.
from zerver.lib.test_runner import Runner
TOOLS_DIR = os.path.dirname(os.path.abspath(__file__)) TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
os.chdir(os.path.dirname(TOOLS_DIR)) os.chdir(os.path.dirname(TOOLS_DIR))
sys.path.insert(0, os.path.dirname(TOOLS_DIR)) sys.path.insert(0, os.path.dirname(TOOLS_DIR))
@ -406,7 +411,9 @@ def main() -> None:
subprocess.check_call(["tools/webpack", "--test"]) subprocess.check_call(["tools/webpack", "--test"])
TestRunner = get_runner(settings) # isinstance check cannot be used with types. This can potentially improved by supporting
# dynamic resolution of the test runner type with the django-stubs mypy plugin.
TestRunner = cast("Type[Runner]", get_runner(settings))
parallel = default_parallel if options.processes is None else options.processes parallel = default_parallel if options.processes is None else options.processes
if parallel > 1: if parallel > 1: