Skip to content

gh-158086: Fix unittest.main shadowed by its submodule under lazy imports - #158087

Open
lazerg wants to merge 2 commits into
python:mainfrom
lazerg:fix-issue-158086
Open

lazerg wants to merge 2 commits into
python:mainfrom
lazerg:fix-issue-158086

Conversation

@lazerg

@lazerg lazerg commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

With -X lazy_imports=all, from .main import TestProgram, main in unittest/__init__.py binds both names lazily. If TestProgram gets resolved first, loading the unittest.main submodule sets unittest.main to the module, and the pending lazy main is lost. unittest.main() then fails with TypeError: 'module' object is not callable.

main is now assigned from TestProgram (the same object unittest.main.main aliases), so it no longer depends on the lazy binding. test_unittest and test_pyrepl (whose only failure was the same unittest.main colorizing check) pass with lazy imports now, so I removed both from Lib/test/lazy_imports_all_exclude.txt.

The underlying problem is in the lazy import machinery, and any package whose __init__ does from .m import T, m hits it. It is closely related to gh-151208, so this only works around it in unittest until the core behavior is decided there.

Fixes #158086

@brittanyrey

Copy link
Copy Markdown
Contributor

I think I have the core fix for #151208, but it does look like my changes alone may not address the issue and your changes are still needed (at least against my version of the implementation).

@lazerg

lazerg commented Sep 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for checking. That makes sense to me. In gh-151208 the explicit import a.foo has to win over the attribute from __init__. Here it's the other way round: the main bound by from .main import main has to win over the submodule that gets set on the package when it loads. So I think both changes are needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-lazy-imports

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JIT: test_unittest fails with -X lazy_imports=all: unittest.main is a module

3 participants