Skip to content

Commit

Permalink
Add compatibility with python 3.13.1 (#2647)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls authored Dec 8, 2024
1 parent cae4388 commit fe01bda
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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`


Expand Down
11 changes: 6 additions & 5 deletions astroid/brain/brain_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 *"
Expand Down Expand Up @@ -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
)
1 change: 1 addition & 0 deletions astroid/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit fe01bda

Please sign in to comment.