-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A cast is considered redundant if the target type of the cast is the same as the inferred type of the expression. A cast to a supertype like `cast(object, 1)` is not considered redundant because such a cast could be needed to work around deficiencies in type inference. Fixes #958.
- Loading branch information
Showing
7 changed files
with
54 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,7 @@ | |
'check-bound.test', | ||
'check-optional.test', | ||
'check-fastparse.test', | ||
'check-warnings.test', | ||
] | ||
|
||
|
||
|
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,36 @@ | ||
-- Test cases for warning generation. | ||
|
||
-- Redundant casts | ||
-- --------------- | ||
|
||
[case testRedundantCast] | ||
# flags: warn-redundant-casts | ||
from typing import cast | ||
a = 1 | ||
b = cast(str, a) | ||
c = cast(int, a) | ||
[out] | ||
main:5: note: Redundant cast to "int" | ||
|
||
[case testRedundantCastWithIsinstance] | ||
# flags: warn-redundant-casts | ||
from typing import cast, Union | ||
x = 1 # type: Union[int, str] | ||
if isinstance(x, str): | ||
cast(str, x) | ||
[builtins fixtures/isinstance.py] | ||
[out] | ||
main:5: note: Redundant cast to "str" | ||
|
||
[case testCastToSuperclassNotRedundant] | ||
# flags: warn-redundant-casts | ||
from typing import cast, TypeVar, List | ||
T = TypeVar('T') | ||
def add(xs: List[T], ys: List[T]) -> List[T]: pass | ||
class A: pass | ||
class B(A): pass | ||
a = A() | ||
b = B() | ||
# Without the cast, the following line would fail to type check. | ||
c = add([cast(A, b)], [a]) | ||
[builtins fixtures/list.py] |