Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pyrefly/lib/alt/special_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,19 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
"`typing.cast` missing required argument `val`".to_owned(),
);
}
if let Some(val_expr) = val {
let val_type = self.expr_infer(val_expr, errors);

if self.subtype(&val_type, &ret) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think subtype is a function?

Try is_subset_eq

self.error(
errors,
range,
ErrorKind::RedundantCast,
None,
"`typing.cast` is unnecessary because the value already has the target type".to_owned(),
Copy link
Contributor

Choose a reason for hiding this comment

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

this message is slightly misleading if the thing being cast is Any

why not something simpler like "redundant cast to {}"? note that when we pass the type we should call .deterministic_printing() on it

);
}
}
ret
}

Expand Down
4 changes: 4 additions & 0 deletions pyrefly/lib/error/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ pub enum ErrorKind {
ParseError,
/// The attribute exists but cannot be modified.
ReadOnly,
/// Raised when a cast is unnecessary because the value already has the target type.
/// It is always a warning.
RedundantCast,
/// Raised by a call to reveal_type().
RevealType,
/// An error related to type alias usage or definition.
Expand Down Expand Up @@ -226,6 +229,7 @@ impl ErrorKind {
match self {
ErrorKind::RevealType => Severity::Info,
ErrorKind::Deprecated => Severity::Warn,
ErrorKind::RedundantCast => Severity::Warn,
_ => Severity::Error,
}
}
Expand Down
38 changes: 38 additions & 0 deletions pyrefly/lib/test/redundant_cast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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_redundant_cast_simple,
r#"
from typing import cast
x = cast(int, 5) # E: Redundant cast: expression already has type `int`
Copy link
Contributor

Choose a reason for hiding this comment

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

these messages don't seem to match the implementaiton in special_calls.rs

y = cast(str, "hello") # E: Redundant cast: expression already has type `str`
"#,
);

testcase!(
test_redundant_cast_with_annotation,
r#"
from typing import cast
x: int = cast(int, 42) # E: Redundant cast: expression already has type `int`
"#,
);

testcase!(
test_non_redundant_cast,
r#"
from typing import Any, cast
def deserialize(data: Any) -> int:
return cast(int, data)
"#,
);
Loading