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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Unreleased

- Add the `@typing_extensions.solid_base` decorator, as specified
in PEP 800. Patch by Jelle Zijlstra.

# Release 4.14.1 (July 4, 2025)

- Fix usage of `typing_extensions.TypedDict` nested inside other types
Expand Down
11 changes: 11 additions & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,17 @@ Decorators
improved, and ``typing_extensions`` backports these performance
improvements.

.. decorator:: solid_base

See :pep:`800`. A class decorator that marks a class as a "solid base", meaning that
child classes of the decorated class cannot inherit from other solid bases that are not
parent classes of the decorated class.

This helps type checkers to detect unreachable code and to understand when two types
can overlap.

.. versionadded:: 4.15.0

Functions
~~~~~~~~~

Expand Down
13 changes: 13 additions & 0 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
reveal_type,
runtime,
runtime_checkable,
solid_base,
)

NoneType = type(None)
Expand Down Expand Up @@ -6669,6 +6670,18 @@ def cached(self): ...
self.assertIs(True, Methods.cached.__final__)


class SolidBaseTests(BaseTestCase):
def test_solid_base_unmodified(self):
class C: ...
self.assertIs(C, solid_base(C))

def test_dunder_solid_base(self):
@solid_base
class C: ...

self.assertIs(C.__solid_base__, True)


class RevealTypeTests(BaseTestCase):
def test_reveal_type(self):
obj = object()
Expand Down
28 changes: 28 additions & 0 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
'reveal_type',
'runtime',
'runtime_checkable',
'solid_base',
'Text',
'TypeAlias',
'TypeAliasType',
Expand Down Expand Up @@ -315,6 +316,33 @@ class Other(Leaf): # Error reported by type checker
return f


if hasattr(typing, "solid_base"): # 3.15
solid_base = typing.solid_base
else:
def solid_base(cls):
"""This decorator marks a class a solid base.

Child classes of a solid base cannot inherit from other solid bases that are
not parent classes of the solid base.

For example:

@solid_base
class Solid1: pass

@solid_base
class Solid2: pass

class Solid3(Solid1, Solid2): pass # Type checker error

Type checkers can use knowledge of solid bases to detect unreachable code
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

return cls


def IntVar(name):
return typing.TypeVar(name)

Expand Down
Loading