Skip to content

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

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

Add unnecessary-type-conversion lint for str/int/float (fixes #3109)#3140
ahaanbohra wants to merge 4 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 Outdated
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

Comment thread website/docs/error-kinds.mdx Outdated

## 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

@github-actions github-actions Bot added size/m and removed size/m labels Apr 16, 2026
@github-actions github-actions Bot added size/m and removed size/m labels Apr 16, 2026
@ahaanbohra
Copy link
Copy Markdown
Author

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.

@github-actions
Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@meta-codesync
Copy link
Copy Markdown
Contributor

meta-codesync Bot commented Apr 22, 2026

@NathanTempest has imported this pull request. If you are a Meta employee, you can view this in D102009018.

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