From f63a39368dd422d49d77c42a7dd6dad00ee1a986 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Fri, 4 Oct 2024 04:18:22 -0400 Subject: [PATCH] Fix python 3.13 compatibility re: collections.abc (#2598) --- ChangeLog | 6 +++++- astroid/brain/brain_collections.py | 20 +++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index f607d78f79..cc29f2a526 100644 --- a/ChangeLog +++ b/ChangeLog @@ -22,7 +22,11 @@ Release date: TBA * Control setting local nodes outside of the supposed local's constructor. - Closes pylint-dev/astroid/issues/1490 + 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 + )