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

Prevent a crash when a module's __path__ is missing #1833

Merged
merged 1 commit into from
Oct 15, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ Release date: TBA

Refs PyCQA/pylint#4039

* Prevent a crash when a module's ``__path__`` attribute is unexpectedly missing.

Refs PyCQA/pylint#7592


What's New in astroid 2.12.11?
Expand Down
2 changes: 2 additions & 0 deletions astroid/interpreter/_import/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def is_namespace(modname: str) -> bool:
found_spec = _find_spec_from_path(
working_modname, path=last_submodule_search_locations
)
except AttributeError:
return False
except ValueError:
if modname == "__main__":
return False
Expand Down
9 changes: 9 additions & 0 deletions tests/unittest_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import unittest
from collections.abc import Iterator
from contextlib import contextmanager
from unittest import mock

import pytest

Expand Down Expand Up @@ -144,6 +145,14 @@ def test_module_unexpectedly_missing_spec(self) -> None:
finally:
astroid_module.__spec__ = original_spec

@mock.patch(
"astroid.interpreter._import.util._find_spec_from_path",
side_effect=AttributeError,
)
def test_module_unexpectedly_missing_path(self, mocked) -> None:
"""https://github.com/PyCQA/pylint/issues/7592"""
self.assertFalse(util.is_namespace("astroid"))

def test_module_unexpectedly_spec_is_none(self) -> None:
astroid_module = sys.modules["astroid"]
original_spec = astroid_module.__spec__
Expand Down