Defer inspect import to reduce import time by ~25%#1547
Open
KRRT7 wants to merge 2 commits intopython-attrs:mainfrom
Open
Defer inspect import to reduce import time by ~25%#1547KRRT7 wants to merge 2 commits intopython-attrs:mainfrom
KRRT7 wants to merge 2 commits intopython-attrs:mainfrom
Conversation
hynek
requested changes
May 10, 2026
Member
hynek
left a comment
There was a problem hiding this comment.
There is merit to the idea but the summary is plausible-but-wrong, it both deleted and violated widely our checklist, and it contains breaking changes.
Please have a human look at it.
7587da9 to
c3df976
Compare
Move `import inspect` from module level to first use in `_compat.py` and `_make.py`. Lazy-load `converters` and `validators` submodules via `__getattr__` in both `attr` and `attrs` packages. This avoids importing `inspect` (which pulls in ast, re, dis, tokenize, etc.) at `import attr` time, reducing import time by ~25%.
c3df976 to
37b168e
Compare
KRRT7
commented
May 11, 2026
Author
KRRT7
left a comment
There was a problem hiding this comment.
Hiya! Sorry for the noise — at the time I was testing out something internally and made a few mistakes as you can see. Now we're in a better place and it won't happen again.
Here's what's been cleaned up:
- Checklist added back and addressed
- Changelog fragment added (
changelog.d/1547.change.md) - Codeflash config reverted (was unrelated)
- Fixed a
globals()bug in_make_getattr— the closure was caching intoattr's dict even when called fromattrs. Now usessys.modules[mod_name].__dict__ - Added
__dir__to both packages soconverters/validatorsappear indir()(they didn't onmaineither, but they should) - Added tests covering the above
CI is green. Let me know if there's anything else.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Defer the import of
inspectand the loading ofconverters/validatorssubmodules until first use, reducingimport attr/import attrstime by ~25%.Why:
inspectcosts ~12ms to import (it pulls inast,re,enum,dis,tokenize) but is only used at class-build time. The eager import is triggered becauseattr/__init__.pyeagerly importsvalidators, which uses@attrs()at module level, triggering class building →Converter()→_AnnotationExtractor()→import inspect.Changes:
_compat.py/_make.py: Moveimport inspectfrom module level into the methods that use itattr/__init__.py/attrs/__init__.py: Lazy-loadconvertersandvalidatorsvia the existing__getattr__mechanism_make_getattrto cache viasys.modules[mod_name].__dict__instead ofglobals()so caching works correctly when called fromattrs__dir__to both packages soconverters/validatorsappear indir()Benchmark (Azure Standard_D2s_v5, Python 3.13, 100 runs):
Pull Request Check List
mainbranch.from attr import converters,attr.validators, etc.).pyi).changelog.d.