Skip to content
Draft
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ build-stamp
.pytest_cache/
.mypy_cache/
.benchmarks/
venv/
20 changes: 20 additions & 0 deletions pylint/checkers/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,26 @@

dotted_modname = get_import_name(importnode, modname)
self.add_message("import-error", args=repr(dotted_modname), node=importnode)
except AttributeError as e:
# Handle mypyc compiled modules that may not have __dict__

Check notice on line 1037 in pylint/checkers/imports.py

View workflow job for this annotation

GitHub Actions / pylint

C0401

Wrong spelling of a word 'mypyc' in a comment:
if "__dict__" in str(e):
if not self.linter.is_message_enabled("import-error"):
return None
if _ignore_import_failure(importnode, modname, self._ignored_modules):
return None
if (
not self.linter.config.analyse_fallback_blocks
and is_from_fallback_block(importnode)
):
return None

dotted_modname = get_import_name(importnode, modname)
self.add_message(
"import-error", args=repr(dotted_modname), node=importnode
)
else:
# Re-raise other AttributeErrors
raise astroid.AstroidError from e
except Exception as e: # pragma: no cover
raise astroid.AstroidError from e
return None
Expand Down
12 changes: 12 additions & 0 deletions tests/functional/i/import_mypy_extension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""Test for mypy extension package import crash regression.

This reproduces the crash described in issue #10223 where importing
mypy modules with --extension-pkg-allow-list=mypy caused an AttributeError
crash due to mypyc-compiled objects not having __dict__ attributes.

The fix ensures this generates an import-error message instead of crashing.
"""
# pylint: disable=unused-import

# This should not crash when using --extension-pkg-allow-list=mypy
import mypy.build # [import-error]
5 changes: 5 additions & 0 deletions tests/functional/i/import_mypy_extension.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[Messages Control]
disable=C,R,W

[TYPECHECK]
extension-pkg-allow-list=mypy
1 change: 1 addition & 0 deletions tests/functional/i/import_mypy_extension.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import-error:12:0:12:17::Unable to import 'mypy.build':UNDEFINED
Loading