1313
1414from .test_flake8_async import initialize_options
1515
16+ try :
17+ import flake8
18+ except ImportError :
19+ flake8 = None # type: ignore[assignment]
20+
1621EXAMPLE_PY_TEXT = """import trio
1722with trio.move_on_after(10):
1823 ...
@@ -140,7 +145,7 @@ def test_run_100_autofix(
140145
141146def test_114_raises_on_invalid_parameter (capsys : pytest .CaptureFixture [str ]):
142147 plugin = Plugin (ast .AST (), [])
143- # flake8 will reraise ArgumentError as SystemExit
148+ # argparse will reraise ArgumentTypeError as SystemExit
144149 for arg in "blah.foo" , "foo*" , "*" :
145150 with pytest .raises (SystemExit ):
146151 initialize_options (plugin , args = [f"--startable-in-context-manager={ arg } " ])
@@ -159,6 +164,7 @@ def test_200_options(capsys: pytest.CaptureFixture[str]):
159164 assert all (word in err for word in (str (i ), arg , "->" ))
160165
161166
167+ @pytest .mark .skipif (flake8 is None , reason = "flake8 is not installed" )
162168def test_anyio_from_config (tmp_path : Path , capsys : pytest .CaptureFixture [str ]):
163169 assert tmp_path .joinpath (".flake8" ).write_text (
164170 """
@@ -228,6 +234,7 @@ async def foo():
228234 )
229235
230236
237+ @pytest .mark .skipif (flake8 is None , reason = "flake8 is not installed" )
231238def test_200_from_config_flake8_internals (
232239 tmp_path : Path , capsys : pytest .CaptureFixture [str ]
233240):
@@ -254,6 +261,7 @@ def test_200_from_config_flake8_internals(
254261 assert err_msg == out
255262
256263
264+ @pytest .mark .skipif (flake8 is None , reason = "flake8 is not installed" )
257265def test_200_from_config_subprocess (tmp_path : Path ):
258266 err_msg = _test_async200_from_config_common (tmp_path )
259267 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):
262270 assert res .stdout == err_msg .encode ("ascii" )
263271
264272
273+ @pytest .mark .skipif (flake8 is None , reason = "flake8 is not installed" )
265274def test_async200_from_config_subprocess (tmp_path : Path ):
266275 err_msg = _test_async200_from_config_common (tmp_path , code = "trio200" )
267276 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):
273282 assert res .stdout == err_msg .encode ("ascii" )
274283
275284
285+ @pytest .mark .skipif (flake8 is None , reason = "flake8 is not installed" )
276286def test_async200_from_config_subprocess_cli_ignore (tmp_path : Path ):
277287 _ = _test_async200_from_config_common (tmp_path )
278288 res = subprocess .run (
@@ -288,6 +298,20 @@ def test_async200_from_config_subprocess_cli_ignore(tmp_path: Path):
288298
289299
290300def 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 ]):
291315 from flake8 .main .cli import main
292316
293317 returnvalue = main (
@@ -303,11 +327,18 @@ def test_900_default_off(capsys: pytest.CaptureFixture[str]):
303327
304328
305329def 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+ """
306334 myfile = tmp_path .joinpath ("foo.py" )
307335 myfile .write_text ("""async def foo():\n print()""" )
308336
337+ binary = "flake8-async" if flake8 is None else "flake8"
338+ select_enable = "enable" if flake8 is None else "select"
339+
309340 res = subprocess .run (
310- ["flake8" , "--select =ASYNC910" , "foo.py" ],
341+ [binary , f "--{ select_enable } =ASYNC910" , "foo.py" ],
311342 cwd = tmp_path ,
312343 capture_output = True ,
313344 check = False ,
@@ -384,6 +415,7 @@ def _helper(*args: str, error: bool = False, autofix: bool = False) -> None:
384415 )
385416
386417
418+ @pytest .mark .skipif (flake8 is None , reason = "flake8 is not installed" )
387419def test_flake8_plugin_with_autofix_fails (tmp_path : Path ):
388420 write_examplepy (tmp_path )
389421 res = subprocess .run (
@@ -453,7 +485,9 @@ def test_disable_noqa_ast(
453485 )
454486
455487
488+ @pytest .mark .skipif (flake8 is None , reason = "flake8 is not installed" )
456489def test_config_select_error_code (tmp_path : Path ) -> None :
490+ # this ... seems to work? I'm confused
457491 assert tmp_path .joinpath (".flake8" ).write_text (
458492 """
459493[flake8]
@@ -469,6 +503,7 @@ def test_config_select_error_code(tmp_path: Path) -> None:
469503
470504
471505# flake8>=6 enforces three-letter error codes in config
506+ @pytest .mark .skipif (flake8 is None , reason = "flake8 is not installed" )
472507def test_config_ignore_error_code (tmp_path : Path ) -> None :
473508 assert tmp_path .joinpath (".flake8" ).write_text (
474509 """
@@ -490,6 +525,7 @@ def test_config_ignore_error_code(tmp_path: Path) -> None:
490525
491526
492527# flake8>=6 enforces three-letter error codes in config
528+ @pytest .mark .skipif (flake8 is None , reason = "flake8 is not installed" )
493529def test_config_extend_ignore_error_code (tmp_path : Path ) -> None :
494530 assert tmp_path .joinpath (".flake8" ).write_text (
495531 """
@@ -511,6 +547,7 @@ def test_config_extend_ignore_error_code(tmp_path: Path) -> None:
511547 assert res .returncode == 1
512548
513549
550+ @pytest .mark .skipif (flake8 is None , reason = "flake8 is not installed" )
514551# but make sure we can disable selected codes
515552def test_config_disable_error_code (tmp_path : Path ) -> None :
516553 # select ASYNC200 and create file that induces ASYNC200
0 commit comments