Use __getattr__ for all package imports, improve import time#7947
Use __getattr__ for all package imports, improve import time#7947samuelcolvin merged 7 commits intomainfrom
__getattr__ for all package imports, improve import time#7947Conversation
Deploying with
|
| Latest commit: |
9733589
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://827630e5.pydantic-docs2.pages.dev |
| Branch Preview URL: | https://defer-init-imports.pydantic-docs2.pages.dev |
__getattr__ for all imports from __init__.py__getattr__ for all package imports, improve import time
__getattr__ for all package imports, improve import time__getattr__ for all package imports, improve import time
|
Awesome thank you, definitely a step in the right direction! Do you see a way to reduce the import cost of |
|
That last commit helped a little! |
|
@ofek I've got rid of import time import of If you see anything else, let me know. |
|
I think this is ready. Please review @sydney-runkle |
sydney-runkle
left a comment
There was a problem hiding this comment.
Nice! LGTM. Solid performance improvements 🏎️
One question - does this merit any update to the docs? I'm guessing not, that a change log entry is enough, but just wanted to check. I'm pretty sure 99% of users aren't going to be digging around for this kind of info.
|
I don't think anything in logs, I think it merits a prominent notice in the release notes and a tweet from me now saying we're doing it. AFAIK this shouldn't affect anyone and I made the change in few steps to see, but the only real answer is to release it and see. |
|
Out of curiosity, how will the practice of being conscious about import time be maintained during continued development? |
|
I've added unit tests that that those modules aren't imported in specific cases, e.g. The longer term solution is all down to @art049 - we should use Codspeed for continuous profile of stuff we care about like import time, as we do on pydantic-core. |


Change Summary
This means
pydantic/__init__.pyis now lazy about all imports using__getattr__.Import performance changes:
Before
After
Tuna output:
Related issue number
As promised in #7423 (comment).
Checklist
Selected Reviewer: @sydney-runkle