From 34c70f0d9ec99e0918e1c90066ced4a5f25f38cc Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 26 Sep 2024 04:37:39 +0000 Subject: [PATCH 1/3] change the type annotation error heuristic (#2583) (#2586) The previous one depended on the message from "typed_ast", which is not used anymore. Instead, we check if there is a "# type:" substring in the source line of the exception. This can yield some false positives, but probably rarely. (cherry picked from commit 62c5badc838419090ee319acfcef3b651ffb1e94) Co-authored-by: temyurchenko <44875844+temyurchenko@users.noreply.github.com> --- astroid/builder.py | 10 ++++++---- tests/test_builder.py | 6 ------ tests/test_nodes.py | 4 ++++ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/astroid/builder.py b/astroid/builder.py index 932b461fa5..f4be6972b8 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -12,6 +12,7 @@ import ast import os +import re import textwrap import types import warnings @@ -33,7 +34,6 @@ # The comment used to select a statement to be extracted # when calling extract_node. _STATEMENT_SELECTOR = "#@" -MISPLACED_TYPE_ANNOTATION_ERROR = "misplaced type annotation" if PY312_PLUS: warnings.filterwarnings("ignore", "invalid escape sequence", SyntaxWarning) @@ -479,9 +479,11 @@ def _parse_string( ) 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 - # type comment support. - if exc.args[0] != MISPLACED_TYPE_ANNOTATION_ERROR or not type_comments: + # to fail the entire parsing of the file, so we need to retry the + # parsing without type comment support. We use a heuristic for + # determining if the error is due to type annotations. + type_annot_related = re.search(r"#\s+type:", exc.text or "") + if not (type_annot_related and type_comments): raise parser_module = get_parser_module(type_comments=False) diff --git a/tests/test_builder.py b/tests/test_builder.py index f9dac6169e..b5335d5667 100644 --- a/tests/test_builder.py +++ b/tests/test_builder.py @@ -914,12 +914,6 @@ def test_module_build_dunder_file() -> None: assert module.path[0] == collections.__file__ -@pytest.mark.xfail( - reason=( - "The builtin ast module does not fail with a specific error " - "for syntax error caused by invalid type comments." - ), -) def test_parse_module_with_invalid_type_comments_does_not_crash(): node = builder.parse( """ diff --git a/tests/test_nodes.py b/tests/test_nodes.py index 64cae2f676..dced983625 100644 --- a/tests/test_nodes.py +++ b/tests/test_nodes.py @@ -1304,6 +1304,10 @@ def test_type_comments_invalid_expression() -> None: def test_type_comments_invalid_function_comments() -> None: module = builder.parse( """ + def func( + # type: () -> int # inside parentheses + ): + pass def func(): # type: something completely invalid pass From d1f934742b48ff2e9da199ecba43e4172325a37a Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Fri, 4 Oct 2024 06:51:29 -0400 Subject: [PATCH 2/3] Fix python 3.13 compatibility re: collections.abc (#2598) (#2599) (cherry picked from commit f63a39368dd422d49d77c42a7dd6dad00ee1a986) --- ChangeLog | 7 +++++++ astroid/brain/brain_collections.py | 20 +++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index de225b18c8..46bc04b1c3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -13,6 +13,13 @@ What's New in astroid 3.3.5? ============================ Release date: TBA +* Control setting local nodes outside of the supposed local's constructor. + + Closes #1490 + +* Fix Python 3.13 compatibility re: `collections.abc` + + Closes pylint-dev/pylint#10000 What's New in astroid 3.3.4? diff --git a/astroid/brain/brain_collections.py b/astroid/brain/brain_collections.py index 22017786ac..94944e67ad 100644 --- a/astroid/brain/brain_collections.py +++ b/astroid/brain/brain_collections.py @@ -4,13 +4,19 @@ from __future__ import annotations +from typing import TYPE_CHECKING + from astroid.brain.helpers import register_module_extender -from astroid.builder import extract_node, parse +from astroid.builder import AstroidBuilder, extract_node, parse +from astroid.const import PY313_PLUS from astroid.context import InferenceContext from astroid.exceptions import AttributeInferenceError from astroid.manager import AstroidManager from astroid.nodes.scoped_nodes import ClassDef +if TYPE_CHECKING: + from astroid import nodes + def _collections_transform(): return parse( @@ -26,6 +32,13 @@ def __getitem__(self, key): return default_factory ) +def _collections_abc_313_transform() -> nodes.Module: + """See https://github.com/python/cpython/pull/124735""" + return AstroidBuilder(AstroidManager()).string_build( + "from _collections_abc import *" + ) + + def _deque_mock(): base_deque_class = """ class deque(object): @@ -118,3 +131,8 @@ def register(manager: AstroidManager) -> None: manager.register_transform( ClassDef, easy_class_getitem_inference, _looks_like_subscriptable ) + + if PY313_PLUS: + register_module_extender( + manager, "collections.abc", _collections_abc_313_transform + ) From 8c74a5f6984753900f0222622ccbf3182de66a47 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Fri, 4 Oct 2024 08:01:39 -0400 Subject: [PATCH 3/3] Bump astroid to 3.3.5, update changelog (#2600) --- ChangeLog | 8 +++++++- astroid/__pkginfo__.py | 2 +- tbump.toml | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 46bc04b1c3..4d3de29097 100644 --- a/ChangeLog +++ b/ChangeLog @@ -9,10 +9,16 @@ Release date: TBA -What's New in astroid 3.3.5? +What's New in astroid 3.3.6? ============================ Release date: TBA + + +What's New in astroid 3.3.5? +============================ +Release date: 2024-10-04 + * Control setting local nodes outside of the supposed local's constructor. Closes #1490 diff --git a/astroid/__pkginfo__.py b/astroid/__pkginfo__.py index 1ade3f2033..51dd03aa60 100644 --- a/astroid/__pkginfo__.py +++ b/astroid/__pkginfo__.py @@ -2,5 +2,5 @@ # For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE # Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt -__version__ = "3.3.4" +__version__ = "3.3.5" version = __version__ diff --git a/tbump.toml b/tbump.toml index d0bf2b3041..11db820749 100644 --- a/tbump.toml +++ b/tbump.toml @@ -1,7 +1,7 @@ github_url = "https://github.com/pylint-dev/astroid" [version] -current = "3.3.4" +current = "3.3.5" regex = ''' ^(?P0|[1-9]\d*) \.