-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added check for data protocols used in an
issubclass
call. PEP 544 …
- Loading branch information
Showing
6 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
48 changes: 48 additions & 0 deletions
48
packages/pyright-internal/src/tests/samples/isinstance5.py
This file contains 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,48 @@ | ||
# This tests error reporting for the use of data protocols in an | ||
# issubclass call. | ||
|
||
from typing import Any, Protocol, runtime_checkable | ||
|
||
# > isinstance() can be used with both data and non-data protocols, while | ||
# > issubclass() can be used only with non-data protocols. | ||
|
||
|
||
@runtime_checkable | ||
class DataProtocol(Protocol): | ||
name: str | ||
|
||
def method1(self) -> int: | ||
... | ||
|
||
|
||
@runtime_checkable | ||
class DataProtocol2(DataProtocol, Protocol): | ||
def method2(self) -> int: | ||
... | ||
|
||
|
||
@runtime_checkable | ||
class NonDataProtocol(Protocol): | ||
def method1(self) -> int: | ||
... | ||
|
||
|
||
def func2(a: Any): | ||
if isinstance(a, DataProtocol): | ||
return | ||
|
||
if isinstance(a, NonDataProtocol): | ||
return | ||
|
||
# This should generate an error because data protocols | ||
# are not allowed with issubclass checks. | ||
if issubclass(a, DataProtocol): | ||
return | ||
|
||
# This should generate an error because data protocols | ||
# are not allowed with issubclass checks. | ||
if issubclass(a, DataProtocol2): | ||
return | ||
|
||
if issubclass(a, NonDataProtocol): | ||
return |
This file contains 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