-
Notifications
You must be signed in to change notification settings - Fork 1.3k
add autofix for E721
#7965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add autofix for E721
#7965
Conversation
let right_side: String = match right_argument.unwrap() { | ||
Expr::Constant(ast::ExprConstant { value, .. }) => match value { | ||
ast::Constant::Str(..) => "str".to_string(), | ||
ast::Constant::Bytes(..) => "bytes".to_string(), | ||
ast::Constant::Int(..) => "int".to_string(), | ||
ast::Constant::Float(..) => "float".to_string(), | ||
ast::Constant::Complex { .. } => "complex".to_string(), | ||
ast::Constant::Bool(..) => "bool".to_string(), | ||
_ => continue, | ||
}, | ||
Expr::FString(ast::ExprFString { .. }) => "str".to_string(), | ||
Expr::Tuple(ast::ExprTuple { .. }) => "tuple".to_string(), | ||
Expr::List(ast::ExprList { .. }) => "list".to_string(), | ||
Expr::Set(ast::ExprSet { .. }) => "set".to_string(), | ||
Expr::Dict(ast::ExprDict { .. }) => "dict".to_string(), | ||
Expr::DictComp(_) => "dict".to_string(), | ||
Expr::BoolOp(_) => "bool".to_string(), | ||
Expr::ListComp(_) => "list".to_string(), | ||
Expr::SetComp(_) => "set".to_string(), | ||
_ => checker.generator().expr(right), | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a convenience method for this kind of thing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm there's the more limited
fn constant_type_name(constant: &Constant) -> &'static str { |
I don't think we've made a broader mapping yet
e827813
to
3d53fcf
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this fix is going to be hard to get right because there are really two valid outcomes: either using isinstance
, or comparing the types directly with is
. (See #7905 for the revised rule logic which better matches the current pycodestyle behavior.) I'm hesitant to recommend a fix when we don't have enough information about user intent.
I'm gonna close this one for now -- sorry for the churn, I appreciate the work but I think it will be hard to get right. |
Summary
Adds autofix for
E721
. Resolves the right-hand side into a builtin from a constant expression if possible.Notes:
if res == types.IntType:
which is not emitted due to an early guard clause on the left-hand side not usingtype()
, I have not fixed this due to added complexityTest Plan
cargo test
and manually