Skip to content

[3.15] gh-155194: Fix not raising on non-module import - #155188

Open
DinoV wants to merge 3 commits into
python:3.15from
DinoV:lazy_fixes
Open

[3.15] gh-155194: Fix not raising on non-module import#155188
DinoV wants to merge 3 commits into
python:3.15from
DinoV:lazy_fixes

Conversation

@DinoV

@DinoV DinoV commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

As initially reported here: https://discuss.python.org/t/sys-lazy-modules-clarification/108385

lazy import math.pi has divergent behavior from import math.pi. Currently we allow this to work and just give you the math module. Instead we should raise ModuleNotFoundError because math.pi is not a module.

This would be a breaking change in the future so starting w/ fixing it in 3.15 because fixing it only in later versions would probably be bad.

@DinoV DinoV changed the title Fix not raising on non-module import gh-155194: Fix not raising on non-module import Aug 4, 2026
@DinoV DinoV added the skip news label Aug 4, 2026
@DinoV DinoV changed the title gh-155194: Fix not raising on non-module import [3.15] gh-155194: Fix not raising on non-module import Aug 4, 2026
@DinoV
DinoV requested a review from hugovk August 4, 2026 21:22
@DinoV
DinoV marked this pull request as ready for review August 4, 2026 22:49
@DinoV DinoV added 3.15 pre-release feature fixes, bugs and security fixes topic-lazy-imports labels Aug 4, 2026
Comment thread Lib/test/test_lazy_import/__init__.py Outdated
assert_python_ok("-c", code)

def test_non_package_lazily_imported(self):
"""Accessing a nonexistent lazy submodule via parent attr raises AttributeError."""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Accessing a nonexistent lazy submodule via parent attr raises AttributeError."""
"""Accessing a nonexistent lazy submodule via parent attr raises ModuleNotFoundError."""

Comment thread Python/import.c
);
Py_DECREF(name);
}
obj = _PyEval_ImportNameWithImport(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, does this change the error for lazy from ... import nonexistent_attr? We don't seem to have explicit tests for what that raises, just the chaining test?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, that'll be the same. In the from case we're already doing a full import (it's probably not super clear here because that's when lz_attr is not NULL and that's maybe not the best name)

@emmatyping

Copy link
Copy Markdown
Member

Is there a reason there is a 3.15 specific PR here rather than backporting the one targeting main once that's merged? #155189

If so, it might be good to explain that in the PR description.

@DinoV

DinoV commented Aug 5, 2026

Copy link
Copy Markdown
Contributor Author

Is there a reason there is a 3.15 specific PR here rather than backporting the one targeting main once that's merged? #155189

If so, it might be good to explain that in the PR description.

Mainly because if we don't fix it in 3.15 I don't think we should ever fix it - it would probably be a breaking change as people would come to depend upon it so I think we should fix it here first or not fix it at all.

@Yhg1s

Yhg1s commented Aug 6, 2026

Copy link
Copy Markdown
Member

Mainly because if we don't fix it in 3.15 I don't think we should ever fix it - it would probably be a breaking change as people would come to depend upon it so I think we should fix it here first or not fix it at all.

Let's get Hugo's opinion on getting this into the next RC then, and if he's okay with that, apply it to main first. (Doing things in the other direction makes future merges more awkward.) FWIW, I think this is such a clear corner case that I think it would be fine to change in 3.15.1 as well, but I'm not in charge :)

@Yhg1s

Yhg1s commented Aug 6, 2026

Copy link
Copy Markdown
Member

@hugovk ^

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

Labels

3.15 pre-release feature fixes, bugs and security fixes awaiting core review skip news topic-lazy-imports

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants