gh-137855: Lazy import inspect module in dataclasses - #144387
Conversation
|
|
||
| # If this is a wrapped function, unwrap it. | ||
| member = inspect.unwrap(member) | ||
| if not isinstance(member, type) and hasattr(member, '__wrapped__'): |
There was a problem hiding this comment.
This check was copied from the while loop in inspect.unwrap
|
Deferring the call to |
ca36c68 to
6bc6199
Compare
|
I believe we should add a NEWS entry, because it is user-facing change (at least in the performance terms). |
|
As a side effect that may be worth noting, deferring Before: PR: Every dataclass is getting its own Following on from this, the Deferring Footnotes
|
They could; that would be nice. |
Ideally with the new annotations, uses of |
|
I'm not sure we should mix changes for |
|
@danielhollas If you open a new PR with the lazy imports and global regex I'm happy to merge those. I'd prefer someone else to review the autodoc stuff. |
|
@hugovk worth noting as it was one of the driving forces behind this that you lose a chunk of the benefit in One way of encouraging people to document their classes I guess 🙂 . |
Thanks, I could do, but note that deferring re without deferring inspect is pointless since inspect imports re as well. (and it also wouldn't help |
I was wrong, I forgot that I made |
|
Alright, #148379 has been merged (thanks Hugo!) so this PR is now only about lazy importing |
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
|
Hi all, just a note that I will be away for two weeks starting 1st of May. |






inspectmodule is slow to import (see #117865) and is dragging down dataclasses with it.There are currently only two uses of
inspectin dataclasses, but they are a bit tricky to inline since they are on a direct code path when the@dataclassdecorator is executed.inspect.signatureis used to autogenerate class docstring (if one is not provided already)inspect.unwrapis used in a rather esoteric code path only for slotted classes, added in gh-90562: Support zero argument super with dataclasses when slots=True #124455)For 1. I have used a descriptor protocol to generate the
__doc__attribute on demand (this is my first time messing with descriptors, apologies if I overlooked something).For 2. can be deferred by calling the unwrap functions only when really necessary (and hopefully this path is not common)
Benchmarks
./python -Ximporttime -c "import dataclasses"Before
After
Overall seems to be a solid 20-30% improvement.