Skip to content

Commit 68ec62b

Browse files
do this checking for lxml the right way
1 parent 1e2da78 commit 68ec62b

File tree

3 files changed

+15
-26
lines changed

3 files changed

+15
-26
lines changed

mypy/test/testcheck.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import os
66
import re
77
import sys
8+
from importlib.util import find_spec as import_exists
9+
10+
import pytest
811

912
from mypy import build
1013
from mypy.build import Graph
@@ -24,14 +27,6 @@
2427
)
2528
from mypy.test.update_data import update_testcase_output
2629

27-
try:
28-
pass # import lxml #try just passing for now...
29-
except ImportError:
30-
lxml_import_failure = True
31-
32-
33-
import pytest
34-
3530
# List of files that contain test case descriptions.
3631
# Includes all check-* files with the .test extension in the test-data/unit directory
3732
typecheck_files = find_test_files(pattern="check-*.test")
@@ -55,7 +50,7 @@ class TypeCheckSuite(DataSuite):
5550
files = typecheck_files
5651

5752
def run_case(self, testcase: DataDrivenTestCase) -> None:
58-
if lxml_import_failure and os.path.basename(testcase.file) == "check-reports.test":
53+
if import_exists("lmxl") and os.path.basename(testcase.file) == "check-reports.test":
5954
pytest.skip("Cannot import lxml. Is it installed?")
6055
incremental = (
6156
"incremental" in testcase.name.lower()

mypy/test/testcmdline.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import re
1111
import subprocess
1212
import sys
13+
from importlib.util import find_spec as import_exists
14+
15+
import pytest
1316

1417
from mypy.test.config import PREFIX, test_temp_dir
1518
from mypy.test.data import DataDrivenTestCase, DataSuite
@@ -19,13 +22,6 @@
1922
normalize_error_messages,
2023
)
2124

22-
try:
23-
pass # import lxml #try just passing for now...
24-
except ImportError:
25-
lxml_import_failure = True
26-
27-
import pytest
28-
2925
# Path to Python 3 interpreter
3026
python3_path = sys.executable
3127

@@ -38,7 +34,7 @@ class PythonCmdlineSuite(DataSuite):
3834
native_sep = True
3935

4036
def run_case(self, testcase: DataDrivenTestCase) -> None:
41-
if lxml_import_failure and os.path.basename(testcase.file) == "reports.test":
37+
if not import_exists("lxml") and os.path.basename(testcase.file) == "reports.test":
4238
pytest.skip("Cannot import lxml. Is it installed?")
4339
for step in [1] + sorted(testcase.output2):
4440
test_python_cmdline(testcase, step)

mypy/test/testreports.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,24 @@
33
from __future__ import annotations
44

55
import textwrap
6+
from importlib.util import find_spec as import_exists
7+
8+
import pytest
69

710
from mypy.report import CoberturaPackage, get_line_rate
811
from mypy.test.helpers import Suite, assert_equal
912

10-
try:
11-
import lxml.etree as etree
12-
except ImportError:
13-
lxml_import_failure = True
14-
15-
import pytest
16-
1713

1814
class CoberturaReportSuite(Suite):
19-
@pytest.mark.skipif(lxml_import_failure, reason="Cannot import lxml. Is it installed?")
15+
@pytest.mark.skipif(not import_exists("lxml"), reason="Cannot import lxml. Is it installed?")
2016
def test_get_line_rate(self) -> None:
2117
assert_equal("1.0", get_line_rate(0, 0))
2218
assert_equal("0.3333", get_line_rate(1, 3))
2319

24-
@pytest.mark.skipif(lxml_import_failure, reason="Cannot import lxml. Is it installed?")
20+
@pytest.mark.skipif(not import_exists("lxml"), reason="Cannot import lxml. Is it installed?")
2521
def test_as_xml(self) -> None:
22+
import lxml.etree as etree
23+
2624
cobertura_package = CoberturaPackage("foobar")
2725
cobertura_package.covered_lines = 21
2826
cobertura_package.total_lines = 42

0 commit comments

Comments
 (0)