Skip to content

Add unnecessary-type-conversion lint for str/int/float (fixes #3109)#3140

Open
ahaanbohra wants to merge 2 commits intofacebook:mainfrom
ahaanbohra:lint-str-conversion
Open

Add unnecessary-type-conversion lint for str/int/float (fixes #3109)#3140
ahaanbohra wants to merge 2 commits intofacebook:mainfrom
ahaanbohra:lint-str-conversion

Conversation

@ahaanbohra
Copy link
Copy Markdown

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.

def f(x: str) -> None:
    y = str(x)  # unnecessary-type-conversion

bool() is intentionally excluded since it is commonly used as an explicit truthiness check rather than a type conversion.

Fixes #3109

Test Plan

  • Added pyrefly/lib/test/unnecessary_type_conversion.rs with test cases covering: redundant str/int/float conversions, non-redundant conversions (int(x: bool)), Any arguments, and no-arg calls
  • All existing tests pass (cargo test)

@meta-cla
Copy link
Copy Markdown

meta-cla bot commented Apr 15, 2026

Hi @ahaanbohra!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla
Copy link
Copy Markdown

meta-cla bot commented Apr 15, 2026

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla bot added the cla signed label Apr 15, 2026
@meta-cla
Copy link
Copy Markdown

meta-cla bot commented Apr 15, 2026

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Comment thread pyrefly/lib/alt/call.rs Outdated
range: TextRange,
errors: &ErrorCollector,
) {
let builtin_names = ["str", "int", "float"];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
"#,
);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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)
bytes(x) where x: bytes — same reasoning

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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

@ahaanbohra ahaanbohra force-pushed the lint-str-conversion branch from 363c4e0 to 141bfa8 Compare April 16, 2026 00:43
@github-actions github-actions bot added size/m and removed size/m labels Apr 16, 2026
Comment thread pyrefly/lib/alt/call.rs
range: TextRange,
errors: &ErrorCollector,
) {
let builtin_names = ["str", "int", "float", "bytes"];
Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Author

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.

Copy link
Copy Markdown
Contributor

@NathanTempest NathanTempest Apr 16, 2026

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:

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
"#,
);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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


## 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.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add bytes to this built in types

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

lint unnecessary type convert

3 participants