Skip to content

Commit b39edc2

Browse files
ddfishergvanrossum
authored andcommitted
Have test files specify flags instead of option names (#2066)
1 parent 3327563 commit b39edc2

15 files changed

+99
-105
lines changed

mypy/main.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,10 @@ def parse_version(v: str) -> Tuple[int, int]:
116116
"Invalid python version '{}' (expected format: 'x.y')".format(v))
117117

118118

119-
def process_options(args: List[str]) -> Tuple[List[BuildSource], Options]:
120-
"""Process command line arguments.
121-
122-
Return (mypy program path (or None),
123-
module to run as script (or None),
124-
parsed flags)
125-
"""
119+
def process_options(args: List[str],
120+
require_targets: bool = True
121+
) -> Tuple[List[BuildSource], Options]:
122+
"""Parse command line arguments."""
126123

127124
# Make the help output a little less jarring.
128125
help_factory = (lambda prog:
@@ -259,14 +256,15 @@ def process_options(args: List[str]) -> Tuple[List[BuildSource], Options]:
259256
file=sys.stderr)
260257

261258
# Check for invalid argument combinations.
262-
code_methods = sum(bool(c) for c in [special_opts.modules,
263-
special_opts.command,
264-
special_opts.package,
265-
special_opts.files])
266-
if code_methods == 0:
267-
parser.error("Missing target module, package, files, or command.")
268-
elif code_methods > 1:
269-
parser.error("May only specify one of: module, package, files, or command.")
259+
if require_targets:
260+
code_methods = sum(bool(c) for c in [special_opts.modules,
261+
special_opts.command,
262+
special_opts.package,
263+
special_opts.files])
264+
if code_methods == 0:
265+
parser.error("Missing target module, package, files, or command.")
266+
elif code_methods > 1:
267+
parser.error("May only specify one of: module, package, files, or command.")
270268

271269
# Set build flags.
272270
if special_opts.strict_optional:

mypy/test/testargs.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313

1414
class ArgSuite(Suite):
1515
def test_coherence(self):
16-
# We have to special case Options.BuildType because we're required to
17-
# set a target
1816
options = Options()
19-
options.build_type = BuildType.PROGRAM_TEXT
20-
_, parsed_options = process_options(['-c', 'cmd'])
17+
_, parsed_options = process_options([], require_targets=False)
2118
assert_equal(options, parsed_options)

mypy/test/testcheck.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from typing import Tuple, List, Dict, Set
1010

1111
from mypy import build, defaults
12-
from mypy.main import parse_version
12+
from mypy.main import parse_version, process_options
1313
from mypy.build import BuildSource, find_module_clear_caches
1414
from mypy.myunit import AssertionFailure
1515
from mypy.test.config import test_temp_dir, test_data_prefix
@@ -278,22 +278,21 @@ def parse_module(self, program_text: str, incremental: int = 0) -> List[Tuple[st
278278

279279
def parse_options(self, program_text: str, testcase: DataDrivenTestCase) -> Options:
280280
options = Options()
281-
flags = re.search('# options: (.*)$', program_text, flags=re.MULTILINE)
282-
version_flag = re.search('# pyversion: (.*)$', program_text, flags=re.MULTILINE)
283-
platform_flag = re.search('# platform: (.*)$', program_text, flags=re.MULTILINE)
281+
flags = re.search('# flags: (.*)$', program_text, flags=re.MULTILINE)
284282

283+
flag_list = None
285284
if flags:
286-
options_to_enable = flags.group(1).split()
287-
for opt in options_to_enable:
288-
setattr(options, opt, True)
289-
290-
# Allow custom pyversion comment to override testcase_pyversion
291-
if version_flag:
292-
options.python_version = parse_version(version_flag.group(1))
285+
flag_list = flags.group(1).split()
286+
targets, options = process_options(flag_list, require_targets=False)
287+
if targets:
288+
# TODO: support specifying targets via the flags pragma
289+
raise RuntimeError('Specifying targets via the flags pragma is not supported.')
293290
else:
294-
options.python_version = testcase_pyversion(testcase.file, testcase.name)
291+
options = Options()
295292

296-
if platform_flag:
297-
options.platform = platform_flag.group(1)
293+
# Allow custom python version to override testcase_pyversion
294+
if (not flag_list or
295+
all(flag not in flag_list for flag in ['--python-version', '-2', '--py2'])):
296+
options.python_version = testcase_pyversion(testcase.file, testcase.name)
298297

299298
return options

test-data/unit/check-async-await.test

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
-- ---------------------------------------
33

44
[case testAsyncDefPass]
5-
# options: fast_parser
5+
# flags: --fast-parser
66
async def f() -> int:
77
pass
88
[builtins fixtures/async_await.pyi]
99

1010
[case testAsyncDefReturn]
11-
# options: fast_parser
11+
# flags: --fast-parser
1212
async def f() -> int:
1313
return 0
1414
reveal_type(f()) # E: Revealed type is 'typing.Awaitable[builtins.int]'
1515
[builtins fixtures/async_await.pyi]
1616

1717
[case testAwaitCoroutine]
18-
# options: fast_parser
18+
# flags: --fast-parser
1919
async def f() -> int:
2020
x = await f()
2121
reveal_type(x) # E: Revealed type is 'builtins.int*'
@@ -25,7 +25,7 @@ async def f() -> int:
2525
main: note: In function "f":
2626

2727
[case testAwaitDefaultContext]
28-
# options: fast_parser
28+
# flags: --fast-parser
2929
from typing import TypeVar
3030
T = TypeVar('T')
3131
async def f(x: T) -> T:
@@ -37,7 +37,7 @@ main: note: In function "f":
3737
main:6: error: Revealed type is 'T`-1'
3838

3939
[case testAwaitAnyContext]
40-
# options: fast_parser
40+
# flags: --fast-parser
4141
from typing import Any, TypeVar
4242
T = TypeVar('T')
4343
async def f(x: T) -> T:
@@ -49,7 +49,7 @@ main: note: In function "f":
4949
main:6: error: Revealed type is 'Any'
5050

5151
[case testAwaitExplicitContext]
52-
# options: fast_parser
52+
# flags: --fast-parser
5353
from typing import TypeVar
5454
T = TypeVar('T')
5555
async def f(x: T) -> T:
@@ -61,7 +61,7 @@ main:5: error: Argument 1 to "f" has incompatible type "T"; expected "int"
6161
main:6: error: Revealed type is 'builtins.int'
6262

6363
[case testAwaitGeneratorError]
64-
# options: fast_parser
64+
# flags: --fast-parser
6565
from typing import Any, Generator
6666
def g() -> Generator[int, None, str]:
6767
yield 0
@@ -74,7 +74,7 @@ main: note: In function "f":
7474
main:7: error: Incompatible types in await (actual type Generator[int, None, str], expected type "Awaitable")
7575

7676
[case testAwaitIteratorError]
77-
# options: fast_parser
77+
# flags: --fast-parser
7878
from typing import Any, Iterator
7979
def g() -> Iterator[Any]:
8080
yield
@@ -86,7 +86,7 @@ main: note: In function "f":
8686
main:6: error: Incompatible types in await (actual type Iterator[Any], expected type "Awaitable")
8787

8888
[case testAwaitArgumentError]
89-
# options: fast_parser
89+
# flags: --fast-parser
9090
def g() -> int:
9191
return 0
9292
async def f() -> int:
@@ -98,7 +98,7 @@ main: note: In function "f":
9898
main:5: error: Incompatible types in await (actual type "int", expected type "Awaitable")
9999

100100
[case testAwaitResultError]
101-
# options: fast_parser
101+
# flags: --fast-parser
102102
async def g() -> int:
103103
return 0
104104
async def f() -> str:
@@ -109,7 +109,7 @@ main: note: In function "f":
109109
main:5: error: Incompatible types in assignment (expression has type "int", variable has type "str")
110110

111111
[case testAwaitReturnError]
112-
# options: fast_parser
112+
# flags: --fast-parser
113113
async def g() -> int:
114114
return 0
115115
async def f() -> str:
@@ -121,7 +121,7 @@ main: note: In function "f":
121121
main:6: error: Incompatible return value type (got "int", expected "str")
122122

123123
[case testAsyncFor]
124-
# options: fast_parser
124+
# flags: --fast-parser
125125
from typing import AsyncIterator
126126
class C(AsyncIterator[int]):
127127
async def __anext__(self) -> int: return 0
@@ -133,7 +133,7 @@ async def f() -> None:
133133
main: note: In function "f":
134134

135135
[case testAsyncForError]
136-
# options: fast_parser
136+
# flags: --fast-parser
137137
from typing import AsyncIterator
138138
async def f() -> None:
139139
async for x in [1]:
@@ -145,7 +145,7 @@ main:4: error: AsyncIterable expected
145145
main:4: error: List[int] has no attribute "__aiter__"
146146

147147
[case testAsyncWith]
148-
# options: fast_parser
148+
# flags: --fast-parser
149149
class C:
150150
async def __aenter__(self) -> int: pass
151151
async def __aexit__(self, x, y, z) -> None: pass
@@ -157,7 +157,7 @@ async def f() -> None:
157157
main: note: In function "f":
158158

159159
[case testAsyncWithError]
160-
# options: fast_parser
160+
# flags: --fast-parser
161161
class C:
162162
def __enter__(self) -> int: pass
163163
def __exit__(self, x, y, z) -> None: pass
@@ -171,7 +171,7 @@ main:6: error: "C" has no attribute "__aenter__"; maybe "__enter__"?
171171
main:6: error: "C" has no attribute "__aexit__"; maybe "__exit__"?
172172

173173
[case testAsyncWithErrorBadAenter]
174-
# options: fast_parser
174+
# flags: --fast-parser
175175
class C:
176176
def __aenter__(self) -> int: pass
177177
async def __aexit__(self, x, y, z) -> None: pass
@@ -183,7 +183,7 @@ async def f() -> None:
183183
main: note: In function "f":
184184

185185
[case testAsyncWithErrorBadAenter2]
186-
# options: fast_parser
186+
# flags: --fast-parser
187187
class C:
188188
def __aenter__(self) -> None: pass
189189
async def __aexit__(self, x, y, z) -> None: pass
@@ -195,7 +195,7 @@ async def f() -> None:
195195
main: note: In function "f":
196196

197197
[case testAsyncWithErrorBadAexit]
198-
# options: fast_parser
198+
# flags: --fast-parser
199199
class C:
200200
async def __aenter__(self) -> int: pass
201201
def __aexit__(self, x, y, z) -> int: pass
@@ -207,7 +207,7 @@ async def f() -> None:
207207
main: note: In function "f":
208208

209209
[case testAsyncWithErrorBadAexit2]
210-
# options: fast_parser
210+
# flags: --fast-parser
211211
class C:
212212
async def __aenter__(self) -> int: pass
213213
def __aexit__(self, x, y, z) -> None: pass
@@ -219,7 +219,7 @@ async def f() -> None:
219219
main: note: In function "f":
220220

221221
[case testNoYieldInAsyncDef]
222-
# options: fast_parser
222+
# flags: --fast-parser
223223
async def f():
224224
yield None
225225
async def g():
@@ -236,7 +236,7 @@ main: note: In function "h":
236236
main:7: error: 'yield' in async function
237237

238238
[case testNoYieldFromInAsyncDef]
239-
# options: fast_parser
239+
# flags: --fast-parser
240240
async def f():
241241
yield from []
242242
async def g():
@@ -249,12 +249,12 @@ main: note: In function "g":
249249
main:5: error: 'yield from' in async function
250250

251251
[case testNoAsyncDefInPY2_python2]
252-
# options: fast_parser
252+
# flags: --fast-parser
253253
async def f(): # E: invalid syntax
254254
pass
255255

256256
[case testYieldFromNoAwaitable]
257-
# options: fast_parser
257+
# flags: --fast-parser
258258
from typing import Any, Generator
259259
async def f() -> str:
260260
return ''
@@ -267,7 +267,7 @@ main: note: In function "g":
267267
main:6: error: "yield from" can't be applied to Awaitable[str]
268268

269269
[case testAwaitableSubclass]
270-
# options: fast_parser
270+
# flags: --fast-parser
271271
from typing import Any, AsyncIterator, Awaitable, Generator
272272
class A(Awaitable[int]):
273273
def __await__(self) -> Generator[Any, None, int]:
@@ -295,7 +295,7 @@ async def main() -> None:
295295
main: note: In function "main":
296296

297297
[case testYieldTypeCheckInDecoratedCoroutine]
298-
# options: fast_parser
298+
# flags: --fast-parser
299299
from typing import Generator
300300
from types import coroutine
301301
@coroutine
@@ -316,7 +316,7 @@ main: note: In function "f":
316316
-- ------------------------------------------
317317

318318
[case testFullCoroutineMatrix]
319-
# options: fast_parser suppress_error_context
319+
# flags: --fast-parser --suppress-error-context
320320
from typing import Any, AsyncIterator, Awaitable, Generator, Iterator
321321
from types import coroutine
322322

test-data/unit/check-expressions.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,12 +1583,12 @@ None < None # E: Unsupported left operand type for < (None)
15831583
[builtins fixtures/ops.pyi]
15841584

15851585
[case testDictWithStarExpr]
1586-
# options: fast_parser
1586+
# flags: --fast-parser
15871587
b = {'z': 26, *a} # E: invalid syntax
15881588
[builtins fixtures/dict.pyi]
15891589

15901590
[case testDictWithStarStarExpr]
1591-
# options: fast_parser
1591+
# flags: --fast-parser
15921592
from typing import Dict
15931593
a = {'a': 1}
15941594
b = {'z': 26, **a}

0 commit comments

Comments
 (0)