Keystone’s pep8 extensions.
In order to make the review process faster and easier for core devs we are adding some Keystone specific pep8 checks. This will catch common errors so that core devs don’t have to.
There are two types of pep8 extensions. One is a function that takes either a physical or logical line. The physical or logical line is the first param in the function definition and can be followed by other parameters supported by pep8. The second type is a class that parses AST trees. For more info please see pep8.py.
Bases: ast.NodeVisitor
Provides a simple framework for writing AST-based checks.
Subclasses should implement visit_* methods like any other AST visitor implementation. When they detect an error for a particular node the method should call self.add_error(offending_node). Details about where in the code the error occurred will be pulled from the node object.
Subclasses should also provide a class variable named CHECK_DESC to be used for the human readable error message.
Bases: keystone.tests.hacking.checks.BaseASTChecker
Ensure that code does not use a None with assert(Not*)Equal.
Bases: keystone.tests.hacking.checks.BaseASTChecker
Bases: keystone.tests.hacking.checks.BaseASTChecker
Check for the use of mutable objects as function/method defaults.
We are only checking for list and dict literals at this time. This means that a developer could specify an instance of their own and cause a bug. The fix for this is probably more work than it’s worth because it will get caught during code review.
There should be a space after the # of block comments.
There is already a check in pep8 that enforces this rule for inline comments.
Okay: # this is a comment Okay: #!/usr/bin/python Okay: # this is a comment K002: #this is a comment
Should use a dict comprehension instead of a dict constructor.
PEP-0274 introduced dict comprehension with performance enhancement and it also makes code more readable.
Okay: lower_res = {k.lower(): v for k, v in res[1].items()} Okay: fool = dict(a=’a’, b=’b’) K008: lower_res = dict((k.lower(), v) for k, v in res[1].items()) K008: attrs = dict([(k, _from_json(v)) K008: dict([[i,i] for i in range(3)])