I found that Pylint already contained some checkers I could use out of the box to check Python code:
checkers.refactoring
with settingmax-nested-blocks=1
for rule #1 (Use only one level of indentation per method.)checkers.design_analysis
with settingmax-branches=1
also for rule #1, but stronger, which I like more.checkers.design_analysis
with settingmax-attributes=2
for rule #7 (Don't use any classes with more than two instance variables.)
self
references.Dynamic Nature
Due to the dynamic nature of Python there is a grey area when working with types. For first-class collections (rule #8), only certain uses of collections are identified at the moment. I was not able to check for primitives at object boundaries at all, so rule #3 is not verified.
Figuring out how to write complex checkers for Pylint was difficult at first, but also a lot of fun. Like PMD and most static analysis tools I know, Pylint works with the Abstract Syntax Tree of the code and checkers are built using the Visitor design pattern. The main problem was figuring out which types of nodes exist, then the implementation of the rules was straight forward.
Python Project Setup
The setup is similar to the Java project. I created a repository for the LCD Numbers under lcd-numbers-object-calisthenics-python-setup. To test your setup there is some code in the project and
./run_pylint
will show its violations. Pylint is configured using the objectcalisthenics.pylintrc
file in the project directory. The individual checkers are in ./pylint/checkers
.
No comments:
Post a Comment