Skip to content

Commit f94e855

Browse files
authored
Another attempt at fixing the collections.abc issue for Python 3.13 (pylint-dev#2665)
* Fix issue with importing of frozen submodules
1 parent f79f2b4 commit f94e855

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

ChangeLog

+6
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,19 @@ What's New in astroid 3.3.8?
3333
============================
3434
Release date: TBA
3535

36+
* Fix inability to import `collections.abc` in python 3.13.1. The reported fixes in astroid 3.3.6
37+
and 3.3.7 did not actually fix this issue.
38+
39+
Closes pylint-dev/pylint#10112
3640

3741

3842

3943
What's New in astroid 3.3.7?
4044
============================
4145
Release date: 2024-12-20
4246

47+
This release was yanked.
48+
4349
* Fix inability to import `collections.abc` in python 3.13.1. The reported fix in astroid 3.3.6
4450
did not actually fix this issue.
4551

astroid/interpreter/_import/spec.py

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

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

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

214222
def contribute_to_path(

0 commit comments

Comments
 (0)