GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-158086: Fix unittest.main shadowed by its submodule under lazy imports by lazerg · Pull Request #158087 · python/cpython

Conversation

@lazerg

@lazerg lazerg commented Sep 24, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@lazerg
pythongh-158086: Fix unittest.main shadowed by its submodule under la…
48bec69
…zy imports
@bedevere-app bedevere-app Bot added the awaiting review label Sep 24, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Sep 24, 2026
JIT: test_unittest fails with -X lazy_imports=all: unittest.main is a module #158086
Open
@hugovk hugovk added topic-lazy-imports interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Sep 24, 2026
@lazerg
Remove test_pyrepl from the lazy imports exclude list
4e7daae
@brittanyrey

brittanyrey commented Sep 24, 2026

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).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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.

brittanyrey reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lazerg

lazerg commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

@cmaloney, please have a look when you have time

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

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

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

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

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@lazerg @brittanyrey @hugovk
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.