-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Implement consider-merging-isinstance
#1009
Implement consider-merging-isinstance
#1009
Conversation
def isinstances(): | ||
"Examples of isinstances" | ||
var = range(10) | ||
|
||
# merged | ||
if isinstance(var[1], (int, float)): | ||
pass | ||
result = isinstance(var[2], (int, float)) | ||
|
||
# not merged | ||
if isinstance(var[3], int) or isinstance(var[3], float) or isinstance(var[3], list) and True: # [consider-merging-isinstance] | ||
pass | ||
result = isinstance(var[4], int) or isinstance(var[4], float) or isinstance(var[5], list) and False # [consider-merging-isinstance] | ||
|
||
result = isinstance(var[5], int) or True or isinstance(var[5], float) # [consider-merging-isinstance] | ||
|
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.
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.
RustPython's code:
OrTest: ast::Expr = {
<e1:AndTest> <location:@L> <e2:("or" AndTest)*> <end_location:@R> => {
if e2.is_empty() {
e1
} else {
let mut values = vec![e1];
values.extend(e2.into_iter().map(|e| e.1));
ast::Expr {
location,
end_location: Some(end_location),
custom: (),
node: ast::ExprKind::BoolOp { op: ast::Boolop::Or, values }
}
}
},
};
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.
Moving <location:@L>
before <e1:AndTest>
seems to work?
OrTest: ast::Expr = {
<location:@L> <e1:AndTest> <e2:("or" AndTest)*> <end_location:@R> => {
^^^^^^^^^^^^^
if e2.is_empty() {
e1
} else {
let mut values = vec![e1];
values.extend(e2.into_iter().map(|e| e.1));
ast::Expr {
location,
end_location: Some(end_location),
custom: (),
node: ast::ExprKind::BoolOp { op: ast::Boolop::Or, values }
}
}
},
};
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.
import ast
# Visitor to print out node locations
class V(ast.NodeVisitor):
def visit_BoolOp(self, node: ast.BoolOp) -> None:
print(
node.__class__.__name__,
(node.lineno, node.col_offset),
(node.end_lineno, node.end_col_offset),
)
self.generic_visit(node)
V().visit(ast.parse("a or b or c"))
prints out:
BoolOp (1, 0) (1, 11)
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.
Oh cool. Do you want to submit an upstream PR for this? Or I can.
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.
Filed RustPython/RustPython#4306.
#970