Skip to content

[mypyc] Set dunder attrs when adding module to sys.modules#21275

Open
p-sawicki wants to merge 5 commits intopython:masterfrom
p-sawicki:set-dunder-attrs-when-adding-to-sys-modules
Open

[mypyc] Set dunder attrs when adding module to sys.modules#21275
p-sawicki wants to merge 5 commits intopython:masterfrom
p-sawicki:set-dunder-attrs-when-adding-to-sys-modules

Conversation

@p-sawicki
Copy link
Copy Markdown
Collaborator

@p-sawicki p-sawicki commented Apr 20, 2026

Recently there was a change to add native modules to sys.modules before they are executed to be able to detect circular imports. This introduced a regression when the module is a package that imports objects from other files within the package, eg. from pkg.file import something inside pkg/__init__.py. Such imports result in an exception ModuleNotFoundError: No module named 'pkg.file'; 'pkg' is not a package., for example when trying to upgrade mypy in black.

This error is raised because Python expects the parent module of file to have the __path__ attribute set when resolving the import but we don't set this attribute before adding the pkg module to sys.modules.

So use existing functions to set relevant dunder attributes (__path__ for packages and __file__, __spec__, and __package__ for all) before registering the module in sys.modules.

Don't skip compilation for __init__.py files in separate compilation mode to make this possible to test.

Use Py_CLEAR instead of Py_DECREF on the import object on failure in CPyImport_ImportNative as the import object might be freed when deleting it from sys.modules. This triggered an assertion when running tests with a debug build of cpython.

@p-sawicki p-sawicki marked this pull request as ready for review April 20, 2026 16:47
@ilevkivskyi ilevkivskyi requested a review from JukkaL April 20, 2026 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant