# eric7.Plugins.CheckerPlugins.CodeStyleChecker.Miscellaneous.MiscellaneousChecker

Module implementing a checker for miscellaneous checks.

### Global Attributes

None

### Classes

BugBearVisitor | Class implementing a node visitor to check for various topics.
DateTimeVisitor | Class implementing a node visitor to check datetime function calls.
LoggingVisitor | Class implementing a node visitor to check logging statements.
MiscellaneousChecker | Class implementing a checker for miscellaneous checks.
NameFinder | Class to extract a name out of a tree of nodes.
ReturnVisitor | Class implementing a node visitor to check return statements.
SysVersionVisitor | Class implementing a node visitor to check the use of sys.version and sys.version_info.
TextVisitor | Class implementing a node visitor for bytes and str instances.

### Functions

composeCallPath | Generator function to assemble the call path of a given node.

## BugBearVisitor

Class implementing a node visitor to check for various topics.

### Derived from

ast.NodeVisitor

### Class Attributes

NodeWindowSize

### Class Methods

None

### Methods

BugBearVisitor | Constructor
__checkForM502 | Private method to check the use of *strip().
__checkForM507 | Private method to check for unused loop variables.
visit | Public method to traverse a given AST node.
visit_Assert | Public method to handle 'assert' statements.
visit_Assign | Public method to handle assignments.
visit_AsyncFor | Public method to handle 'for' statements.
visit_Attribute | Public method to handle attributes.
visit_Call | Public method to handle a function call.
visit_For | Public method to handle 'for' statements.
visit_JoinedStr | Public method to handle f-string arguments.
visit_UAdd | Public method to handle unary additions.

### Static Methods

None

#### BugBearVisitor (Constructor)

BugBearVisitor()

Constructor

#### BugBearVisitor.__checkForM502

__checkForM502(node)

Private method to check the use of *strip().

node (ast.Call)
  reference to the node to be processed

#### BugBearVisitor.__checkForM507

__checkForM507(node)

Private method to check for unused loop variables.

node (ast.For)
  reference to the node to be processed

#### BugBearVisitor.visit

visit(node)

Public method to traverse a given AST node.

node (ast.Node)
  AST node to be traversed

#### BugBearVisitor.visit_Assert

visit_Assert(node)

Public method to handle 'assert' statements.

node (ast.Assert)
  reference to the node to be processed

#### BugBearVisitor.visit_Assign

visit_Assign(node)

Public method to handle assignments.

node (ast.Assign)
  reference to the node to be processed

#### BugBearVisitor.visit_AsyncFor

visit_AsyncFor(node)

Public method to handle 'for' statements.

node (ast.AsyncFor)
  reference to the node to be processed

#### BugBearVisitor.visit_Attribute

visit_Attribute(node)

Public method to handle attributes.

node (ast.Attribute)
  reference to the node to be processed

#### BugBearVisitor.visit_Call

visit_Call(node)

Public method to handle a function call.

node (ast.Call)
  reference to the node to be processed

#### BugBearVisitor.visit_For

visit_For(node)

Public method to handle 'for' statements.

node (ast.For)
  reference to the node to be processed

#### BugBearVisitor.visit_JoinedStr

visit_JoinedStr(node)

Public method to handle f-string arguments.

node (ast.JoinedStr)
  reference to the node to be processed

#### BugBearVisitor.visit_UAdd

visit_UAdd(node)

Public method to handle unary additions.

node (ast.UAdd)
  reference to the node to be processed

Up

## DateTimeVisitor

Class implementing a node visitor to check datetime function calls.

Note: This class is modelled after flake8_datetimez checker.

### Derived from

ast.NodeVisitor

### Class Attributes

None

### Class Methods

None

### Methods

DateTimeVisitor | Constructor
__getFromKeywords | Private method to get a keyword node given its name.
visit_Call | Public method to handle a function call.

### Static Methods

None

#### DateTimeVisitor (Constructor)

DateTimeVisitor()

Constructor

#### DateTimeVisitor.__getFromKeywords

__getFromKeywords(keywords, name)

Private method to get a keyword node given its name.

keywords (list of ast.AST)
  list of keyword argument nodes
name (str)
  name of the keyword node

Return:
  keyword node

Return Type:
  ast.AST

#### DateTimeVisitor.visit_Call

visit_Call(node)

Public method to handle a function call.

Every datetime related function call is check for use of the naive variant (i.e. use without TZ info).

node (ast.Call)
  reference to the node to be processed

Up

## LoggingVisitor

Class implementing a node visitor to check logging statements.

### Derived from

ast.NodeVisitor

### Class Attributes

LoggingLevels

### Class Methods

None

### Methods

LoggingVisitor | Constructor
__detectLoggingLevel | Private method to decide whether an AST Call is a logging call.
__isFormatCall | Private method to check if a function call uses format.
__withinExtraKeyword | Private method to check, if we are inside the extra keyword.
__withinLoggingArgument | Private method to check, if we are inside a logging argument.
__withinLoggingStatement | Private method to check, if we are inside a logging statement.
visit_BinOp | Public method to handle binary operations while processing the first logging argument.
visit_Call | Public method to handle a function call.
visit_JoinedStr | Public method to handle f-string arguments.

### Static Methods

None

#### LoggingVisitor (Constructor)

LoggingVisitor()

Constructor

#### LoggingVisitor.__detectLoggingLevel

__detectLoggingLevel(node)

Private method to decide whether an AST Call is a logging call.

node (ast.Call)
  reference to the node to be processed

Return:
  logging level

Return Type:
  str or None

#### LoggingVisitor.__isFormatCall

__isFormatCall(node)

Private method to check if a function call uses format.

node (ast.Call)
  reference to the node to be processed

Return:
  flag indicating the function call uses format

Return Type:
  bool

#### LoggingVisitor.__withinExtraKeyword

__withinExtraKeyword(node)

Private method to check, if we are inside the extra keyword.

node (ast.keyword)
  reference to the node to be checked

Return:
  flag indicating we are inside the extra keyword

Return Type:
  bool

#### LoggingVisitor.__withinLoggingArgument

__withinLoggingArgument()

Private method to check, if we are inside a logging argument.

Return:
  flag indicating we are inside a logging argument

Return Type:
  bool

#### LoggingVisitor.__withinLoggingStatement

__withinLoggingStatement()

Private method to check, if we are inside a logging statement.

Return:
  flag indicating we are inside a logging statement

Return Type:
  bool

#### LoggingVisitor.visit_BinOp

visit_BinOp(node)

Public method to handle binary operations while processing the first logging argument.

node (ast.BinOp)
  reference to the node to be processed

#### LoggingVisitor.visit_Call

visit_Call(node)

Public method to handle a function call.

Every logging statement and string format is expected to be a function call.

node (ast.Call)
  reference to the node to be processed

#### LoggingVisitor.visit_JoinedStr

visit_JoinedStr(node)

Public method to handle f-string arguments.

node (ast.JoinedStr)
  reference to the node to be processed

Up

## MiscellaneousChecker

Class implementing a checker for miscellaneous checks.

### Derived from

None

### Class Attributes

BuiltinsWhiteList
Codes
FormatFieldRegex
Formatter

### Class Methods

None

### Methods

MiscellaneousChecker | Constructor
__checkBugBear | Private method for bugbear checks.
__checkBuiltins | Private method to check, if built-ins are shadowed.
__checkCoding | Private method to check the presence of a coding line and valid encodings.
__checkCommentedCode | Private method to check for commented code.
__checkComprehensions | Private method to check some comprehension related things.
__checkCopyright | Private method to check the presence of a copyright statement.
__checkDateTime | Private method to check use of naive datetime functions.
__checkDictWithSortedKeys | Private method to check, if dictionary keys appear in sorted order.
__checkFormatString | Private method to check string format strings.
__checkFuture | Private method to check the __future__ imports.
__checkGettext | Private method to check the 'gettext' import statement.
__checkLineContinuation | Private method to check line continuation using backslash.
__checkLogging | Private method to check logging statements.
__checkMutableDefault | Private method to check for use of mutable types as default arguments.
__checkPep3101 | Private method to check for old style string formatting.
__checkPrintStatements | Private method to check for print statements.
__checkReturn | Private method to check return statements.
__checkSysVersion | Private method to check the use of sys.version and sys.version_info.
__checkTuple | Private method to check for one element tuples.
__dictShouldBeChecked | Private function to test, if the node should be checked.
__error | Private method to record an issue.
__getCoding | Private method to get the defined coding of the source.
__getFields | Private method to extract the format field information.
__ignoreCode | Private method to check if the message code should be ignored.
run | Public method to check the given source against miscellaneous conditions.

### Static Methods

None

#### MiscellaneousChecker (Constructor)

MiscellaneousChecker(source, filename, tree, select, ignore, expected, repeat, args)

Constructor

source (list of str)
  source code to be checked
filename (str)
  name of the source file
tree (ast.Module)
  AST tree of the source code
select (list of str)
  list of selected codes
ignore (list of str)
  list of codes to be ignored
expected (list of str)
  list of expected codes
repeat (bool)
  flag indicating to report each occurrence of a code
args (dict)
  dictionary of arguments for the miscellaneous checks

#### MiscellaneousChecker.__checkBugBear

__checkBugBear()

Private method for bugbear checks.

#### MiscellaneousChecker.__checkBuiltins

__checkBuiltins()

Private method to check, if built-ins are shadowed.

#### MiscellaneousChecker.__checkCoding

__checkCoding()

Private method to check the presence of a coding line and valid encodings.

#### MiscellaneousChecker.__checkCommentedCode

__checkCommentedCode()

Private method to check for commented code.

#### MiscellaneousChecker.__checkComprehensions

__checkComprehensions()

Private method to check some comprehension related things.

#### MiscellaneousChecker.__checkCopyright

__checkCopyright()

Private method to check the presence of a copyright statement.

#### MiscellaneousChecker.__checkDateTime

__checkDateTime()

Private method to check use of naive datetime functions.

#### MiscellaneousChecker.__checkDictWithSortedKeys

__checkDictWithSortedKeys()

Private method to check, if dictionary keys appear in sorted order.

#### MiscellaneousChecker.__checkFormatString

__checkFormatString()

Private method to check string format strings.

#### MiscellaneousChecker.__checkFuture

__checkFuture()

Private method to check the __future__ imports.

#### MiscellaneousChecker.__checkGettext

__checkGettext()

Private method to check the 'gettext' import statement.

#### MiscellaneousChecker.__checkLineContinuation

__checkLineContinuation()

Private method to check line continuation using backslash.

#### MiscellaneousChecker.__checkLogging

__checkLogging()

Private method to check logging statements.

#### MiscellaneousChecker.__checkMutableDefault

__checkMutableDefault()

Private method to check for use of mutable types as default arguments.

#### MiscellaneousChecker.__checkPep3101

__checkPep3101()

Private method to check for old style string formatting.

#### MiscellaneousChecker.__checkPrintStatements

__checkPrintStatements()

Private method to check for print statements.

#### MiscellaneousChecker.__checkReturn

__checkReturn()

Private method to check return statements.

#### MiscellaneousChecker.__checkSysVersion

__checkSysVersion()

Private method to check the use of sys.version and sys.version_info.

#### MiscellaneousChecker.__checkTuple

__checkTuple()

Private method to check for one element tuples.

#### MiscellaneousChecker.__dictShouldBeChecked

__dictShouldBeChecked(node)

Private function to test, if the node should be checked.

node
  reference to the AST node

Return:
  flag indicating to check the node

Return Type:
  bool

#### MiscellaneousChecker.__error

__error(lineNumber, offset, code, *args)

Private method to record an issue.

lineNumber (int)
  line number of the issue
offset (int)
  position within line of the issue
code (str)
  message code
args (list)
  arguments for the message

#### MiscellaneousChecker.__getCoding

__getCoding()

Private method to get the defined coding of the source.

Return:
  tuple containing the line number and the coding

Return Type:
  tuple of int and str

#### MiscellaneousChecker.__getFields

__getFields(string)

Private method to extract the format field information.

string (str)
  format string to be parsed

Return:
  format field information as a tuple with fields, implicit field definitions present and explicit field definitions present

Return Type:
  tuple of set of str, bool, bool

#### MiscellaneousChecker.__ignoreCode

__ignoreCode(code)

Private method to check if the message code should be ignored.

code (str)
  message code to check for

Return:
  flag indicating to ignore the given code

Return Type:
  bool

#### MiscellaneousChecker.run

run()

Public method to check the given source against miscellaneous conditions.

Up

## NameFinder

Class to extract a name out of a tree of nodes.

### Derived from

ast.NodeVisitor

### Class Attributes

None

### Class Methods

None

### Methods

NameFinder | Constructor
getNames | Public method to return the extracted names and Name nodes.
visit | Public method to traverse a given AST node.
visit_Name | Public method to handle 'Name' nodes.

### Static Methods

None

#### NameFinder (Constructor)

NameFinder()

Constructor

#### NameFinder.getNames

getNames()

Public method to return the extracted names and Name nodes.

Return:
  dictionary containing the names as keys and the list of nodes

Return Type:
  dict

#### NameFinder.visit

visit(node)

Public method to traverse a given AST node.

node (ast.Node)
  AST node to be traversed

#### NameFinder.visit_Name

visit_Name(node)

Public method to handle 'Name' nodes.

node (ast.Name)
  reference to the node to be processed

Up

## ReturnVisitor

Class implementing a node visitor to check return statements.

### Derived from

ast.NodeVisitor

### Class Attributes

Assigns
Refs
Returns

### Class Methods

None

### Methods

ReturnVisitor | Constructor
__checkFunction | Private method to check a function definition node.
__checkImplicitReturn | Private method to check for an implicit return statement.
__checkImplicitReturnValue | Private method to check for implicit return values.
__checkUnnecessaryAssign | Private method to check for an unnecessary assign statement.
__checkUnnecessaryReturnNone | Private method to check for an unnecessary 'return None' statement.
__hasRefsBeforeNextAssign | Private method to check for references before a following assign statement.
__isFalse | Private method to check, if a node value is False.
__isNone | Private method to check, if a node value is None.
__resultExists | Private method to check the existance of a return result.
__visitAssignTarget | Private method to handle an assign target node.
__visitLoop | Private method to handle loop nodes.
__visitWithStack | Private method to traverse a given function node using a stack.
assigns | Public method to get the Assign nodes.
refs | Public method to get the References nodes.
returns | Public method to get the Return nodes.
visit_Assign | Public method to handle an assign node.
visit_AsyncFor | Public method to handle an async for loop.
visit_AsyncFunctionDef | Public method to handle a function definition.
visit_For | Public method to handle a for loop.
visit_FunctionDef | Public method to handle a function definition.
visit_Name | Public method to handle a name node.
visit_Return | Public method to handle a return node.
visit_While | Public method to handle a while loop.

### Static Methods

None

#### ReturnVisitor (Constructor)

ReturnVisitor()

Constructor

#### ReturnVisitor.__checkFunction

__checkFunction(node)

Private method to check a function definition node.

node (ast.AsyncFunctionDef or ast.FunctionDef)
  reference to the node to check

#### ReturnVisitor.__checkImplicitReturn

__checkImplicitReturn(node)

Private method to check for an implicit return statement.

node (ast.AST)
  reference to the node to check

#### ReturnVisitor.__checkImplicitReturnValue

__checkImplicitReturnValue()

Private method to check for implicit return values.

#### ReturnVisitor.__checkUnnecessaryAssign

__checkUnnecessaryAssign(node)

Private method to check for an unnecessary assign statement.

node (ast.AST)
  reference to the node to check

#### ReturnVisitor.__checkUnnecessaryReturnNone

__checkUnnecessaryReturnNone()

Private method to check for an unnecessary 'return None' statement.

#### ReturnVisitor.__hasRefsBeforeNextAssign

__hasRefsBeforeNextAssign(varname, returnLineno)

Private method to check for references before a following assign statement.

varname (str)
  variable name to check for
returnLineno (int)
  line number of the return statement

Return:
  flag indicating the existence of references

Return Type:
  bool

#### ReturnVisitor.__isFalse

__isFalse(node)

Private method to check, if a node value is False.

node (ast.AST)
  reference to the node to check

Return:
  flag indicating the node contains a False value

Return Type:
  bool

#### ReturnVisitor.__isNone

__isNone(node)

Private method to check, if a node value is None.

node (ast.AST)
  reference to the node to check

Return:
  flag indicating the node contains a None value

Return Type:
  bool

#### ReturnVisitor.__resultExists

__resultExists()

Private method to check the existance of a return result.

Return:
  flag indicating the existence of a return result

Return Type:
  bool

#### ReturnVisitor.__visitAssignTarget

__visitAssignTarget(node)

Private method to handle an assign target node.

node (ast.AST)
  reference to the node to handle

#### ReturnVisitor.__visitLoop

__visitLoop(node)

Private method to handle loop nodes.

node (ast.For, ast.AsyncFor or ast.While)
  reference to the loop node to handle

#### ReturnVisitor.__visitWithStack

__visitWithStack(node)

Private method to traverse a given function node using a stack.

node (ast.FunctionDef or ast.AsyncFunctionDef)
  AST node to be traversed

#### ReturnVisitor.assigns

assigns()

Public method to get the Assign nodes.

Return:
  dictionary containing the node name as key and line number as value

Return Type:
  dict

#### ReturnVisitor.refs

refs()

Public method to get the References nodes.

Return:
  dictionary containing the node name as key and line number as value

Return Type:
  dict

#### ReturnVisitor.returns

returns()

Public method to get the Return nodes.

Return:
  dictionary containing the node name as key and line number as value

Return Type:
  dict

#### ReturnVisitor.visit_Assign

visit_Assign(node)

Public method to handle an assign node.

node (ast.Assign)
  reference to the node to handle

#### ReturnVisitor.visit_AsyncFor

visit_AsyncFor(node)

Public method to handle an async for loop.

node (ast.AsyncFor)
  reference to the async for node to handle

#### ReturnVisitor.visit_AsyncFunctionDef

visit_AsyncFunctionDef(node)

Public method to handle a function definition.

node (ast.AsyncFunctionDef)
  reference to the node to handle

#### ReturnVisitor.visit_For

visit_For(node)

Public method to handle a for loop.

node (ast.For)
  reference to the for node to handle

#### ReturnVisitor.visit_FunctionDef

visit_FunctionDef(node)

Public method to handle a function definition.

node (ast.FunctionDef)
  reference to the node to handle

#### ReturnVisitor.visit_Name

visit_Name(node)

Public method to handle a name node.

node (ast.Name)
  reference to the node to handle

#### ReturnVisitor.visit_Return

visit_Return(node)

Public method to handle a return node.

node (ast.Return)
  reference to the node to handle

#### ReturnVisitor.visit_While

visit_While(node)

Public method to handle a while loop.

node (ast.While)
  reference to the while node to handle

Up

## SysVersionVisitor

Class implementing a node visitor to check the use of sys.version and sys.version_info.

Note: This class is modelled after flake8-2020 checker.

### Derived from

ast.NodeVisitor

### Class Attributes

None

### Class Methods

None

### Methods

SysVersionVisitor | Constructor
__isSys | Private method to check for a reference to sys attribute.
__isSysVersionUpperSlice | Private method to check the upper slice of sys.version.
visit_Attribute | Public method to handle an attribute.
visit_Compare | Public method to handle a comparison.
visit_ImportFrom | Public method to handle a from ...
visit_Name | Public method to handle an name.
visit_Subscript | Public method to handle a subscript.

### Static Methods

None

#### SysVersionVisitor (Constructor)

SysVersionVisitor()

Constructor

#### SysVersionVisitor.__isSys

__isSys(attr, node)

Private method to check for a reference to sys attribute.

attr (str)
  attribute name
node (ast.Node)
  reference to the node to be checked

Return:
  flag indicating a match

Return Type:
  bool

#### SysVersionVisitor.__isSysVersionUpperSlice

__isSysVersionUpperSlice(node, n)

Private method to check the upper slice of sys.version.

node (ast.Node)
  reference to the node to be checked
n (int)
  slice value to check against

Return:
  flag indicating a match

Return Type:
  bool

#### SysVersionVisitor.visit_Attribute

visit_Attribute(node)

Public method to handle an attribute.

node (ast.Attribute)
  reference to the node to be processed

#### SysVersionVisitor.visit_Compare

visit_Compare(node)

Public method to handle a comparison.

node (ast.Compare)
  reference to the node to be processed

#### SysVersionVisitor.visit_ImportFrom

visit_ImportFrom(node)

Public method to handle a from ... import ... statement.

node (ast.ImportFrom)
  reference to the node to be processed

#### SysVersionVisitor.visit_Name

visit_Name(node)

Public method to handle an name.

node (ast.Name)
  reference to the node to be processed

#### SysVersionVisitor.visit_Subscript

visit_Subscript(node)

Public method to handle a subscript.

node (ast.Subscript)
  reference to the node to be processed

Up

## TextVisitor

Class implementing a node visitor for bytes and str instances.

It tries to detect docstrings as string of the first expression of each module, class or function.

### Derived from

ast.NodeVisitor

### Class Attributes

None

### Class Methods

None

### Methods

TextVisitor | Constructor
__addNode | Private method to add a node to our list of nodes.
__visitBody | Private method to traverse the body of the node manually.
__visitDefinition | Private method handling class and function definitions.
visit_AsyncFunctionDef | Public method to handle an asynchronous function definition.
visit_Bytes | Public method to record a bytes node.
visit_Call | Public method to handle a function call.
visit_ClassDef | Public method to handle a class definition.
visit_Constant | Public method to handle constant nodes.
visit_FunctionDef | Public method to handle a function definition.
visit_Module | Public method to handle a module.
visit_Str | Public method to record a string node.

### Static Methods

None

#### TextVisitor (Constructor)

TextVisitor()

Constructor

#### TextVisitor.__addNode

__addNode(node)

Private method to add a node to our list of nodes.

node (ast.AST)
  reference to the node to add

#### TextVisitor.__visitBody

__visitBody(node)

Private method to traverse the body of the node manually.

If the first node is an expression which contains a string or bytes it marks that as a docstring.

node (ast.AST)
  reference to the node to traverse

#### TextVisitor.__visitDefinition

__visitDefinition(node)

Private method handling class and function definitions.

node (ast.FunctionDef, ast.AsyncFunctionDef or ast.ClassDef)
  reference to the node to handle

#### TextVisitor.visit_AsyncFunctionDef

visit_AsyncFunctionDef(node)

Public method to handle an asynchronous function definition.

node (ast.AsyncFunctionDef)
  reference to the node to handle

#### TextVisitor.visit_Bytes

visit_Bytes(node)

Public method to record a bytes node.

node (ast.Bytes)
  reference to the bytes node

#### TextVisitor.visit_Call

visit_Call(node)

Public method to handle a function call.

node (ast.Call)
  reference to the node to handle

#### TextVisitor.visit_ClassDef

visit_ClassDef(node)

Public method to handle a class definition.

node (ast.ClassDef)
  reference to the node to handle

#### TextVisitor.visit_Constant

visit_Constant(node)

Public method to handle constant nodes.

node (ast.Constant)
  reference to the bytes node

#### TextVisitor.visit_FunctionDef

visit_FunctionDef(node)

Public method to handle a function definition.

node (ast.FunctionDef)
  reference to the node to handle

#### TextVisitor.visit_Module

visit_Module(node)

Public method to handle a module.

node (ast.Module)
  reference to the node to handle

#### TextVisitor.visit_Str

visit_Str(node)

Public method to record a string node.

node (ast.Str)
  reference to the string node

Up

## composeCallPath

composeCallPath(node)

Generator function to assemble the call path of a given node.

node (ast.Node)
  node to assemble call path for

Yield:
  call path components

Yield Type:
  str

Up