Skip to content

Commit 68714df

Browse files
[Backport maintenance/3.3.x] Another attempt at fixing the collections.abc issue for Python 3.13 (#2665) (#2666)
* Fix issue with importing of frozen submodules (cherry picked from commit f94e855) * Test on Python 3.13 final * Bump CI jobs to python 3.13 --------- Co-authored-by: Daniël van Noord <[email protected]>
1 parent 7cfbad1 commit 68714df

File tree

4 files changed

+46
-32
lines changed

4 files changed

+46
-32
lines changed

.github/workflows/ci.yaml

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ on:
1010
env:
1111
CACHE_VERSION: 3
1212
KEY_PREFIX: venv
13-
DEFAULT_PYTHON: "3.12"
13+
DEFAULT_PYTHON: "3.13"
1414
PRE_COMMIT_CACHE: ~/.cache/pre-commit
1515

1616
concurrency:
@@ -81,7 +81,7 @@ jobs:
8181
strategy:
8282
fail-fast: false
8383
matrix:
84-
python-version: [3.9, "3.10", "3.11", "3.12", "3.13-dev"]
84+
python-version: [3.9, "3.10", "3.11", "3.12", "3.13"]
8585
outputs:
8686
python-key: ${{ steps.generate-python-key.outputs.key }}
8787
steps:
@@ -138,7 +138,7 @@ jobs:
138138
strategy:
139139
fail-fast: false
140140
matrix:
141-
python-version: [3.9, "3.10", "3.11", "3.12", "3.13-dev"]
141+
python-version: [3.9, "3.10", "3.11", "3.12", "3.13"]
142142
steps:
143143
- name: Set temp directory
144144
run: echo "TEMP=$env:USERPROFILE\AppData\Local\Temp" >> $env:GITHUB_ENV
@@ -242,11 +242,11 @@ jobs:
242242
steps:
243243
- name: Check out code from GitHub
244244
uses: actions/[email protected]
245-
- name: Set up Python 3.12
245+
- name: Set up Python 3.13
246246
id: python
247247
uses: actions/[email protected]
248248
with:
249-
python-version: "3.12"
249+
python-version: "3.13"
250250
check-latest: true
251251
- name: Install dependencies
252252
run: pip install -U -r requirements_minimal.txt

.github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
- published
77

88
env:
9-
DEFAULT_PYTHON: "3.12"
9+
DEFAULT_PYTHON: "3.13"
1010

1111
permissions:
1212
contents: read

ChangeLog

+6
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,18 @@ What's New in astroid 3.3.8?
1313
============================
1414
Release date: TBA
1515

16+
* Fix inability to import `collections.abc` in python 3.13.1. The reported fixes in astroid 3.3.6
17+
and 3.3.7 did not actually fix this issue.
18+
19+
Closes pylint-dev/pylint#10112
1620

1721

1822
What's New in astroid 3.3.7?
1923
============================
2024
Release date: 2024-12-20
2125

26+
This release was yanked.
27+
2228
* Fix inability to import `collections.abc` in python 3.13.1. The reported fix in astroid 3.3.6
2329
did not actually fix this issue.
2430

astroid/interpreter/_import/spec.py

+34-26
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,45 @@ def find_module(
142142
type=ModuleType.C_BUILTIN,
143143
)
144144

145+
if submodule_path is not None:
146+
search_paths = list(submodule_path)
147+
else:
148+
search_paths = sys.path
149+
150+
suffixes = (".py", ".pyi", importlib.machinery.BYTECODE_SUFFIXES[0])
151+
for entry in search_paths:
152+
package_directory = os.path.join(entry, modname)
153+
for suffix in suffixes:
154+
package_file_name = "__init__" + suffix
155+
file_path = os.path.join(package_directory, package_file_name)
156+
if os.path.isfile(file_path):
157+
return ModuleSpec(
158+
name=modname,
159+
location=package_directory,
160+
type=ModuleType.PKG_DIRECTORY,
161+
)
162+
for suffix, type_ in ImportlibFinder._SUFFIXES:
163+
file_name = modname + suffix
164+
file_path = os.path.join(entry, file_name)
165+
if os.path.isfile(file_path):
166+
return ModuleSpec(name=modname, location=file_path, type=type_)
167+
145168
# sys.stdlib_module_names was added in Python 3.10
146169
if PY310_PLUS:
147-
# If the module is a stdlib module, check whether this is a frozen module. Note that
148-
# `find_spec` actually imports the module, so we want to make sure we only run this code
149-
# for stuff that can be expected to be frozen. For now this is only stdlib.
170+
# If the module name matches a stdlib module name, check whether this is a frozen
171+
# module. Note that `find_spec` actually imports parent modules, so we want to make
172+
# sure we only run this code for stuff that can be expected to be frozen. For now
173+
# this is only stdlib.
150174
if modname in sys.stdlib_module_names or (
151175
processed and processed[0] in sys.stdlib_module_names
152176
):
153-
spec = importlib.util.find_spec(".".join((*processed, modname)))
177+
try:
178+
with warnings.catch_warnings():
179+
warnings.filterwarnings("ignore", category=Warning)
180+
spec = importlib.util.find_spec(".".join((*processed, modname)))
181+
except ValueError:
182+
spec = None
183+
154184
if (
155185
spec
156186
and spec.loader # type: ignore[comparison-overlap] # noqa: E501
@@ -186,28 +216,6 @@ def find_module(
186216
except ValueError:
187217
pass
188218

189-
if submodule_path is not None:
190-
search_paths = list(submodule_path)
191-
else:
192-
search_paths = sys.path
193-
194-
suffixes = (".py", ".pyi", importlib.machinery.BYTECODE_SUFFIXES[0])
195-
for entry in search_paths:
196-
package_directory = os.path.join(entry, modname)
197-
for suffix in suffixes:
198-
package_file_name = "__init__" + suffix
199-
file_path = os.path.join(package_directory, package_file_name)
200-
if os.path.isfile(file_path):
201-
return ModuleSpec(
202-
name=modname,
203-
location=package_directory,
204-
type=ModuleType.PKG_DIRECTORY,
205-
)
206-
for suffix, type_ in ImportlibFinder._SUFFIXES:
207-
file_name = modname + suffix
208-
file_path = os.path.join(entry, file_name)
209-
if os.path.isfile(file_path):
210-
return ModuleSpec(name=modname, location=file_path, type=type_)
211219
return None
212220

213221
def contribute_to_path(

0 commit comments

Comments
 (0)