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 4 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
| 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
|
Added bool to the checked types and included the three test cases you suggested. Also updated test_bool_special_exports_bug in generic_legacy.rs to expect the lint, since bool(x) there is a no-op and if x produces the same narrowing. The Literal[True] case is handled by the existing equality check and does not fire. |
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
|
@NathanTempest has imported this pull request. If you are a Meta employee, you can view this in D102009018. |
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