-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
PEP 800: Solid bases in the type system #4505
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
Conversation
Co-authored-by: Carl Meyer <carl@oddbird.net>
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.
Well written. Good motivation and clear spec.
I found a few minor typos, but other than that I don't have any suggestions.
peps/pep-0800.rst
Outdated
The ``__solid_base__`` attribute may be used for runtime introspection. | ||
|
||
It will be useful to validate whether the ``@solid_base`` decorator should be applied in a stub. While | ||
CPython does not document precisely which classes are solid bases, it is possible to replicate the behavior | ||
of the interpreter using runtime introspection | ||
(`example implementation <https://github.com/JelleZijlstra/pycroscope/blob/0d19236e4eda771175170a6b165b0e9f6a211d19/pycroscope/relations.py#L1469>`__). | ||
Stub validation tools, such as mypy's ``stubtest``, could use this logic to check whether the | ||
``@solid_base`` decorator is applied to the correct classes in stubs. |
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.
Practically speaking, I'm not sure the __solid_base__
attribute will tell you much other than that the class was explicitly decorated with the @solid_base
decorator. No builtin class will set the attribute, most external C extensions won't either, and any class that defines non-empty slots will be an implicit solid base even if it doesn't use the decorator. A class might also have a metaclass that prevents setting attributes, so not even all classes explicitly decorated with @solid_base
will necessarily have this attribute set (python/typing_extensions#634 (comment)).
If we want third-party tools to be able to introspect a class's solid base at runtime, a better solution would probably be to add an inspect.get_solid_base
introspection helper that does something similar to your logic in pycroscope. I don't think it's essential to add that as part of the PEP, though it might be useful.
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.
Sure, but that doesn't seem too different from using the __final__
attribute: it doesn't actually stop you from creating subclasses, and there is no bool.__final__
.
I think an inspect
helper like that could be useful but don't feel a strong need for it myself.
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.
My vague worry is that people will misunderstand this attribute and think that it's a reliable way of determining whether a class is a solid base or not. Unlike @final
, it feels pretty unlikely that we'll see ever much usage of this decorator in .py
files, so I'd expect the vast majority of solid bases not to have this attribute. (Though to be clear, I agree that it makes sense to make it possible to apply the decorator to classes in .py
files.)
Still, you're right that there are lots of @final
classes that also don't have the __final__
attribute. I suppose it's fine to make solid_base
introspectable whenever it is used in .py
files, and I suppose the __solid_base__
attribute no more misleading than the __final__
attribute set by @final
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Basic requirements (all PEP Types)
pep-NNNN.rst
), PR title (PEP 123: <Title of PEP>
) andPEP
headerAuthor
orSponsor
, and formally confirmed their approvalAuthor
,Status
(Draft
),Type
andCreated
headers filled out correctlyPEP-Delegate
,Topic
,Requires
andReplaces
headers completed if appropriate.github/CODEOWNERS
for the PEPStandards Track requirements
Python-Version
set to valid (pre-beta) future Python version, if relevantDiscussions-To
andPost-History
📚 Documentation preview 📚: https://pep-previews--4505.org.readthedocs.build/