GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-154675: Avoid importing inspect when creating a slotted dataclass by aisk · Pull Request #156841 · python/cpython

Conversation

@aisk

@aisk aisk commented Sep 2, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Using @dataclass(slots=True) always imports inspect, just for the single inspect.unwrap() call in _add_slots().

typing and annotationlib already inline the same __wrapped__ walk to avoid the import, so dataclasses can too. Perhaps in the future unwrap() could move into a small private module that inspect and all these copies import, so the implementations do not drift apart.

Importing a module that defines one slotted dataclass, best of 30 runs.

$ cat app.py
from dataclasses import dataclass

@dataclass(slots=True)
class Point:
    x: int = 0
    y: int = 0

$ ./python -c 'import time; t = time.perf_counter(); import app; print(time.perf_counter() - t)'
0.022856871997646522   # before
0.010011243997723795   # after

About 2.3x faster.

  • Issue: Improve import time of various stdlib modules #154675

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@aisk
pythongh-154675: Avoid importing inspect when creating a slotted data…
d86a8ef
…class
@aisk
aisk requested a review from ericvsmith as a code owner September 2, 2026 16:48
@bedevere-app bedevere-app Bot mentioned this pull request Sep 2, 2026
Improve import time of various stdlib modules #154675
Open
@bedevere-app bedevere-app Bot added the awaiting review label Sep 2, 2026
@aisk
Use ensure_lazy_imports in the regression test
2b98d01
@eendebakpt

eendebakpt commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

typing and annotationlib already inline the same __wrapped__ walk to avoid the import, so dataclasses can too. Perhaps in the future unwrap() could move into a small private module that inspect and all these copies import, so the implementations do not drift apart.

Adding another copy to be kept in sync is not very appealing, so I would be in favor of checking whether the private module idea can work. (note that typing and annotationlib use a set for the memo instead of a dict, which can potentially lead to issues when ids are reused).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@DavidCEllis

DavidCEllis commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

I agree that it's not really appealing to have yet another copy of the unwrap function to keep updated. It's unfortunate inspect is such a heavy import.

This is currently nearly always triggered due to dataclasses generating methods before the slotted version of the class is created and __repr__ being wrapped in recursive_repr.

As an alternative approach, we could move the creation of the slotted version of the class to before the methods are generated which should prevent basic slotted dataclasses from triggering the import. User methods that are wrapped would still trigger the import but this is probably sufficient in most cases.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

aisk added 2 commits September 15, 2026 00:07
@aisk
Create the slotted class before generating methods
0aa2387
@aisk
Update NEWS entry
436ffbc
@aisk

aisk commented Sep 14, 2026

Copy link
Copy Markdown
Member Author

I agree that not adding another copy is better.

I took @DavidCEllis's advice and changed the order of calling _add_slots, so now inspect is only imported when a slotted dataclass has a user defined method wrapped by a decorator. I think that's a rare case, so we don't need to optimize it.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@aisk
Merge branch 'main' into dataclasses-inline-unwrap
f0ca027
@eendebakpt

eendebakpt commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

This PR happens to solve a bug on main:

from dataclasses import dataclass

@dataclass(frozen=True, slots=True)
class F:
    x: int
    def m(self):
        return super().__repr__()
    
f = F(1)
f.y = 2                              # main: TypeError: super(type, obj): obj (instance of F) is not an instance ...; PR: FrozenInstanceError
print(f)
print(f.m()) # also affected

Could you add a regression test and update the news entry?

aisk reacted with eyes emoji
All reactions
  • 👀 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@aisk
Add a regression test
a0ccb79
@DavidCEllis DavidCEllis mentioned this pull request Sep 18, 2026
Add public functions to dataclasses to correctly identify methods and features #157314
Open
@ambv ambv mentioned this pull request Oct 20, 2026
gh-154675: Improve import performance of tkinter library with lazy imports #148409
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ericvsmith ericvsmith Awaiting requested review from ericvsmith ericvsmith is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting review

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@aisk @eendebakpt @DavidCEllis
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.