Skip to content

Commit a107d0c

Browse files
committed
refactor case dicovery logic
1 parent ec1f03f commit a107d0c

13 files changed

+111
-232
lines changed

mypy/test/data.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@
99

1010
import pytest # type: ignore # no pytest in typeshed
1111
from typing import List, Tuple, Set, Optional, Iterator, Any, Dict
12+
import typing
1213

1314
from mypy.myunit import ProtoTestCase
14-
15+
from mypy.test.config import test_data_prefix
1516

1617
root_dir = os.path.normpath(os.path.join(os.path.dirname(__file__), '..', '..'))
1718

1819

1920
def parse_test_cases(
2021
path: str,
2122
base_path: str = '.',
22-
optional_out: bool = False, *,
23+
optional_out: bool = False,
2324
native_sep: bool = False) -> List['DataDrivenTestCase']:
2425
"""Parse a file with test case descriptions.
2526
@@ -495,13 +496,20 @@ def pytest_addoption(parser: Any) -> None:
495496
def pytest_pycollect_makeitem(collector: Any, name: str, obj: Any) -> Any:
496497
if not isinstance(obj, type) or not issubclass(obj, DataSuite):
497498
return None
499+
if obj is DataSuite:
500+
return None
498501
return MypyDataSuite(name, parent=collector)
499502

500503

501504
class MypyDataSuite(pytest.Class): # type: ignore # inheriting from Any
502505
def collect(self) -> Iterator['MypyDataCase']:
503-
for case in self.obj.cases():
504-
yield MypyDataCase(case.name, self, case)
506+
cls = self.obj
507+
for f in cls.files:
508+
for case in parse_test_cases(os.path.join(test_data_prefix, f),
509+
base_path=cls.base_path,
510+
optional_out=cls.optional_out,
511+
native_sep=cls.native_sep):
512+
yield MypyDataCase(case.name, self, case)
505513

506514

507515
class MypyDataCase(pytest.Item): # type: ignore # inheriting from Any
@@ -544,12 +552,13 @@ def repr_failure(self, excinfo: Any) -> str:
544552

545553

546554
class DataSuite:
555+
files = None # type: typing.ClassVar[List[str]]
556+
base_path = '.' # type: typing.ClassVar[str]
557+
optional_out = False # type: typing.ClassVar[bool]
558+
native_sep = False # type: typing.ClassVar[bool]
559+
547560
def __init__(self, *, update_data: bool) -> None:
548561
self.update_data = update_data
549562

550-
@classmethod
551-
def cases(cls) -> List[DataDrivenTestCase]:
552-
return []
553-
554563
def run_case(self, testcase: DataDrivenTestCase) -> None:
555564
raise NotImplementedError

mypy/test/testcheck.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@
33
import os
44
import re
55
import shutil
6-
import sys
7-
import time
8-
import typed_ast
96

107
from typing import Dict, List, Optional, Set, Tuple
8+
import typing
119

1210
from mypy import build, defaults
1311
from mypy.main import process_options
1412
from mypy.build import BuildSource, find_module_clear_caches
1513
from mypy.myunit import AssertionFailure
16-
from mypy.test.config import test_temp_dir, test_data_prefix
17-
from mypy.test.data import parse_test_cases, DataDrivenTestCase, DataSuite
14+
from mypy.test.config import test_temp_dir
15+
from mypy.test.data import DataDrivenTestCase, DataSuite
1816
from mypy.test.helpers import (
1917
assert_string_arrays_equal, normalize_error_messages,
2018
retry_on_error, testcase_pyversion, update_testcase_output,
@@ -25,7 +23,7 @@
2523
from mypy import experiments
2624

2725
# List of files that contain test case descriptions.
28-
files = [
26+
typecheck_files = [
2927
'check-basic.test',
3028
'check-callable.test',
3129
'check-classes.test',
@@ -83,14 +81,9 @@
8381

8482

8583
class TypeCheckSuite(DataSuite):
86-
87-
@classmethod
88-
def cases(cls) -> List[DataDrivenTestCase]:
89-
c = [] # type: List[DataDrivenTestCase]
90-
for f in files:
91-
c += parse_test_cases(os.path.join(test_data_prefix, f),
92-
test_temp_dir, True)
93-
return c
84+
files = typecheck_files # type: typing.ClassVar[List[str]]
85+
base_path = test_temp_dir # type: typing.ClassVar[str]
86+
optional_out = True # type: typing.ClassVar[bool]
9487

9588
def run_case(self, testcase: DataDrivenTestCase) -> None:
9689
incremental = ('incremental' in testcase.name.lower()

mypy/test/testcmdline.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
import subprocess
1010
import sys
1111

12-
from typing import Tuple, List, Dict, Set
12+
from typing import List
13+
import typing
1314

14-
from mypy.myunit import Suite, SkipTestCaseException, AssertionFailure
15-
from mypy.test.config import test_data_prefix, test_temp_dir
15+
from mypy.myunit import AssertionFailure
16+
from mypy.test.config import test_temp_dir
1617
from mypy.test.data import fix_cobertura_filename
17-
from mypy.test.data import parse_test_cases, DataDrivenTestCase, DataSuite
18+
from mypy.test.data import DataDrivenTestCase, DataSuite
1819
from mypy.test.helpers import assert_string_arrays_equal, normalize_error_messages
1920
from mypy.version import __version__, base_version
2021

@@ -28,23 +29,17 @@
2829
]
2930

3031

31-
class PythonEvaluationSuite(DataSuite):
32-
33-
@classmethod
34-
def cases(cls) -> List[DataDrivenTestCase]:
35-
c = [] # type: List[DataDrivenTestCase]
36-
for f in cmdline_files:
37-
c += parse_test_cases(os.path.join(test_data_prefix, f),
38-
base_path=test_temp_dir,
39-
optional_out=True,
40-
native_sep=True)
41-
return c
32+
class PythonCmdlineSuite(DataSuite):
33+
files = cmdline_files # type: typing.ClassVar[List[str]]
34+
base_path = test_temp_dir # type: typing.ClassVar[str]
35+
optional_out = True # type: typing.ClassVar[bool]
36+
native_sep = True # type: typing.ClassVar[bool]
4237

4338
def run_case(self, testcase: DataDrivenTestCase):
44-
test_python_evaluation(testcase)
39+
test_python_cmdline(testcase)
4540

4641

47-
def test_python_evaluation(testcase: DataDrivenTestCase) -> None:
42+
def test_python_cmdline(testcase: DataDrivenTestCase) -> None:
4843
assert testcase.old_cwd is not None, "test was not properly set up"
4944
# Write the program to a file.
5045
program = '_program.py'

mypy/test/testdeps.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,24 @@
22

33
import os
44
from typing import List, Tuple, Dict, Optional
5+
import typing
56

67
from mypy import build
78
from mypy.build import BuildSource
89
from mypy.errors import CompileError
910
from mypy.nodes import MypyFile, Expression
1011
from mypy.options import Options
1112
from mypy.server.deps import get_dependencies
12-
from mypy.test.config import test_temp_dir, test_data_prefix
13-
from mypy.test.data import parse_test_cases, DataDrivenTestCase, DataSuite
13+
from mypy.test.config import test_temp_dir
14+
from mypy.test.data import DataDrivenTestCase, DataSuite
1415
from mypy.test.helpers import assert_string_arrays_equal
1516
from mypy.types import Type
1617

17-
files = [
18-
'deps.test'
19-
]
20-
2118

2219
class GetDependenciesSuite(DataSuite):
23-
24-
@classmethod
25-
def cases(cls) -> List[DataDrivenTestCase]:
26-
c = [] # type: List[DataDrivenTestCase]
27-
for f in files:
28-
c += parse_test_cases(os.path.join(test_data_prefix, f),
29-
test_temp_dir, True)
30-
return c
20+
files = ['deps.test'] # type: typing.ClassVar[List[str]]
21+
base_path = test_temp_dir # type: typing.ClassVar[str]
22+
optional_out = True # type: typing.ClassVar[bool]
3123

3224
def run_case(self, testcase: DataDrivenTestCase) -> None:
3325
src = '\n'.join(testcase.input)

mypy/test/testdiff.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,23 @@
22

33
import os
44
from typing import List, Tuple, Dict, Optional
5+
import typing
56

67
from mypy import build
78
from mypy.build import BuildSource
89
from mypy.errors import CompileError
910
from mypy.nodes import MypyFile
1011
from mypy.options import Options
1112
from mypy.server.astdiff import compare_symbol_tables
12-
from mypy.test.config import test_temp_dir, test_data_prefix
13-
from mypy.test.data import parse_test_cases, DataDrivenTestCase, DataSuite
13+
from mypy.test.config import test_temp_dir
14+
from mypy.test.data import DataDrivenTestCase, DataSuite
1415
from mypy.test.helpers import assert_string_arrays_equal
1516

1617

17-
files = [
18-
'diff.test'
19-
]
20-
21-
2218
class ASTDiffSuite(DataSuite):
23-
24-
@classmethod
25-
def cases(cls) -> List[DataDrivenTestCase]:
26-
c = [] # type: List[DataDrivenTestCase]
27-
for f in files:
28-
c += parse_test_cases(os.path.join(test_data_prefix, f),
29-
test_temp_dir, True)
30-
return c
19+
files = ['diff.test'] # type: typing.ClassVar[List[str]]
20+
base_path = test_temp_dir # type: typing.ClassVar[str]
21+
optional_out = True # type: typing.ClassVar[bool]
3122

3223
def run_case(self, testcase: DataDrivenTestCase) -> None:
3324
first_src = '\n'.join(testcase.input)

mypy/test/testfinegrained.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import re
1212
import shutil
1313
from typing import List, Tuple, Dict
14+
import typing
1415

1516
from mypy import build
1617
from mypy.build import BuildManager, BuildSource, Graph
@@ -29,19 +30,10 @@
2930
from mypy.util import short_type
3031

3132

32-
files = [
33-
'fine-grained.test'
34-
]
35-
36-
3733
class FineGrainedSuite(DataSuite):
38-
@classmethod
39-
def cases(cls) -> List[DataDrivenTestCase]:
40-
c = [] # type: List[DataDrivenTestCase]
41-
for f in files:
42-
c += parse_test_cases(os.path.join(test_data_prefix, f),
43-
test_temp_dir, True)
44-
return c
34+
files = ['fine-grained.test'] # type: typing.ClassVar[List[str]]
35+
base_path = test_temp_dir # type: typing.ClassVar[str]
36+
optional_out = True # type: typing.ClassVar[bool]
4537

4638
def run_case(self, testcase: DataDrivenTestCase) -> None:
4739
main_src = '\n'.join(testcase.input)

mypy/test/testmerge.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import shutil
55
from typing import List, Tuple, Dict, Optional
6+
import typing
67

78
from mypy import build
89
from mypy.build import BuildManager, BuildSource, State
@@ -15,19 +16,14 @@
1516
from mypy.server.subexpr import get_subexpressions
1617
from mypy.server.update import build_incremental_step, replace_modules_with_new_variants
1718
from mypy.strconv import StrConv, indent
18-
from mypy.test.config import test_temp_dir, test_data_prefix
19-
from mypy.test.data import parse_test_cases, DataDrivenTestCase, DataSuite
19+
from mypy.test.config import test_temp_dir
20+
from mypy.test.data import DataDrivenTestCase, DataSuite
2021
from mypy.test.helpers import assert_string_arrays_equal
2122
from mypy.test.testtypegen import ignore_node
2223
from mypy.types import TypeStrVisitor, Type
2324
from mypy.util import short_type, IdMapper
2425

2526

26-
files = [
27-
'merge.test'
28-
]
29-
30-
3127
# Which data structures to dump in a test case?
3228
SYMTABLE = 'SYMTABLE'
3329
TYPEINFO = ' TYPEINFO'
@@ -36,21 +32,17 @@
3632

3733

3834
class ASTMergeSuite(DataSuite):
35+
files = ['merge.test'] # type: typing.ClassVar[List[str]]
36+
base_path = test_temp_dir # type: typing.ClassVar[str]
37+
optional_out = True # type: typing.ClassVar[bool]
38+
3939
def __init__(self, *, update_data: bool) -> None:
4040
super().__init__(update_data=update_data)
4141
self.str_conv = StrConv(show_ids=True)
4242
assert self.str_conv.id_mapper is not None
4343
self.id_mapper = self.str_conv.id_mapper # type: IdMapper
4444
self.type_str_conv = TypeStrVisitor(self.id_mapper)
4545

46-
@classmethod
47-
def cases(cls) -> List[DataDrivenTestCase]:
48-
c = [] # type: List[DataDrivenTestCase]
49-
for f in files:
50-
c += parse_test_cases(os.path.join(test_data_prefix, f),
51-
test_temp_dir, True)
52-
return c
53-
5446
def run_case(self, testcase: DataDrivenTestCase) -> None:
5547
name = testcase.name
5648
# We use the test case name to decide which data structures to dump.

mypy/test/testparse.py

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,19 @@
11
"""Tests for the mypy parser."""
2-
3-
import os.path
4-
52
from typing import List
3+
import typing
64

75
from mypy import defaults
8-
from mypy.myunit import Suite, AssertionFailure
6+
from mypy.myunit import AssertionFailure
97
from mypy.test.helpers import assert_string_arrays_equal
10-
from mypy.test.data import parse_test_cases, DataDrivenTestCase, DataSuite
11-
from mypy.test import config
8+
from mypy.test.data import DataDrivenTestCase, DataSuite
129
from mypy.parse import parse
1310
from mypy.errors import CompileError
1411
from mypy.options import Options
1512

1613

1714
class ParserSuite(DataSuite):
18-
parse_files = ['parse.test',
19-
'parse-python2.test']
20-
21-
@classmethod
22-
def cases(cls) -> List[DataDrivenTestCase]:
23-
# The test case descriptions are stored in data files.
24-
c = [] # type: List[DataDrivenTestCase]
25-
for f in cls.parse_files:
26-
c += parse_test_cases(os.path.join(config.test_data_prefix, f))
27-
return c
15+
files = ['parse.test',
16+
'parse-python2.test'] # type: typing.ClassVar[List[str]]
2817

2918
def run_case(self, testcase: DataDrivenTestCase) -> None:
3019
test_parser(testcase)
@@ -61,11 +50,7 @@ def test_parser(testcase: DataDrivenTestCase) -> None:
6150

6251

6352
class ParseErrorSuite(DataSuite):
64-
@classmethod
65-
def cases(cls) -> List[DataDrivenTestCase]:
66-
# Test case descriptions are in an external file.
67-
return parse_test_cases(os.path.join(config.test_data_prefix,
68-
'parse-errors.test'))
53+
files = ['parse-errors.test'] # type: typing.ClassVar[List[str]]
6954

7055
def run_case(self, testcase: DataDrivenTestCase) -> None:
7156
test_parse_error(testcase)

0 commit comments

Comments
 (0)