diff --git a/flake8_async/__init__.py b/flake8_async/__init__.py index ab66ef4..70ec57d 100644 --- a/flake8_async/__init__.py +++ b/flake8_async/__init__.py @@ -106,7 +106,19 @@ def main() -> int: ] any_error = False for file in all_filenames: - plugin = Plugin.from_filename(file) + try: + plugin = Plugin.from_filename(file) + except SyntaxError as error: + location = file + if error.lineno is not None and error.offset is not None: + location = f"{file}:{error.lineno}:{error.offset}" + python_version = f"{sys.version_info.major}.{sys.version_info.minor}" + print( + f"{location}: SyntaxError (Python {python_version}): {error.msg}", + file=sys.stderr, + ) + any_error = True + continue for error in sorted(plugin.run()): print(f"{file}:{error}") any_error = True diff --git a/tests/test_config_and_args.py b/tests/test_config_and_args.py index 5547e49..f92a816 100644 --- a/tests/test_config_and_args.py +++ b/tests/test_config_and_args.py @@ -68,6 +68,48 @@ def test_run_flake8_async(tmp_path: Path): assert res.stdout == EXAMPLE_PY_ERROR.encode("ascii") +def test_run_flake8_async_continues_after_syntax_error(tmp_path: Path): + invalid = tmp_path / "invalid.py" + invalid.write_text("if True\n pass\n") + write_examplepy(tmp_path) + res = subprocess.run( + ["flake8-async", "--autofix=ASYNC", "./invalid.py", "./example.py"], + cwd=tmp_path, + capture_output=True, + check=False, + text=True, + ) + + python_version = f"{sys.version_info.major}.{sys.version_info.minor}" + assert res.returncode == 1 + assert ( + res.stderr + == f"./invalid.py:1:8: SyntaxError (Python {python_version}): expected ':'\n" + ) + assert res.stdout == EXAMPLE_PY_ERROR + assert invalid.read_text() == "if True\n pass\n" + assert_autofixed(tmp_path) + + +def test_run_flake8_async_syntax_error_without_location(tmp_path: Path): + write_examplepy(tmp_path, text="# coding: nonexistent_codec\n") + res = subprocess.run( + ["flake8-async", "./example.py"], + cwd=tmp_path, + capture_output=True, + check=False, + text=True, + ) + + python_version = f"{sys.version_info.major}.{sys.version_info.minor}" + assert res.returncode == 1 + assert res.stderr.startswith( + f"./example.py: SyntaxError (Python {python_version}): " + ) + assert res.stderr.endswith("nonexistent_codec\n") + assert not res.stdout + + def test_systemexit_0( tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str] ):