-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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 "unsafe overlaps" for a runtime-checkable protocol wh…
- Loading branch information
Showing
7 changed files
with
137 additions
and
1 deletion.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# This sample tests the detection of a runtime checkable protocol | ||
# that unsafely overlaps a class within an isinstance or issubclass | ||
# call. | ||
|
||
# > Type checkers should reject an isinstance() or issubclass() call if there | ||
# > is an unsafe overlap between the type of the first argument and the protocol. | ||
|
||
|
||
from typing import Protocol, runtime_checkable | ||
|
||
|
||
@runtime_checkable | ||
class Proto3(Protocol): | ||
def method1(self, a: int) -> int: | ||
... | ||
|
||
|
||
class Concrete3A: | ||
def method1(self, a: str) -> None: | ||
pass | ||
|
||
|
||
@runtime_checkable | ||
class Proto2(Protocol): | ||
def other(self) -> None: | ||
... | ||
|
||
|
||
class Concrete3B: | ||
method1: int = 1 | ||
|
||
|
||
def func3(): | ||
if isinstance(Concrete3A(), Proto3): # Type error: unsafe overlap | ||
pass | ||
|
||
if isinstance(Concrete3B(), (Proto3, Proto2)): # Type error: unsafe overlap | ||
pass | ||
|
||
if issubclass(Concrete3A, (Proto3, Proto2)): # Type error: unsafe overlap | ||
pass |
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