Skip to content

Commit f178bd3

Browse files
authored
Merge pull request #1543 from asottile/filename-in-execution-error
include the file path in the plugin execution error
2 parents e704ab4 + d2333c4 commit f178bd3

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

src/flake8/checker.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,9 @@ def run_check(self, plugin: LoadedPlugin, **arguments: Any) -> Any:
356356
exc_info=True,
357357
)
358358
raise exceptions.PluginExecutionFailed(
359-
plugin_name=plugin.display_name, exception=all_exc
359+
filename=self.filename,
360+
plugin_name=plugin.display_name,
361+
exception=all_exc,
360362
)
361363

362364
@staticmethod

src/flake8/exceptions.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,24 @@ def __str__(self) -> str:
5454
class PluginExecutionFailed(Flake8Exception):
5555
"""The plugin failed during execution."""
5656

57-
FORMAT = '"%(name)s" failed during execution due to "%(exc)s"'
58-
59-
def __init__(self, plugin_name: str, exception: Exception) -> None:
57+
FORMAT = '{fname}: "{plugin}" failed during execution due to {exc!r}'
58+
59+
def __init__(
60+
self,
61+
filename: str,
62+
plugin_name: str,
63+
exception: Exception,
64+
) -> None:
6065
"""Utilize keyword arguments for message generation."""
66+
self.filename = filename
6167
self.plugin_name = plugin_name
6268
self.original_exception = exception
63-
super().__init__(plugin_name, exception)
69+
super().__init__(filename, plugin_name, exception)
6470

6571
def __str__(self) -> str:
6672
"""Format our exception message."""
67-
return self.FORMAT % {
68-
"name": self.plugin_name,
69-
"exc": self.original_exception,
70-
}
73+
return self.FORMAT.format(
74+
fname=self.filename,
75+
plugin=self.plugin_name,
76+
exc=self.original_exception,
77+
)

tests/unit/test_exceptions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
exception=ValueError("boom!"),
1919
),
2020
exceptions.PluginExecutionFailed(
21+
filename="filename.py",
2122
plugin_name="plugin_name",
2223
exception=ValueError("boom!"),
2324
),

tests/unit/test_file_checker.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,10 @@ def test_raises_exception_on_failed_plugin(tmp_path, default_options):
5454
plugins=finder.Checkers([], [], []),
5555
options=default_options,
5656
)
57-
with pytest.raises(flake8.exceptions.PluginExecutionFailed):
57+
with pytest.raises(flake8.exceptions.PluginExecutionFailed) as excinfo:
5858
fchecker.run_check(plugin)
59+
expected = (
60+
f'{fname}: "plugin-name[X]" failed during execution '
61+
f"due to ValueError()"
62+
)
63+
assert str(excinfo.value) == expected

0 commit comments

Comments
 (0)