From 108f675190b57bda9c67ae6c9dbfadf6b3fa2236 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Sat, 10 Sep 2022 18:26:49 -0400 Subject: [PATCH] Stop detecting modules compiled by `cffi` as namespace packages --- ChangeLog | 4 ++++ astroid/interpreter/_import/util.py | 8 +++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 142a9a7e0b..ab8ac586d8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -17,6 +17,10 @@ What's New in astroid 2.12.10? ============================== Release date: TBA +* Fixed a crash when introspecting modules compiled by `cffi`. + + Closes #1776 + Closes PyCQA/pylint#7399 What's New in astroid 2.12.9? diff --git a/astroid/interpreter/_import/util.py b/astroid/interpreter/_import/util.py index 8cdb8ea19d..c9466999ab 100644 --- a/astroid/interpreter/_import/util.py +++ b/astroid/interpreter/_import/util.py @@ -49,7 +49,13 @@ def is_namespace(modname: str) -> bool: # .pth files will be on sys.modules # __spec__ is set inconsistently on PyPy so we can't really on the heuristic here # See: https://foss.heptapod.net/pypy/pypy/-/issues/3736 - return sys.modules[modname].__spec__ is None and not IS_PYPY + # Check first fragment of modname, e.g. "astroid", not "astroid.interpreter" + # because of cffi's behavior + # See: https://github.com/PyCQA/astroid/issues/1776 + return ( + sys.modules[processed_components[0]].__spec__ is None + and not IS_PYPY + ) except KeyError: return False except AttributeError: