Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix collections.abc import error #2656

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions astroid/interpreter/_import/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import types
import warnings
import zipimport
from collections.abc import Iterable, Iterator, Sequence

Check failure on line 18 in astroid/interpreter/_import/spec.py

View workflow job for this annotation

GitHub Actions / Checks

E0401

Unable to import 'collections.abc'
from functools import lru_cache
from pathlib import Path
from typing import Literal, NamedTuple, Protocol
Expand Down Expand Up @@ -126,6 +126,27 @@
+ [(s, ModuleType.PY_COMPILED) for s in importlib.machinery.BYTECODE_SUFFIXES]
)

@staticmethod
def find_frozen_module(module: str) -> ModuleSpec | None:
try:
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=UserWarning)
spec = importlib.util.find_spec(module)
if (
spec
and spec.loader # type: ignore[comparison-overlap] # noqa: E501
is importlib.machinery.FrozenImporter
):
# No need for BuiltinImporter; builtins handled above
return ModuleSpec(
name=module,
location=getattr(spec.loader_state, "filename", None),
type=ModuleType.PY_FROZEN,
)
except ValueError:
pass
return None

@staticmethod
@lru_cache(maxsize=1024)
def find_module(
Expand All @@ -135,6 +156,11 @@
submodule_path: tuple[str, ...] | None,
) -> ModuleSpec | None:
if submodule_path is not None:
frozen_module_spec = ImportlibFinder.find_frozen_module(
".".join(part for part in module_parts)
)
if frozen_module_spec:
return frozen_module_spec
search_paths = list(submodule_path)
elif modname in sys.builtin_module_names:
return ModuleSpec(
Expand All @@ -143,23 +169,9 @@
type=ModuleType.C_BUILTIN,
)
else:
try:
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=UserWarning)
spec = importlib.util.find_spec(modname)
if (
spec
and spec.loader # type: ignore[comparison-overlap] # noqa: E501
is importlib.machinery.FrozenImporter
):
# No need for BuiltinImporter; builtins handled above
return ModuleSpec(
name=modname,
location=getattr(spec.loader_state, "filename", None),
type=ModuleType.PY_FROZEN,
)
except ValueError:
pass
frozen_module_spec = ImportlibFinder.find_frozen_module(modname)
if frozen_module_spec:
return frozen_module_spec
search_paths = sys.path

suffixes = (".py", ".pyi", importlib.machinery.BYTECODE_SUFFIXES[0])
Expand Down
Loading