Add unnecessary-type-conversion lint for str/int/float (fixes #3109)#3140
Add unnecessary-type-conversion lint for str/int/float (fixes #3109)#3140ahaanbohra wants to merge 2 commits intofacebook:mainfrom
Conversation
|
Hi @ahaanbohra! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
| range: TextRange, | ||
| errors: &ErrorCollector, | ||
| ) { | ||
| let builtin_names = ["str", "int", "float"]; |
There was a problem hiding this comment.
bool is mentioned as a redundant data type in error_kind.rs, but it's not included in builtin_names here. bool is immutable, so bool(x) when x: bool is always redundant. Consider adding "bool" (and potentially "bytes") to the list.
There was a problem hiding this comment.
I looked into adding bool, but I dont think it is always redundant. For example, in test_bool_special_exports_bug, bool(x) where x: bool is used in a conditional to drive type narrowing (e.g., narrowing to Literal[True] / Literal[False]). In this case, the call affects control flow, so flagging it as an unnecessary conversion would likely be incorrect.
Because of that, I’ve removed bool from the set of checked builtins and redundant data types for now. Would be glad to revisit if we're sure thats the direction we want, either by refining the lint to be context-aware (e.g., skipping conditionals) or by adjusting expectations if that’s the intended direction.
| def f() -> None: | ||
| y = str() # OK - no argument | ||
| "#, | ||
| ); |
There was a problem hiding this comment.
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)
bytes(x) where x: bytes — same reasoning
There was a problem hiding this comment.
bytes is added but bool is not, we are just trying to be as comprehensive as possible for our typechecker
363c4e0 to
141bfa8
Compare
| range: TextRange, | ||
| errors: &ErrorCollector, | ||
| ) { | ||
| let builtin_names = ["str", "int", "float", "bytes"]; |
There was a problem hiding this comment.
Bool should be added to the built_in names here
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
def f(x: bool):
if bool(x):
assert_type(x, Literal[True])
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 write if 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:
from typing import Literal
def f(x: Literal[True]) -> None:
y = bool(x) # OK - Literal[True] is not exactly bool
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.
| def f() -> None: | ||
| y = str() # OK - no argument | ||
| "#, | ||
| ); |
There was a problem hiding this comment.
bytes is added but bool is not, we are just trying to be as comprehensive as possible for our typechecker
|
|
||
| ## 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. |
There was a problem hiding this comment.
add bytes to this built in types
Summary
Adds a new unnecessary-type-conversion lint that warns when str(), int(), or float() is called on an argument that is already of that exact type, making the conversion redundant.
bool() is intentionally excluded since it is commonly used as an explicit truthiness check rather than a type conversion.
Fixes #3109
Test Plan