From fe01bda8d9b3e118b1713f726dea6efffa0c6b07 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Sun, 8 Dec 2024 00:56:49 -0500 Subject: [PATCH] Add compatibility with python 3.13.1 (#2647) Refs https://github.com/python/cpython/pull/125415 --- ChangeLog | 4 ++++ astroid/brain/brain_collections.py | 11 ++++++----- astroid/const.py | 1 + 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8dba33223f..724d6b45c2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -31,6 +31,10 @@ What's New in astroid 3.3.6? ============================ Release date: TBA +* Fix inability to import `collections.abc` in python 3.13.1. + + Closes pylint-dev/pylint#10112 + * Fix precedence of `path` arg in `modpath_from_file_with_callback` to be higher than `sys.path` diff --git a/astroid/brain/brain_collections.py b/astroid/brain/brain_collections.py index 94944e67ad..462c85add2 100644 --- a/astroid/brain/brain_collections.py +++ b/astroid/brain/brain_collections.py @@ -8,7 +8,7 @@ from astroid.brain.helpers import register_module_extender from astroid.builder import AstroidBuilder, extract_node, parse -from astroid.const import PY313_PLUS +from astroid.const import PY313_0, PY313_PLUS from astroid.context import InferenceContext from astroid.exceptions import AttributeInferenceError from astroid.manager import AstroidManager @@ -20,7 +20,8 @@ def _collections_transform(): return parse( - """ + (" import _collections_abc as abc" if PY313_PLUS and not PY313_0 else "") + + """ class defaultdict(dict): default_factory = None def __missing__(self, key): pass @@ -32,7 +33,7 @@ def __getitem__(self, key): return default_factory ) -def _collections_abc_313_transform() -> nodes.Module: +def _collections_abc_313_0_transform() -> nodes.Module: """See https://github.com/python/cpython/pull/124735""" return AstroidBuilder(AstroidManager()).string_build( "from _collections_abc import *" @@ -132,7 +133,7 @@ def register(manager: AstroidManager) -> None: ClassDef, easy_class_getitem_inference, _looks_like_subscriptable ) - if PY313_PLUS: + if PY313_0: register_module_extender( - manager, "collections.abc", _collections_abc_313_transform + manager, "collections.abc", _collections_abc_313_0_transform ) diff --git a/astroid/const.py b/astroid/const.py index c010818063..a10c0f4a2b 100644 --- a/astroid/const.py +++ b/astroid/const.py @@ -9,6 +9,7 @@ PY311_PLUS = sys.version_info >= (3, 11) PY312_PLUS = sys.version_info >= (3, 12) PY313_PLUS = sys.version_info >= (3, 13) +PY313_0 = sys.version_info[:3] == (3, 13, 0) WIN32 = sys.platform == "win32"