forked from microsoft/pyright
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved
reportUnnecessaryCast
so it works with types other than cl… (
microsoft#5336) Improved `reportUnnecessaryCast` so it works with types other than class instances. This addresses microsoft#5333.
- Loading branch information
Showing
3 changed files
with
49 additions
and
8 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
51 changes: 46 additions & 5 deletions
51
packages/pyright-internal/src/tests/samples/unnecessaryCast1.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 |
---|---|---|
@@ -1,13 +1,54 @@ | ||
# This sample tests the type checker's reportUnnecessaryCast feature. | ||
|
||
from typing import cast, Union | ||
from typing import Never, NoReturn, TypeVar, cast | ||
|
||
|
||
def foo(a: int): | ||
def func1(a: int): | ||
# This should generate an error if | ||
# reportUnnecessaryCast is enabled. | ||
b = cast(int, a) | ||
v1 = cast(int, a) | ||
|
||
|
||
c: Union[int, str] = "hello" | ||
d = cast(int, c) | ||
def func2(a: int | str): | ||
v1 = cast(int, a) | ||
|
||
b: str = "hello" | ||
v2 = cast(int, b) | ||
|
||
|
||
def func3(a: int | None): | ||
v1 = cast(int, a) | ||
|
||
# This should generate an error if | ||
# reportUnnecessaryCast is enabled. | ||
v2 = cast(int | None, a) | ||
|
||
|
||
T = TypeVar("T") | ||
|
||
|
||
def func4(a: list[T]) -> list[T]: | ||
# This should generate an error if | ||
# reportUnnecessaryCast is enabled. | ||
v1 = cast(list[T], a) | ||
|
||
return a | ||
|
||
|
||
def func5(a: Never): | ||
# This should generate an error if | ||
# reportUnnecessaryCast is enabled. | ||
v1 = cast(NoReturn, a) | ||
|
||
|
||
def func6(a: type[int], b: int): | ||
v1 = cast(int, a) | ||
v2 = cast(type[int], b) | ||
|
||
# This should generate an error if | ||
# reportUnnecessaryCast is enabled. | ||
v3 = cast(type[int], a) | ||
|
||
# This should generate an error if | ||
# reportUnnecessaryCast is enabled. | ||
v4 = cast(int, b) |