Skip to content

Commit

Permalink
Include modname in AST warnings (#2380)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p authored Feb 9, 2024
1 parent 5accd50 commit 35dba66
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Release date: TBA
to the ``PartialFunction`` and ``Property`` constructors have been removed (call
``postinit(doc_node=...)`` instead.)

* Include modname in AST warnings. Useful for ``invalid escape sequence`` warnings
with Python 3.12.



What's New in astroid 3.0.3?
Expand Down
6 changes: 5 additions & 1 deletion astroid/_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ class ParserModule(NamedTuple):
bin_op_classes: dict[type[ast.operator], str]
context_classes: dict[type[ast.expr_context], Context]

def parse(self, string: str, type_comments: bool = True) -> ast.Module:
def parse(
self, string: str, type_comments: bool = True, filename: str | None = None
) -> ast.Module:
if filename:
return ast.parse(string, filename=filename, type_comments=type_comments)
return ast.parse(string, type_comments=type_comments)


Expand Down
10 changes: 7 additions & 3 deletions astroid/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ def _data_build(
) -> tuple[nodes.Module, rebuilder.TreeRebuilder]:
"""Build tree node from data and add some informations."""
try:
node, parser_module = _parse_string(data, type_comments=True)
node, parser_module = _parse_string(
data, type_comments=True, modname=modname
)
except (TypeError, ValueError, SyntaxError) as exc:
raise AstroidSyntaxError(
"Parsing Python code failed:\n{error}",
Expand Down Expand Up @@ -466,11 +468,13 @@ def _extract_single_node(code: str, module_name: str = "") -> nodes.NodeNG:


def _parse_string(
data: str, type_comments: bool = True
data: str, type_comments: bool = True, modname: str | None = None
) -> tuple[ast.Module, ParserModule]:
parser_module = get_parser_module(type_comments=type_comments)
try:
parsed = parser_module.parse(data + "\n", type_comments=type_comments)
parsed = parser_module.parse(
data + "\n", type_comments=type_comments, filename=modname
)
except SyntaxError as exc:
# If the type annotations are misplaced for some reason, we do not want
# to fail the entire parsing of the file, so we need to retry the parsing without
Expand Down
12 changes: 12 additions & 0 deletions tests/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,18 @@ def test_data_build_invalid_x_escape(self) -> None:
with self.assertRaises(AstroidSyntaxError):
self.builder.string_build('"\\x1"')

def test_data_build_error_filename(self) -> None:
"""Check that error filename is set to modname if given."""
with pytest.raises(AstroidSyntaxError, match="invalid escape sequence") as ctx:
self.builder.string_build("'\\d+\\.\\d+'")
assert isinstance(ctx.value.error, SyntaxError)
assert ctx.value.error.filename == "<unknown>"

with pytest.raises(AstroidSyntaxError, match="invalid escape sequence") as ctx:
self.builder.string_build("'\\d+\\.\\d+'", modname="mymodule")
assert isinstance(ctx.value.error, SyntaxError)
assert ctx.value.error.filename == "mymodule"

def test_missing_newline(self) -> None:
"""Check that a file with no trailing new line is parseable."""
resources.build_file("data/noendingnewline.py")
Expand Down

0 comments on commit 35dba66

Please sign in to comment.