13
13
14
14
from .test_flake8_async import initialize_options
15
15
16
+ try :
17
+ import flake8
18
+ except ImportError :
19
+ flake8 = None # type: ignore[assignment]
20
+
16
21
EXAMPLE_PY_TEXT = """import trio
17
22
with trio.move_on_after(10):
18
23
...
@@ -140,7 +145,7 @@ def test_run_100_autofix(
140
145
141
146
def test_114_raises_on_invalid_parameter (capsys : pytest .CaptureFixture [str ]):
142
147
plugin = Plugin (ast .AST (), [])
143
- # flake8 will reraise ArgumentError as SystemExit
148
+ # argparse will reraise ArgumentTypeError as SystemExit
144
149
for arg in "blah.foo" , "foo*" , "*" :
145
150
with pytest .raises (SystemExit ):
146
151
initialize_options (plugin , args = [f"--startable-in-context-manager={ arg } " ])
@@ -159,6 +164,7 @@ def test_200_options(capsys: pytest.CaptureFixture[str]):
159
164
assert all (word in err for word in (str (i ), arg , "->" ))
160
165
161
166
167
+ @pytest .mark .skipif (flake8 is None , reason = "flake8 is not installed" )
162
168
def test_anyio_from_config (tmp_path : Path , capsys : pytest .CaptureFixture [str ]):
163
169
assert tmp_path .joinpath (".flake8" ).write_text (
164
170
"""
@@ -228,6 +234,7 @@ async def foo():
228
234
)
229
235
230
236
237
+ @pytest .mark .skipif (flake8 is None , reason = "flake8 is not installed" )
231
238
def test_200_from_config_flake8_internals (
232
239
tmp_path : Path , capsys : pytest .CaptureFixture [str ]
233
240
):
@@ -254,6 +261,7 @@ def test_200_from_config_flake8_internals(
254
261
assert err_msg == out
255
262
256
263
264
+ @pytest .mark .skipif (flake8 is None , reason = "flake8 is not installed" )
257
265
def test_200_from_config_subprocess (tmp_path : Path ):
258
266
err_msg = _test_async200_from_config_common (tmp_path )
259
267
res = subprocess .run (["flake8" ], cwd = tmp_path , capture_output = True , check = False )
@@ -262,6 +270,7 @@ def test_200_from_config_subprocess(tmp_path: Path):
262
270
assert res .stdout == err_msg .encode ("ascii" )
263
271
264
272
273
+ @pytest .mark .skipif (flake8 is None , reason = "flake8 is not installed" )
265
274
def test_async200_from_config_subprocess (tmp_path : Path ):
266
275
err_msg = _test_async200_from_config_common (tmp_path , code = "trio200" )
267
276
res = subprocess .run (["flake8" ], cwd = tmp_path , capture_output = True , check = False )
@@ -273,6 +282,7 @@ def test_async200_from_config_subprocess(tmp_path: Path):
273
282
assert res .stdout == err_msg .encode ("ascii" )
274
283
275
284
285
+ @pytest .mark .skipif (flake8 is None , reason = "flake8 is not installed" )
276
286
def test_async200_from_config_subprocess_cli_ignore (tmp_path : Path ):
277
287
_ = _test_async200_from_config_common (tmp_path )
278
288
res = subprocess .run (
@@ -288,6 +298,20 @@ def test_async200_from_config_subprocess_cli_ignore(tmp_path: Path):
288
298
289
299
290
300
def test_900_default_off (capsys : pytest .CaptureFixture [str ]):
301
+ res = subprocess .run (
302
+ ["flake8-async" , "tests/eval_files/async900.py" ],
303
+ capture_output = True ,
304
+ check = False ,
305
+ encoding = "utf8" ,
306
+ )
307
+ assert res .returncode == 1
308
+ assert not res .stderr
309
+ assert "ASYNC124" in res .stdout
310
+ assert "ASYNC900" not in res .stdout
311
+
312
+
313
+ @pytest .mark .skipif (flake8 is None , reason = "flake8 is not installed" )
314
+ def test_900_default_off_flake8 (capsys : pytest .CaptureFixture [str ]):
291
315
from flake8 .main .cli import main
292
316
293
317
returnvalue = main (
@@ -303,11 +327,18 @@ def test_900_default_off(capsys: pytest.CaptureFixture[str]):
303
327
304
328
305
329
def test_910_can_be_selected (tmp_path : Path ):
330
+ """Check if flake8 allows us to --select our 5-letter code.
331
+
332
+ But we can run with --enable regardless.
333
+ """
306
334
myfile = tmp_path .joinpath ("foo.py" )
307
335
myfile .write_text ("""async def foo():\n print()""" )
308
336
337
+ binary = "flake8-async" if flake8 is None else "flake8"
338
+ select_enable = "enable" if flake8 is None else "select"
339
+
309
340
res = subprocess .run (
310
- ["flake8" , "--select =ASYNC910" , "foo.py" ],
341
+ [binary , f "--{ select_enable } =ASYNC910" , "foo.py" ],
311
342
cwd = tmp_path ,
312
343
capture_output = True ,
313
344
check = False ,
@@ -384,6 +415,7 @@ def _helper(*args: str, error: bool = False, autofix: bool = False) -> None:
384
415
)
385
416
386
417
418
+ @pytest .mark .skipif (flake8 is None , reason = "flake8 is not installed" )
387
419
def test_flake8_plugin_with_autofix_fails (tmp_path : Path ):
388
420
write_examplepy (tmp_path )
389
421
res = subprocess .run (
@@ -453,7 +485,9 @@ def test_disable_noqa_ast(
453
485
)
454
486
455
487
488
+ @pytest .mark .skipif (flake8 is None , reason = "flake8 is not installed" )
456
489
def test_config_select_error_code (tmp_path : Path ) -> None :
490
+ # this ... seems to work? I'm confused
457
491
assert tmp_path .joinpath (".flake8" ).write_text (
458
492
"""
459
493
[flake8]
@@ -469,6 +503,7 @@ def test_config_select_error_code(tmp_path: Path) -> None:
469
503
470
504
471
505
# flake8>=6 enforces three-letter error codes in config
506
+ @pytest .mark .skipif (flake8 is None , reason = "flake8 is not installed" )
472
507
def test_config_ignore_error_code (tmp_path : Path ) -> None :
473
508
assert tmp_path .joinpath (".flake8" ).write_text (
474
509
"""
@@ -490,6 +525,7 @@ def test_config_ignore_error_code(tmp_path: Path) -> None:
490
525
491
526
492
527
# flake8>=6 enforces three-letter error codes in config
528
+ @pytest .mark .skipif (flake8 is None , reason = "flake8 is not installed" )
493
529
def test_config_extend_ignore_error_code (tmp_path : Path ) -> None :
494
530
assert tmp_path .joinpath (".flake8" ).write_text (
495
531
"""
@@ -511,6 +547,7 @@ def test_config_extend_ignore_error_code(tmp_path: Path) -> None:
511
547
assert res .returncode == 1
512
548
513
549
550
+ @pytest .mark .skipif (flake8 is None , reason = "flake8 is not installed" )
514
551
# but make sure we can disable selected codes
515
552
def test_config_disable_error_code (tmp_path : Path ) -> None :
516
553
# select ASYNC200 and create file that induces ASYNC200
0 commit comments