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-103740: Improve support of __slots__ for variable-length classes by serhiy-storchaka · Pull Request #141636 · python/cpython

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Nov 16, 2025 •
edited by github-actions Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Member
  • Allow defining the dict and weakref slots for any class.
  • Allow defining any slots for a class derived from the type class or other "variable-length" built-in type with the Py_TPFLAGS_ITEMS_AT_END flag.
  • Issue: Allow __slots__ on classes with Py_TPFLAGS_ITEMS_AT_END #103740

📚 Documentation preview 📚: https://cpython-previews--141636.org.readthedocs.build/

Sorry, something went wrong.

Uh oh!

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

All reactions
@serhiy-storchaka
serhiy-storchaka requested review from AA-Turner, markshannon and willingc as code owners November 16, 2025 19:24
@bedevere-app bedevere-app Bot added the awaiting core review label Nov 16, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Nov 16, 2025
Allow __slots__ on classes with Py_TPFLAGS_ITEMS_AT_END #103740
Open
@serhiy-storchaka
serhiy-storchaka requested a review from encukou November 16, 2025 19:25
@serhiy-storchaka
pythongh-103740: Improve support of __slots__ for variable-length cla…
c17b254
…sses

* Allow defining the __dict__ and __weakref__ __slots__ for any class.
* Allow defining any __slots__ for a class derived from the type class or
  other "variable-length" built-in type with the Py_TPFLAGS_ITEMS_AT_END flag.
@serhiy-storchaka
serhiy-storchaka force-pushed the special-slots-with-items2 branch from 8c3d950 to c17b254 Compare November 16, 2025 20:35
picnixz
picnixz reviewed Nov 16, 2025
View reviewed changes
Comment thread Doc/whatsnew/3.15.rst Resolved

Uh oh!

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

@serhiy-storchaka @picnixz
Update Doc/whatsnew/3.15.rst
7223cd5
Co-authored-by: Bénédikt Tran <[email protected]>
@picnixz

picnixz commented Nov 17, 2025

Copy link
Copy Markdown
Member

Well.. that's weird. The docs still fail?

All reactions

Sorry, something went wrong.

Uh oh!

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

picnixz
picnixz reviewed Nov 17, 2025
View reviewed changes
Comment thread Misc/NEWS.d/next/Core_and_Builtins/2025-11-16-21-14-48.gh-issue-103740.rXIj5h.rst Resolved

Uh oh!

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

@serhiy-storchaka @picnixz
Update Misc/NEWS.d/next/Core_and_Builtins/2025-11-16-21-14-48.gh-issu…
723dcaa
…e-103740.rXIj5h.rst

Co-authored-by: Bénédikt Tran <[email protected]>
@serhiy-storchaka

serhiy-storchaka commented Nov 17, 2025

Copy link
Copy Markdown
Member Author

Thank you. I noticed this error yesterday (this is is the second time I make such error in a month), but it was already late night.

All reactions

Sorry, something went wrong.

Uh oh!

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

@picnixz

picnixz commented Nov 17, 2025

Copy link
Copy Markdown
Member

No worry! don't hesitate to ping in the future if you encounter other Sphinx errors!

All reactions

Sorry, something went wrong.

Uh oh!

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

encukou
encukou reviewed Nov 17, 2025
View reviewed changes

@encukou encukou left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!
I need a bit more time to review the code; sending docs suggestions in the meantime.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Doc/reference/datamodel.rst Resolved

Uh oh!

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

Comment thread Doc/reference/datamodel.rst Resolved

Uh oh!

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

@serhiy-storchaka @encukou
Apply suggestions from code review
5846379
Co-authored-by: Petr Viktorin <[email protected]>
serhiy-storchaka
serhiy-storchaka commented Nov 17, 2025
View reviewed changes
Comment thread Doc/reference/datamodel.rst
Comment on lines +2637 to +2638
For example, *__slots__* other than *__dict__* and *__weakref__* cannot
be defined on subclasses of :class:`int`, :class:`bytes`, or :class:`tuple`.

@serhiy-storchaka serhiy-storchaka Nov 17, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use "they"?

Suggested change
For example, *__slots__* other than *__dict__* and *__weakref__* cannot
be defined on subclasses of :class:`int`, :class:`bytes`, or :class:`tuple`.
For example, they cannot be defined on subclasses of
:class:`int`, :class:`bytes`, or :class:`tuple`.

Sorry, something went wrong.

Uh oh!

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

encukou reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@encukou

encukou commented Nov 18, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

The only Py_TPFLAGS_ITEMS_AT_END type in stdlib is type itself.
So, this PR allows slotted types. Adding data descriptors to types leads to some rather hard-to-explain behaviour, for example:

>>> class Meta(type):
...     __slots__ = ['foo']  # now possible
...     
>>> class X(metaclass=Meta):
...     def foo(self): return 'f'
...     
>>> X().foo
<bound method X.foo of <__main__.X object at 0x7f7c579c2900>>
>>> X.foo
Traceback (most recent call last):
  File "<python-input-3>", line 1, in <module>
    X.foo
AttributeError: 'Meta' object has no attribute 'foo'
>>> X.foo = slice
>>> X.foo(X())
slice(None, <__main__.X object at 0x7f7c5788c050>, None)
>>> X().foo()
'f'

Do we want to make this easier?

All reactions

Sorry, something went wrong.

Uh oh!

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

@serhiy-storchaka

serhiy-storchaka commented Nov 18, 2025

Copy link
Copy Markdown
Member Author

I did not have any use case for this feature, but this looks interesting. We already have data descriptors (__module__, __qualname__, etc). And we can get the same with properties:

class Meta(type):
    @property
    def foo(self):
        return slice

class X(metaclass=Meta):
    def foo(self): return 'f'
 
print(X.foo(X()))
print(X().foo())
All reactions

Sorry, something went wrong.

Uh oh!

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

@encukou

encukou commented Nov 19, 2025

Copy link
Copy Markdown
Member

Yes, descriptors are already weird. But given stdlib's non-usage of Py_TPFLAGS_ITEMS_AT_END, this feature looks to be specifically about allowing descriptors for type, so I'm not sure we want it.

What about explicitly disabling arbitrary __slots__ on type subclasses, given how many optimizations assume that the __dict__ is authoritative?


The __dict__ & __weakref__ change looks uncontroversial -- and easier to explain, too.

All reactions

Sorry, something went wrong.

Uh oh!

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

@serhiy-storchaka

serhiy-storchaka commented Nov 19, 2025

Copy link
Copy Markdown
Member Author

Was not it the same with PEP 697? It allowed extending type in C. This PR allows to extend it in Python.

Anyway, I am only interesting in extending tuples. Adding support for special slots would be a side effect. Supporting __slots__ in the type subclasses is not necessary, it would only look inconsistent to not support it after PEP 697.

All reactions

Sorry, something went wrong.

Uh oh!

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

@serhiy-storchaka

serhiy-storchaka commented Nov 19, 2025

Copy link
Copy Markdown
Member Author

See #141755.

encukou reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@encukou

encukou commented Nov 19, 2025

Copy link
Copy Markdown
Member

PEP 697's “extending” was about adding a C-level chunk of memory, not about Python attributes. You can already extend type in Python. It has a __dict__.

Having a __dict__ also means the memory savings from __slots__ will be tiny, unless you have many slots. (Not to mention that type itself is huge.)

All reactions

Sorry, something went wrong.

Uh oh!

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

@serhiy-storchaka

serhiy-storchaka commented Nov 19, 2025

Copy link
Copy Markdown
Member Author

The other half of this PR (very simple) has been moved to #141764.

All reactions

Sorry, something went wrong.

Uh oh!

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

@serhiy-storchaka serhiy-storchaka closed this Nov 19, 2025
@serhiy-storchaka

serhiy-storchaka commented Nov 19, 2025

Copy link
Copy Markdown
Member Author

Lets discuss this not in PRs, but in the issue #103740.

All reactions

Sorry, something went wrong.

Uh oh!

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

@serhiy-storchaka
serhiy-storchaka deleted the special-slots-with-items2 branch July 1, 2026 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@encukou encukou encukou left review comments
@picnixz picnixz picnixz left review comments
@markshannon markshannon Awaiting requested review from markshannon markshannon is a code owner
@AA-Turner AA-Turner Awaiting requested review from AA-Turner AA-Turner is a code owner
@willingc willingc Awaiting requested review from willingc willingc is a code owner

Uh oh!

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

Assignees

No one assigned

Labels

awaiting core 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

@serhiy-storchaka @picnixz @encukou
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.