From 1491385de20db3ad1e9ce6b486d7fbc70ed77ae1 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Mon, 19 Feb 2024 13:56:07 +0100 Subject: [PATCH 1/4] Suppress SyntaxWarnings when parsing modules --- ChangeLog | 4 ++++ astroid/builder.py | 9 ++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index bf230c9864..2b1865a12d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -27,6 +27,10 @@ Release date: TBA * Include modname in AST warnings. Useful for ``invalid escape sequence`` warnings with Python 3.12. +* Suppress ``SyntaxWarnings`` when parsing modules. + + Closes pylint-dev/pylint#9322 + What's New in astroid 3.0.3? diff --git a/astroid/builder.py b/astroid/builder.py index 1a4c10ecfe..0950c9d862 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -14,6 +14,7 @@ import os import textwrap import types +import warnings from collections.abc import Iterator, Sequence from io import TextIOWrapper from tokenize import detect_encoding @@ -173,9 +174,11 @@ 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, modname=modname - ) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", SyntaxWarning) + 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}", From 908ecdbc17818cca82b926c811bd2429cead4bc6 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Mon, 19 Feb 2024 14:05:04 +0100 Subject: [PATCH 2/4] Fix test --- tests/test_builder.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_builder.py b/tests/test_builder.py index 84ac65e099..e496f50e42 100644 --- a/tests/test_builder.py +++ b/tests/test_builder.py @@ -19,7 +19,7 @@ import pytest from astroid import Instance, builder, nodes, test_utils, util -from astroid.const import IS_PYPY, PY38, PY39_PLUS, PYPY_7_3_11_PLUS +from astroid.const import IS_PYPY, PY38, PY39_PLUS, PY312_PLUS, PYPY_7_3_11_PLUS from astroid.exceptions import ( AstroidBuildingError, AstroidSyntaxError, @@ -416,6 +416,7 @@ def test_data_build_invalid_x_escape(self) -> None: with self.assertRaises(AstroidSyntaxError): self.builder.string_build('"\\x1"') + @pytest.mark.skipif(PY312_PLUS, reason="Ignoring SyntaxWarnings triggered by 3.12+") 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: From dd1e864773b95aafa5b274aa6cf1a67361186321 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Mon, 19 Feb 2024 14:15:57 +0100 Subject: [PATCH 3/4] Filter SyntaxWarnings globally --- astroid/builder.py | 12 +++++++----- tests/test_builder.py | 3 +-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/astroid/builder.py b/astroid/builder.py index 0950c9d862..cff859124e 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -21,6 +21,7 @@ from astroid import bases, modutils, nodes, raw_building, rebuilder, util from astroid._ast import ParserModule, get_parser_module +from astroid.const import PY312_PLUS from astroid.exceptions import AstroidBuildingError, AstroidSyntaxError, InferenceError from astroid.manager import AstroidManager @@ -34,6 +35,9 @@ _STATEMENT_SELECTOR = "#@" MISPLACED_TYPE_ANNOTATION_ERROR = "misplaced type annotation" +if PY312_PLUS: + warnings.filterwarnings("ignore", "invalid escape sequence", SyntaxWarning) + def open_source_file(filename: str) -> tuple[TextIOWrapper, str, str]: # pylint: disable=consider-using-with @@ -174,11 +178,9 @@ def _data_build( ) -> tuple[nodes.Module, rebuilder.TreeRebuilder]: """Build tree node from data and add some informations.""" try: - with warnings.catch_warnings(): - warnings.simplefilter("ignore", SyntaxWarning) - node, parser_module = _parse_string( - data, type_comments=True, modname=modname - ) + 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}", diff --git a/tests/test_builder.py b/tests/test_builder.py index e496f50e42..84ac65e099 100644 --- a/tests/test_builder.py +++ b/tests/test_builder.py @@ -19,7 +19,7 @@ import pytest from astroid import Instance, builder, nodes, test_utils, util -from astroid.const import IS_PYPY, PY38, PY39_PLUS, PY312_PLUS, PYPY_7_3_11_PLUS +from astroid.const import IS_PYPY, PY38, PY39_PLUS, PYPY_7_3_11_PLUS from astroid.exceptions import ( AstroidBuildingError, AstroidSyntaxError, @@ -416,7 +416,6 @@ def test_data_build_invalid_x_escape(self) -> None: with self.assertRaises(AstroidSyntaxError): self.builder.string_build('"\\x1"') - @pytest.mark.skipif(PY312_PLUS, reason="Ignoring SyntaxWarnings triggered by 3.12+") 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: From 591c848080760636e110e4da4a7d0daed5a385b9 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Mon, 19 Feb 2024 14:24:50 +0100 Subject: [PATCH 4/4] Update ChangeLog Co-authored-by: Jacob Walls --- ChangeLog | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 2b1865a12d..aedde4bdd4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -27,7 +27,7 @@ Release date: TBA * Include modname in AST warnings. Useful for ``invalid escape sequence`` warnings with Python 3.12. -* Suppress ``SyntaxWarnings`` when parsing modules. +* Suppress ``SyntaxWarning`` for invalid escape sequences on Python 3.12 when parsing modules. Closes pylint-dev/pylint#9322