Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/flake8/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def build_logical_line_tokens(self) -> _Logical: # noqa: C901

def build_ast(self) -> ast.AST:
"""Build an abstract syntax tree from the list of lines."""
return ast.parse("".join(self.lines))
return ast.parse("".join(self.lines), self.filename)

def build_logical_line(self) -> tuple[str, str, _LogicalMapping]:
"""Build a logical line from the current tokens list."""
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/test_file_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,17 @@ def test_build_ast(default_options):
assert isinstance(module, ast.Module)


def test_build_ast_preserves_filename_in_warnings(default_options):
file_processor = processor.FileProcessor(
'fname.py', default_options, lines=[r"s = '\d'\n"],
)

# the SyntaxWarning is upgraded to error due to `filterwarnings`
with pytest.raises(SyntaxError) as excinfo:
file_processor.build_ast()
assert excinfo.value.filename == 'fname.py'


def test_next_logical_line_updates_the_previous_logical_line(default_options):
"""Verify that we update our tracking of the previous logical line."""
file_processor = processor.FileProcessor(
Expand Down
Loading