eric7.Plugins.CheckerPlugins.CodeStyleChecker.Simplify.SimplifyNodeVisitor

Module implementing a node visitor checking for code that could be simplified.

Global Attributes

None

Classes

SimplifyNodeVisitor Class to traverse the AST node tree and check for code that can be simplified.

Functions

None


SimplifyNodeVisitor

Class to traverse the AST node tree and check for code that can be simplified.

Derived from

ast.NodeVisitor

Class Attributes

None

Class Methods

None

Methods

SimplifyNodeVisitor Constructor
__bodyContainsContinue Private method to check, if a list of statements contain a 'continue' statement.
__check101 Private method to check for duplicate isinstance() calls.
__check102 Private method to check for nested if statements without else blocks.
__check103 Private method to check for calls that wrap a condition to return a bool.
__check104 Private method to check for "iterate and yield" patterns.
__check105 Private method to check for "try-except-pass" patterns.
__check106 Private method to check for calls where an exception is raised in else.
__check107 Private method to check for calls where try/except and finally have 'return'.
__check108 Private method to check for if-elses which could be a ternary operator assignment.
__check109 Private method to check for multiple equalities with the same value are combined via "or".
__check110_111 Private method to check if any / all could be used.
__check112 Private method to check for non-capitalized calls to environment variables.
__check113 Private method to check for loops in which "enumerate" should be used.
__check114 Private method to check for alternative if clauses with identical bodies.
__check115 Private method to to check for places where open() is called without a context handler.
__check116 Private method to check for places with 3 or more consecutive if-statements with direct returns.
__check117 Private method to check for multiple with-statements with same scope.
__check118 Private method to check for usages of "key in dict.keys()".
__check119 Private method to check for classes that should be "dataclasses".
__check120_121 Private method to check for classes that inherit from object.
__check122 Private method to check for all if-blocks which only check if a key is in a dictionary.
__check123 Private method to check for complicated dictionary access with default value.
__check181 Private method to check for assignments that could be converted into an augmented assignment.
__check182 Private method to check for calls of type 'super()' that could be shortened to 'super()'.
__check201 Private method to check for calls where an unary 'not' is used for an unequality.
__check202 Private method to check for calls where an unary 'not' is used for an equality.
__check203 Private method to check for calls where an unary 'not' is used for an in-check.
__check204 Private method to check for calls of the type "not (a < b)".
__check205 Private method to check for calls of the type "not (a <= b)".
__check206 Private method to check for calls of the type "not (a > b)".
__check207 Private method to check for calls of the type "not (a >= b)".
__check208 Private method to check for calls of the type "not (not a)".
__check211 Private method to check for calls of the type "True if a else False".
__check212 Private method to check for calls of the type "False if a else True".
__check213 Private method to check for calls of the type "b if not a else a".
__check221 Private method to check for calls of the type "a and not a".
__check222 Private method to check for calls of the type "a or not a".
__check223 Private method to check for calls of the type "...
__check224 Private method to check for calls of the type "...
__check301 Private method to check for Yoda conditions.
__check401 Private method to check for bare boolean function arguments.
__check402 Private method to check for bare numeric function arguments.
__check901 Private method to check for unnecessary bool conversion.
__check904 Private method to check for dictionary initialization.
__check905 Private method to check for list initialization by splitting a string.
__check906 Private method to check for unnecessary nesting of os.path.join().
__check907 Private method to check for Union type annotation with None.
__check909 Private method to check for reflexive assignments.
__check910 Private method to check for uses of 'dict.get(key, None)'.
__check911 Private method to check for the expression "zip(_.keys(), _.values())".
__expressionUsesVariable Private method to check, if a variable is used by an expression.
__getDuplicatedIsinstanceCall Private method to get a list of isinstance arguments which could be combined.
__getIfBodyPairs Private method to extract a list of pairs of test and body for an If node.
__isConstantIncrease Private method to check an expression for being a constant increase.
__isExceptionCheck Private method to check, if the node is checking an exception.
__isSameBody Private method check, if the given bodies are equivalent.
__isSameExpression Private method to check, if two expressions are equal.
__isStatementEqual Private method to check, if two statements are equal.
__negateTest Private method negate the given Compare node.
getOsPathJoinArgs
visit_Assign Public method to process an Assign node.
visit_BoolOp Public method to process a BoolOp node.
visit_Call Public method to process a Call node.
visit_ClassDef Public method to process a ClassDef node.
visit_Compare Public method to process a Compare node.
visit_Expr Public method to process an Expr node.
visit_For Public method to process a For node.
visit_If Public method to process an If node.
visit_IfExp Public method to process an IfExp node.
visit_Subscript Public method to process a Subscript node.
visit_Try Public method to process a Try node.
visit_UnaryOp Public method to process a UnaryOp node.
visit_With Public method to process a With node.

Static Methods

None

SimplifyNodeVisitor (Constructor)

SimplifyNodeVisitor(errorCallback)

Constructor

errorCallback (func)
callback function to register an error

SimplifyNodeVisitor.__bodyContainsContinue

__bodyContainsContinue(stmts)

Private method to check, if a list of statements contain a 'continue' statement.

stmts (list of ast.stmt)
list of statements
Return:
flag indicating a continue statement
Return Type:
bool

SimplifyNodeVisitor.__check101

__check101(node)

Private method to check for duplicate isinstance() calls.

node (ast.BoolOp)
reference to the AST node to be checked

SimplifyNodeVisitor.__check102

__check102(node)

Private method to check for nested if statements without else blocks.

node (ast.If)
reference to the AST node to be checked

SimplifyNodeVisitor.__check103

__check103(node)

Private method to check for calls that wrap a condition to return a bool.

node (ast.If)
reference to the AST node to be checked

SimplifyNodeVisitor.__check104

__check104(node)

Private method to check for "iterate and yield" patterns.

node (ast.For)
reference to the AST node to be checked

SimplifyNodeVisitor.__check105

__check105(node)

Private method to check for "try-except-pass" patterns.

node (ast.Try)
reference to the AST node to be checked

SimplifyNodeVisitor.__check106

__check106(node)

Private method to check for calls where an exception is raised in else.

node (ast.If)
reference to the AST node to be checked

SimplifyNodeVisitor.__check107

__check107(node)

Private method to check for calls where try/except and finally have 'return'.

node (ast.Try)
reference to the AST node to be checked

SimplifyNodeVisitor.__check108

__check108(node)

Private method to check for if-elses which could be a ternary operator assignment.

node (ast.If)
reference to the AST node to be checked

SimplifyNodeVisitor.__check109

__check109(node)

Private method to check for multiple equalities with the same value are combined via "or".

node (ast.BoolOp)
reference to the AST node to be checked

SimplifyNodeVisitor.__check110_111

__check110_111(node)

Private method to check if any / all could be used.

node (ast.For)
reference to the AST node to be checked

SimplifyNodeVisitor.__check112

__check112(node)

Private method to check for non-capitalized calls to environment variables.

node (ast.Expr)
reference to the AST node to be checked

SimplifyNodeVisitor.__check113

__check113(node)

Private method to check for loops in which "enumerate" should be used.

node (ast.For)
reference to the AST node to be checked

SimplifyNodeVisitor.__check114

__check114(node)

Private method to check for alternative if clauses with identical bodies.

node (ast.If)
reference to the AST node to be checked

SimplifyNodeVisitor.__check115

__check115(node)

Private method to to check for places where open() is called without a context handler.

node (ast.Call)
reference to the AST node to be checked

SimplifyNodeVisitor.__check116

__check116(node)

Private method to check for places with 3 or more consecutive if-statements with direct returns.

* Each if-statement must be a check for equality with the same variable * Each if-statement must just have a "return" * Else must also just have a return

node (ast.If)
reference to the AST node to be checked

SimplifyNodeVisitor.__check117

__check117(node)

Private method to check for multiple with-statements with same scope.

node (ast.With)
reference to the AST node to be checked

SimplifyNodeVisitor.__check118

__check118(node)

Private method to check for usages of "key in dict.keys()".

node (ast.Compare or ast.For)
reference to the AST node to be checked

SimplifyNodeVisitor.__check119

__check119(node)

Private method to check for classes that should be "dataclasses".

node (ast.ClassDef)
reference to the AST node to be checked

SimplifyNodeVisitor.__check120_121

__check120_121(node)

Private method to check for classes that inherit from object.

node (ast.ClassDef)
reference to the AST node to be checked

SimplifyNodeVisitor.__check122

__check122(node)

Private method to check for all if-blocks which only check if a key is in a dictionary.

node (ast.If)
reference to the AST node to be checked

SimplifyNodeVisitor.__check123

__check123(node)

Private method to check for complicated dictionary access with default value.

node (ast.If)
reference to the AST node to be checked

SimplifyNodeVisitor.__check181

__check181(node)

Private method to check for assignments that could be converted into an augmented assignment.

node (ast.Assign)
reference to the AST node to be checked

SimplifyNodeVisitor.__check182

__check182(node)

Private method to check for calls of type 'super()' that could be shortened to 'super()'.

node (ast.Call)
reference to the AST node to be checked

SimplifyNodeVisitor.__check201

__check201(node)

Private method to check for calls where an unary 'not' is used for an unequality.

node (ast.UnaryOp)
reference to the UnaryOp node

SimplifyNodeVisitor.__check202

__check202(node)

Private method to check for calls where an unary 'not' is used for an equality.

node (ast.UnaryOp)
reference to the UnaryOp node

SimplifyNodeVisitor.__check203

__check203(node)

Private method to check for calls where an unary 'not' is used for an in-check.

node (ast.UnaryOp)
reference to the UnaryOp node

SimplifyNodeVisitor.__check204

__check204(node)

Private method to check for calls of the type "not (a < b)".

node (ast.UnaryOp)
reference to the UnaryOp node

SimplifyNodeVisitor.__check205

__check205(node)

Private method to check for calls of the type "not (a <= b)".

node (ast.UnaryOp)
reference to the UnaryOp node

SimplifyNodeVisitor.__check206

__check206(node)

Private method to check for calls of the type "not (a > b)".

node (ast.UnaryOp)
reference to the UnaryOp node

SimplifyNodeVisitor.__check207

__check207(node)

Private method to check for calls of the type "not (a >= b)".

node (ast.UnaryOp)
reference to the UnaryOp node

SimplifyNodeVisitor.__check208

__check208(node)

Private method to check for calls of the type "not (not a)".

node (ast.UnaryOp)
reference to the UnaryOp node

SimplifyNodeVisitor.__check211

__check211(node)

Private method to check for calls of the type "True if a else False".

node (ast.IfExp)
reference to the AST node to be checked

SimplifyNodeVisitor.__check212

__check212(node)

Private method to check for calls of the type "False if a else True".

node (ast.IfExp)
reference to the AST node to be checked

SimplifyNodeVisitor.__check213

__check213(node)

Private method to check for calls of the type "b if not a else a".

node (ast.IfExp)
reference to the AST node to be checked

SimplifyNodeVisitor.__check221

__check221(node)

Private method to check for calls of the type "a and not a".

node (ast.BoolOp)
reference to the AST node to be checked

SimplifyNodeVisitor.__check222

__check222(node)

Private method to check for calls of the type "a or not a".

node (ast.BoolOp)
reference to the AST node to be checked

SimplifyNodeVisitor.__check223

__check223(node)

Private method to check for calls of the type "... or True".

node (ast.BoolOp)
reference to the AST node to be checked

SimplifyNodeVisitor.__check224

__check224(node)

Private method to check for calls of the type "... and False".

node (ast.BoolOp)
reference to the AST node to be checked

SimplifyNodeVisitor.__check301

__check301(node)

Private method to check for Yoda conditions.

node (ast.Compare)
reference to the AST node to be checked

SimplifyNodeVisitor.__check401

__check401(node)

Private method to check for bare boolean function arguments.

node (ast.Call)
reference to the AST node to be checked

SimplifyNodeVisitor.__check402

__check402(node)

Private method to check for bare numeric function arguments.

node (ast.Call)
reference to the AST node to be checked

SimplifyNodeVisitor.__check901

__check901(node)

Private method to check for unnecessary bool conversion.

node (ast.Call)
reference to the AST node to be checked

SimplifyNodeVisitor.__check904

__check904(node)

Private method to check for dictionary initialization.

node (ast.Assign)
reference to the AST node to be checked

SimplifyNodeVisitor.__check905

__check905(node)

Private method to check for list initialization by splitting a string.

node (ast.Call)
reference to the AST node to be checked

SimplifyNodeVisitor.__check906

__check906(node)

Private method to check for unnecessary nesting of os.path.join().

node (ast.Call)
reference to the AST node to be checked

SimplifyNodeVisitor.__check907

__check907(node)

Private method to check for Union type annotation with None.

node (ast.Subscript)
reference to the AST node to be checked

SimplifyNodeVisitor.__check909

__check909(node)

Private method to check for reflexive assignments.

node (ast.Assign)
reference to the AST node to be checked

SimplifyNodeVisitor.__check910

__check910(node)

Private method to check for uses of 'dict.get(key, None)'.

node (ast.Call)
reference to the AST node to be checked

SimplifyNodeVisitor.__check911

__check911(node)

Private method to check for the expression "zip(_.keys(), _.values())".

node (ast.Call)
reference to the AST node to be checked

SimplifyNodeVisitor.__expressionUsesVariable

__expressionUsesVariable(expr, var)

Private method to check, if a variable is used by an expression.

expr (ast.expr)
expression node to be checked
var (str)
variable name to be checked for
Return:
flag indicating the expression uses the variable
Return Type:
bool

SimplifyNodeVisitor.__getDuplicatedIsinstanceCall

__getDuplicatedIsinstanceCall(node)

Private method to get a list of isinstance arguments which could be combined.

node (ast.BoolOp)
reference to the AST node to be inspected
Return:
list of variable names of duplicated isinstance calls
Return Type:
list of str

SimplifyNodeVisitor.__getIfBodyPairs

__getIfBodyPairs(node)

Private method to extract a list of pairs of test and body for an If node.

node (ast.If)
reference to the If node to be processed
Return:
list of pairs of test and body
Return Type:
list of tuples of (ast.expr, [ast.stmt])

SimplifyNodeVisitor.__isConstantIncrease

__isConstantIncrease(expr)

Private method to check an expression for being a constant increase.

expr (ast.AugAssign)
reference to the node to be checked
Return:
flag indicating a constant increase
Return Type:
bool

SimplifyNodeVisitor.__isExceptionCheck

__isExceptionCheck(node)

Private method to check, if the node is checking an exception.

node (ast.If)
reference to the node to be checked
Return:
flag indicating an exception check
Return Type:
bool

SimplifyNodeVisitor.__isSameBody

__isSameBody(body1, body2)

Private method check, if the given bodies are equivalent.

body1 (list of ast.stmt)
list of statements of the first body
body2 (list of ast.stmt)
list of statements of the second body
Return:
flag indicating identical bodies
Return Type:
bool

SimplifyNodeVisitor.__isSameExpression

__isSameExpression(a, b)

Private method to check, if two expressions are equal.

a (ast.expr)
first expression to be checked
b (ast.expr)
second expression to be checked
Return:
flag indicating equal expressions
Return Type:
bool

SimplifyNodeVisitor.__isStatementEqual

__isStatementEqual(a, b)

Private method to check, if two statements are equal.

a (ast.stmt)
reference to the first statement
b (ast.stmt)
reference to the second statement
Return:
flag indicating if the two statements are equal
Return Type:
bool

SimplifyNodeVisitor.__negateTest

__negateTest(node)

Private method negate the given Compare node.

node (ast.Compare)
reference to the node to be negated
Return:
node with negated logic
Return Type:
ast.Compare

SimplifyNodeVisitor.getOsPathJoinArgs

getOsPathJoinArgs()

SimplifyNodeVisitor.visit_Assign

visit_Assign(node)

Public method to process an Assign node.

node (ast.Assign)
reference to the Assign node

SimplifyNodeVisitor.visit_BoolOp

visit_BoolOp(node)

Public method to process a BoolOp node.

node (ast.BoolOp)
reference to the BoolOp node

SimplifyNodeVisitor.visit_Call

visit_Call(node)

Public method to process a Call node.

node (ast.Call)
reference to the Call node

SimplifyNodeVisitor.visit_ClassDef

visit_ClassDef(node)

Public method to process a ClassDef node.

node (ast.ClassDef)
reference to the ClassDef node

SimplifyNodeVisitor.visit_Compare

visit_Compare(node)

Public method to process a Compare node.

node (ast.Compare)
reference to the Compare node

SimplifyNodeVisitor.visit_Expr

visit_Expr(node)

Public method to process an Expr node.

node (ast.Expr)
reference to the Expr node

SimplifyNodeVisitor.visit_For

visit_For(node)

Public method to process a For node.

node (ast.For)
reference to the For node

SimplifyNodeVisitor.visit_If

visit_If(node)

Public method to process an If node.

node (ast.If)
reference to the If node

SimplifyNodeVisitor.visit_IfExp

visit_IfExp(node)

Public method to process an IfExp node.

node (ast.IfExp)
reference to the IfExp node

SimplifyNodeVisitor.visit_Subscript

visit_Subscript(node)

Public method to process a Subscript node.

node (ast.Subscript)
reference to the Subscript node

SimplifyNodeVisitor.visit_Try

visit_Try(node)

Public method to process a Try node.

node (ast.Try)
reference to the Try node

SimplifyNodeVisitor.visit_UnaryOp

visit_UnaryOp(node)

Public method to process a UnaryOp node.

node (ast.UnaryOp)
reference to the UnaryOp node

SimplifyNodeVisitor.visit_With

visit_With(node)

Public method to process a With node.

node (ast.With)
reference to the With node
Up