-
Notifications
You must be signed in to change notification settings - Fork 310
Add unnecessary-type-conversion lint for str/int/float (fixes #3109) #3140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| use crate::testcase; | ||
|
|
||
| testcase!( | ||
| test_str_to_str, | ||
| r#" | ||
| def f(x: str) -> None: | ||
| y = str(x) # E: Unnecessary `str()` call; argument is already of type `str` | ||
| "#, | ||
| ); | ||
|
|
||
| testcase!( | ||
| test_int_to_int, | ||
| r#" | ||
| def f(x: int) -> None: | ||
| y = int(x) # E: Unnecessary `int()` call; argument is already of type `int` | ||
| "#, | ||
| ); | ||
|
|
||
| testcase!( | ||
| test_float_to_float, | ||
| r#" | ||
| def f(x: float) -> None: | ||
| y = float(x) # E: Unnecessary `float()` call; argument is already of type `float` | ||
| "#, | ||
| ); | ||
|
|
||
| testcase!( | ||
| test_int_to_str_ok, | ||
| r#" | ||
| def f(x: int) -> None: | ||
| y = str(x) # OK - converting int to str | ||
| "#, | ||
| ); | ||
|
|
||
| testcase!( | ||
| test_bool_to_int_ok, | ||
| r#" | ||
| def f(x: bool) -> None: | ||
| y = int(x) # OK - bool is a subtype of int, types are not equal | ||
| "#, | ||
| ); | ||
|
|
||
| testcase!( | ||
| test_any_ok, | ||
| r#" | ||
| from typing import Any | ||
| def f(x: Any) -> None: | ||
| y = str(x) # OK - argument is Any, type is unknown | ||
| "#, | ||
| ); | ||
|
|
||
| testcase!( | ||
| test_bytes_to_bytes, | ||
| r#" | ||
| def f(x: bytes) -> None: | ||
| y = bytes(x) # E: Unnecessary `bytes()` call; argument is already of type `bytes` | ||
| "#, | ||
| ); | ||
|
|
||
| testcase!( | ||
| test_no_args_ok, | ||
| r#" | ||
| def f() -> None: | ||
| y = str() # OK - no argument | ||
| "#, | ||
| ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good test coverage but could you also add few cases: bool(x) where x: bool — should warn (if bool is added to the checked types)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bytes is added but bool is not, we are just trying to be as comprehensive as possible for our typechecker |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1372,6 +1372,17 @@ def test1(x: object) -> None: | |
|
|
||
| This check is relatively conservative and only warns on limited cases where the comparison is highly likely to be redundant. | ||
|
|
||
| ## unnecessary-type-conversion | ||
|
|
||
| This warning is raised when a builtin type constructor (`str`, `int`, `float`, or `bool`) is called on a value that is already of that type, making the conversion redundant. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add |
||
|
|
||
| The default severity of this diagnostic is `warn`. | ||
|
|
||
| ```python | ||
| def f(x: str) -> None: | ||
| y = str(x) # unnecessary-type-conversion: `x` is already of type `str` | ||
| ``` | ||
|
|
||
| ## unreachable | ||
|
|
||
| This error is raised when a `return` or `yield` can never be reached because it comes | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bool should be added to the built_in names here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding bool here causes test_bool_special_exports_bug to fail, where bool(x) is used in a conditional for type narrowing so it does not seem to be redundant. Happy to include it if we want to change that behavior, but wanted to flag this first.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think bool should be included in the lint. I think that the narrowing argument doesn't hold up. The test test_bool_special_exports_bug in generic_legacy.rs:820 shows:
But this narrowing comes from the if statement, not from bool(). Writing
if x:produces the exact same narrowing because bool(x) is a no-op here. The lint flagging it as unnecessary would actually be correct since the user could just writeif x:At runtime, bool(x) where x: bool is always a no-op , bool.new returns the same object unchanged. This is unlike bool(x) where x: str | None, which performs meaningful truthiness coercion and would not be flagged by this lint (since str | None != bool).
No other tool catches this today. Neither mypy, pyright, ruff, nor pylint flag bool(x) where x: bool. Adding it to pyrefly would be novel but correct and being comprehensive is a reasonable goal for a type checker.
One edge case to consider: bool(x) where x: Literal[True] is arguably not redundant since Literal[True] != bool the call widens the type. The lint should only fire when the argument type is exactly bool, not a Literal bool. Worth adding a test for this:
How about we implement this and check the mypy_primer results to see if any tests change. If the primer comes back clean, we have strong evidence that this doesn't cause false positives in real-world code. If it does flag something, we can use those concrete examples to decide whether to exclude bool or refine the lint.