Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-148639: Implement PEP 800 (typing.disjoint_base)#148640
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
3f7202c27d989a0cd8736adb683490b44187099499ca31f24File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3358,6 +3358,36 @@ Functions and decorators | ||
| .. versionadded:: 3.12 | ||
| .. decorator:: disjoint_base | ||
| Decorator to mark a class as a disjoint base. | ||
| Type checkers do not allow child classes of a disjoint base ``C`` to | ||
| inherit from other disjoint bases that are not parent or child classes of ``C``. | ||
| For example:: | ||
| @disjoint_base | ||
| class Disjoint1: pass | ||
| @disjoint_base | ||
| class Disjoint2: pass | ||
| class Disjoint3(Disjoint1, Disjoint2): pass # Type checker error | ||
| Type checkers can use knowledge of disjoint bases to detect unreachable code | ||
| and determine when two types can overlap. | ||
| The corresponding runtime concept is a solid base (see :ref:`multiple-inheritance`). | ||
| Classes that are solid bases at runtime can be marked with ``@disjoint_base`` in stub files. | ||
| Users may also mark other classes as disjoint bases to indicate to type checkers that | ||
| multiple inheritance with other disjoint bases should not be allowed. | ||
JelleZijlstra marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Note that the concept of a solid base is a CPython implementation | ||
| detail, and the exact set of standard library classes that are | ||
| disjoint bases at runtime may change in future versions of Python. | ||
| .. versionadded:: next | ||
| .. decorator:: type_check_only | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -126,6 +126,7 @@ | ||
| 'cast', | ||
| 'clear_overloads', | ||
| 'dataclass_transform', | ||
| 'disjoint_base', | ||
| 'evaluate_forward_ref', | ||
| 'final', | ||
| 'get_args', | ||
| @@ -2794,6 +2795,29 @@ class Other(Leaf): # Error reported by type checker | ||
| return f | ||
| def disjoint_base(cls): | ||
| """This decorator marks a class as a disjoint base. | ||
| Child classes of a disjoint base cannot inherit from other disjoint bases that are | ||
| not parent or child classes of the disjoint base. | ||
| For example: | ||
| @disjoint_base | ||
| class Disjoint1: pass | ||
| @disjoint_base | ||
| class Disjoint2: pass | ||
| class Disjoint3(Disjoint1, Disjoint2): pass # Type checker error | ||
| Type checkers can use knowledge of disjoint bases to detect unreachable code | ||
| and determine when two types can overlap. | ||
| """ | ||
| cls.__disjoint_base__ = True | ||
| return cls | ||
JelleZijlstra marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| # Some unconstrained type variables. These were initially used by the container types. | ||
| # They were never meant for export and are now unused, but we keep them around to | ||
| # avoid breaking compatibility with users who import them. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Implement :pep:`800`, adding the :deco:`typing.disjoint_base` decorator. | ||
| Patch by Jelle Zijlstra. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.