-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prohibit some illegal uses of Literal (#6034)
This pull request checks to make sure we report errors if the user tries using Literal types in invalid places. In particular, this PR... 1. Adds tests to make sure we cannot subclass Literals (mypy already directly supported this) 2. Checks to make sure we cannot use Literals inside `isinstance` and `issubclass` checks I also wanted to add a check preventing people from attempting to instantiate a Literal (e.g. disallow `Literal[3]()` or `Literal()`), but that might require a little more work and a few changes to `typing_extensions`. (We currently don't raise an error when people try doing things like `Final[int]()` or `Protocol[...]()` either).
- Loading branch information
1 parent
32263c2
commit dcfebd7
Showing
7 changed files
with
108 additions
and
21 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
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 |
---|---|---|
@@ -1,11 +1,15 @@ | ||
from typing import TypeVar | ||
from typing import TypeVar, Any | ||
|
||
_T = TypeVar('_T') | ||
|
||
class Protocol: pass | ||
class _SpecialForm: | ||
def __getitem__(self, typeargs: Any) -> Any: | ||
pass | ||
|
||
Protocol: _SpecialForm = ... | ||
def runtime(x: _T) -> _T: pass | ||
|
||
class Final: pass | ||
Final: _SpecialForm = ... | ||
def final(x: _T) -> _T: pass | ||
|
||
class Literal: pass | ||
Literal: _SpecialForm = ... |