Skip to content

Add @solid_base (PEP 800) #634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

JelleZijlstra
Copy link
Member

@JelleZijlstra JelleZijlstra commented Jul 22, 2025

@JelleZijlstra JelleZijlstra changed the title Add @solid_base (PEP 800) Add @solid_base (PEP 800) Jul 22, 2025
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

🚀

and determine when two types can overlap.

See PEP 800."""
cls.__solid_base__ = True
Copy link
Member

Choose a reason for hiding this comment

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

would it not be better to protect this with a try/except block, similar to what we do with typing.final? https://github.com/python/cpython/blob/9a21df7c0a494e2819775eabd522ebec994d96c0/Lib/typing.py#L2677-L2684

Most solid bases from the standard library will not permit you to set this attribute:

>>> int.__solid_base__ = True
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    int.__solid_base__ = True
    ^^^^^^^^^^^^^^^^^^
TypeError: cannot set '__solid_base__' attribute of immutable type 'int'

And you might plausibly write a metaclass to make class objects immutable even if a class is written in pure Python -- I don't think you should have to avoid adding this decorator (which exists primarily to help out static type checkers) just because the class is immutable at runtime

Copy link
Member Author

Choose a reason for hiding this comment

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

I did it that way in @final because we added the dunder attribute later and didn't want to break compatibility. Other more recently added decorators such as @deprecated and @dataclass_transform don't have the try-except.

Not necessarily opposed to adding the try-except though.

Copy link
Member

@AlexWaygood AlexWaygood Jul 22, 2025

Choose a reason for hiding this comment

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

yeah, it just seems sort-of odd to set it unconditionally, given that most pre-existing solid bases without __slots__ would not allow you to set the attribute. And it also seems more likely than usual that you might try to make a pure-Python class might be immutable if you're adding the solid_base decorator to it

JelleZijlstra and others added 2 commits July 22, 2025 07:01
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com>
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

I still don't see any harm in being more cautious about trying to set the attribute, and I think it has some benefits, so I'd still prefer protecting it in a try/except block. But I'm also happy with this being landed as-is if you disagree :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy