gh-117865: Defer several imports in inspect.py by AlexWaygood · Pull Request #119526 · python/cpython
(But this is possibly a redundant optimisation if it turns out that inspect needs to depend on ast more heavily due to PEP 649 anyway? Not sure)
I'll keep this on the back burner until it's decided where the new PEP-649 functions will go, and which modules they'll depend on
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(This showed up in my list of review requests, so I want to clear it out even if it's a draft.)
I think we can apply the changes that don't affect ast. My current implementation of the PEP 649 functionality relies very heavily on ast, so it's likely we won't be able to defer that import.
I think we can apply the changes that don't affect
ast.
Right -- but ast itself imports a bunch of stuff, so I'm not sure how much deferring the other imports helps if we can't defer the ast import. It might just make the code uglier for no apparent gain. I'll play around a bit when I get a chance; maybe now we've deferred the re import in ast things might have changed...
I think we can apply the changes that don't affect ast. My current implementation of the PEP 649 functionality relies very heavily on ast, so it's likely we won't be able to defer that import.
I ended up looking at this on Python 3.15a5 main and indeed the ast is used heavily in annotationlib, which is itself imported from inspect in a way that cannot be made lazy.
We could still lazy import tokenize and re, especially once the lazy import PR lands.
@AlexWaygood PEP810 has just landed. Are you planning to work on this PR in the near future? Per my previous comment, it could be a simple two-line change, just adding a lazy keyword in front of the re and tokenize modules. (ast module cannot be made lazy anymore given PEP 649).
@AlexWaygood PEP810 has just landed. Are you planning to work on this PR in the near future? Per my previous comment, it could be a simple two-line change, just adding a lazy keyword in front of the
reandtokenizemodules. (astmodule cannot be made lazy anymore given PEP 649).
I'm a bit snowed under with work on ty right now -- feel free to pick this up if you'd like to!
I'm a bit snowed under with work on ty right now -- feel free to pick this up if you'd like to!
I'm happy to! I'll try to cherry-pick your commit to preserve your authorship.
(watching the ty work from the sidelines, really excited about how it's progressing 👏 )
#144756 is merged, let's close this. Thanks all!