-
Notifications
You must be signed in to change notification settings - Fork 292
Add PEP 800 (disjoint bases) #2262
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
JelleZijlstra
wants to merge
1
commit into
python:main
Choose a base branch
from
JelleZijlstra:pep800
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| conformant = "Pass" | ||
| conformance_automated = "Pass" | ||
| errors_diff = """ | ||
| """ | ||
| output = """ | ||
| directives_disjoint_base.py:69: error: Class "LeftAndRight" has incompatible disjoint bases [misc] | ||
| directives_disjoint_base.py:73: error: Class "LeftChildAndRight" has incompatible disjoint bases [misc] | ||
| directives_disjoint_base.py:77: error: Class "LeftAndRightViaChild" has incompatible disjoint bases [misc] | ||
| directives_disjoint_base.py:81: error: Class "LeftRecord" has incompatible disjoint bases [misc] | ||
| directives_disjoint_base.py:105: error: Class "IncompatibleSlots" has incompatible disjoint bases [misc] | ||
| directives_disjoint_base.py:113: error: Value of type variable "_TC" of "disjoint_base" cannot be "Callable[[], None]" [type-var] | ||
| directives_disjoint_base.py:118: error: @disjoint_base cannot be used with TypedDict [misc] | ||
| directives_disjoint_base.py:123: error: @disjoint_base cannot be used with protocol class [misc] | ||
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| conformance_automated = "Fail" | ||
| conformant = "Unsupported" | ||
| notes = """ | ||
| Does not support PEP 800 disjoint-base semantics. | ||
| """ | ||
| errors_diff = """ | ||
| Line 69: Expected 1 errors | ||
| Line 73: Expected 1 errors | ||
| Line 77: Expected 1 errors | ||
| Line 105: Expected 1 errors | ||
| Line 118: Expected 1 errors | ||
| Line 123: Expected 1 errors | ||
| Line 60: Unexpected errors ['Named tuples do not support multiple inheritance [invalid-inheritance]'] | ||
| """ | ||
| output = """ | ||
| ERROR directives_disjoint_base.py:60:7-18: Named tuples do not support multiple inheritance [invalid-inheritance] | ||
| ERROR directives_disjoint_base.py:81:7-17: Named tuples do not support multiple inheritance [invalid-inheritance] | ||
| ERROR directives_disjoint_base.py:113:1-15: `() -> None` is not assignable to upper bound `type[object]` of type variable `_TC` [bad-specialization] | ||
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| conformance_automated = "Fail" | ||
| conformant = "Unsupported" | ||
| notes = """ | ||
| Does not support PEP 800 disjoint-base semantics. | ||
| """ | ||
| errors_diff = """ | ||
| Line 69: Expected 1 errors | ||
| Line 73: Expected 1 errors | ||
| Line 77: Expected 1 errors | ||
| Line 81: Expected 1 errors | ||
| Line 105: Expected 1 errors | ||
| Line 118: Expected 1 errors | ||
| Line 123: Expected 1 errors | ||
| """ | ||
| output = """ | ||
| directives_disjoint_base.py:113:2 - error: Argument of type "() -> None" cannot be assigned to parameter "cls" of type "_TC@disjoint_base" in function "disjoint_base" | ||
| Type "() -> None" is not assignable to type "type" | ||
| "FunctionType" is not assignable to "type" (reportArgumentType) | ||
| directives_disjoint_base.py:135:22 - error: Argument of type "<subclass of Left and Right>" cannot be assigned to parameter "arg" of type "Never" in function "assert_never" | ||
| Type "<subclass of Left and Right>" is not assignable to type "Never" (reportArgumentType) | ||
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| conformance_automated = "Fail" | ||
| conformant = "Partial" | ||
| notes = """ | ||
| Does not reject @disjoint_base on TypedDict or Protocol definitions. | ||
| """ | ||
| errors_diff = """ | ||
| Line 118: Expected 1 errors | ||
| Line 123: Expected 1 errors | ||
| """ | ||
| output = """ | ||
| directives_disjoint_base.py:69:7: error[instance-layout-conflict] Class will raise `TypeError` at runtime due to incompatible bases: Bases `Left` and `Right` cannot be combined in multiple inheritance | ||
| directives_disjoint_base.py:73:7: error[instance-layout-conflict] Class will raise `TypeError` at runtime due to incompatible bases: Bases `LeftChild` and `Right` cannot be combined in multiple inheritance | ||
| directives_disjoint_base.py:77:7: error[instance-layout-conflict] Class will raise `TypeError` at runtime due to incompatible bases: Bases `LeftAndPlain` and `Right` cannot be combined in multiple inheritance | ||
| directives_disjoint_base.py:81:7: error[instance-layout-conflict] Class will raise `TypeError` at runtime due to incompatible bases: Bases `Left` and `Record` cannot be combined in multiple inheritance | ||
| directives_disjoint_base.py:105:7: error[instance-layout-conflict] Class will raise `TypeError` at runtime due to incompatible bases: Bases `SlotBase1` and `SlotBase2` cannot be combined in multiple inheritance | ||
| directives_disjoint_base.py:113:1: error[invalid-argument-type] Argument to function `disjoint_base` is incorrect: Argument type `def func() -> None` does not satisfy upper bound `type` of type variable `_TC` | ||
| """ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| conformance_automated = "Fail" | ||
| conformant = "Unsupported" | ||
| notes = """ | ||
| Does not support PEP 800 disjoint-base semantics. | ||
| """ | ||
| errors_diff = """ | ||
| Line 69: Expected 1 errors | ||
| Line 73: Expected 1 errors | ||
| Line 77: Expected 1 errors | ||
| Line 81: Expected 1 errors | ||
| Line 105: Expected 1 errors | ||
| Line 118: Expected 1 errors | ||
| Line 123: Expected 1 errors | ||
| """ | ||
| output = """ | ||
| directives_disjoint_base.py:113: error: Value of type variable "_TC" of "disjoint_base" cannot be "Callable[[], None]" [type-var] | ||
| directives_disjoint_base.py:135: error: Argument 1 to "assert_never" has incompatible type "<subclass of "tests.directives_disjoint_base.Left" and "tests.directives_disjoint_base.Right">"; expected "Never" [arg-type] | ||
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| """ | ||
| Tests the typing.disjoint_base decorator introduced in PEP 800. | ||
| """ | ||
|
|
||
| # Specification: https://typing.readthedocs.io/en/latest/spec/directives.html#disjoint-base | ||
| # See also https://peps.python.org/pep-0800/ | ||
|
|
||
| from typing import NamedTuple, Protocol, TypedDict, assert_never | ||
| from typing_extensions import disjoint_base | ||
|
|
||
|
|
||
| # > It may only be used on nominal classes, including ``NamedTuple`` | ||
| # > definitions | ||
|
|
||
|
|
||
| @disjoint_base | ||
| class Left: | ||
| pass | ||
|
|
||
|
|
||
| @disjoint_base | ||
| class Right: | ||
| pass | ||
|
|
||
|
|
||
| @disjoint_base | ||
| class LeftChild(Left): | ||
| pass | ||
|
|
||
|
|
||
| @disjoint_base | ||
| class Record(NamedTuple): | ||
| value: int | ||
|
|
||
|
|
||
| class Plain: | ||
| pass | ||
|
|
||
|
|
||
| # > If the candidate set contains a single disjoint base, that is the | ||
| # > class's disjoint base. | ||
|
|
||
|
|
||
| class OtherLeftChild(Left): | ||
| pass | ||
|
|
||
|
|
||
| # > If there are multiple candidates, but one of them is a subclass of | ||
| # > all other candidates, that class is the disjoint base. | ||
|
|
||
|
|
||
| class LeftAndPlain(Left, Plain): | ||
| pass | ||
|
|
||
|
|
||
| class LeftChildAndLeft(LeftChild, Left): | ||
| pass | ||
|
|
||
|
|
||
| class PlainRecord(Plain, Record): | ||
| pass | ||
|
|
||
|
|
||
| # > Type checkers must check for a valid disjoint base when checking class definitions, | ||
| # > and emit a diagnostic if they encounter a class | ||
| # > definition that lacks a valid disjoint base. | ||
|
|
||
|
|
||
| class LeftAndRight(Left, Right): # E: incompatible disjoint bases | ||
| pass | ||
|
|
||
|
|
||
| class LeftChildAndRight(LeftChild, Right): # E: incompatible disjoint bases | ||
| pass | ||
|
|
||
|
|
||
| class LeftAndRightViaChild(LeftAndPlain, Right): # E: incompatible disjoint bases | ||
| pass | ||
|
|
||
|
|
||
| class LeftRecord(Left, Record): # E: incompatible disjoint bases | ||
| pass | ||
|
|
||
|
|
||
| # > A nominal class is a disjoint base if it [...] contains a non-empty | ||
| # > `__slots__` definition. | ||
|
|
||
|
|
||
| class SlotBase1: | ||
| __slots__ = ("x",) | ||
|
|
||
|
|
||
| class SlotBase2: | ||
| __slots__ = ("y",) | ||
|
|
||
|
|
||
| class EmptySlots: | ||
| __slots__ = () | ||
|
|
||
|
|
||
| class SlotAndEmptySlots(SlotBase1, EmptySlots): | ||
| pass | ||
|
|
||
|
|
||
| class IncompatibleSlots(SlotBase1, SlotBase2): # E: incompatible disjoint bases | ||
| pass | ||
|
|
||
|
|
||
| # > it is a type checker error to use the decorator on a function, | ||
| # > ``TypedDict`` definition, or ``Protocol`` definition. | ||
|
|
||
|
|
||
| @disjoint_base # E: disjoint_base cannot be applied to a function | ||
| def func() -> None: | ||
| pass | ||
|
|
||
|
|
||
| @disjoint_base # E: disjoint_base cannot be applied to a TypedDict | ||
| class Movie(TypedDict): | ||
| name: str | ||
|
|
||
|
|
||
| @disjoint_base # E: disjoint_base cannot be applied to a Protocol | ||
| class SupportsClose(Protocol): | ||
| def close(self) -> None: | ||
| ... | ||
|
|
||
|
|
||
| # > Type checkers may use disjoint bases to determine that two classes cannot | ||
| # > have a common subclass. | ||
|
|
||
|
|
||
| def narrow(obj: Left) -> None: | ||
| if isinstance(obj, Right): # E?: may be treated as unreachable | ||
| assert_never(obj) # E?: may not be narrowed to Never |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
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.
hm, this error is actually inaccurate; there's no runtime error if the class is just marked with
@disjoint_baseat runtime.Uh oh!
There was an error while loading. Please reload this page.
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.
I think the error is correct for the intended use of disjoint_base, though. I'm not sure it would be preferable to adjust the error message to account for misuse on classes that aren't actually disjoint bases.