Performing static analysis with Pyflakes

We will perform static analysis of a part of the NumPy codebase. In order to do this, we will check out the code using Git. We will then run static analysis on part of the code using pyflakes.

How to do it...

  1. Check out the code.

    To check out the NumPy code, we will need Git. Installing Git is outside the scope of this book. The Git command to retrieve the code is as follows:

    git clone git://github.com/numpy/numpy.git numpy
    

    Alternatively, we can download a zip archive from https://github.com/numpy/numpy .

  2. Analyze the code.

    The previous step should have created a numpy directory with all the NumPy code. Go to this directory, and within it run the following command:

    $ pyflakes *.py
    pavement.py:71: redefinition of unused 'md5' from line 69
    pavement.py:88: redefinition of unused 'GIT_REVISION' from line 86
    pavement.py:314: 'virtualenv' imported but unused
    pavement.py:315: local variable 'e' is assigned to but never used
    pavement.py:380: local variable 'sdir' is assigned to but never used
    pavement.py:381: local variable 'bdir' is assigned to but never used
    pavement.py:536: local variable 'st' is assigned to but never used
    setup.py:21: 're' imported but unused
    setup.py:27: redefinition of unused 'builtins' from line 25
    setup.py:124: redefinition of unused 'GIT_REVISION' from line 118
    setupegg.py:17: 'setup' imported but unused
    setupscons.py:61: 'numpy' imported but unused
    setupscons.py:64: 'numscons' imported but unused
    setupsconsegg.py:6: 'setup' imported but unused
    

    This will run analysis on the code style, and check for PEP-8 violations in all the Python scripts within the current directory. You can also analyze a single file if you prefer.

How it works...

As you can see, it is pretty simple to analyze code style and look for PEP-8 violations with Pyflakes. The other advantage is speed; however, the number of error types that Pyflakes reports is pretty limited.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset