python lint using kwParsing
The goal of this module/filter is to help find
programming errors in python source files.
As a filter use thusly:
% python kjpylint.py source_file.py
As an internal tool use like this:
import kjpylint
(pyg, context) = kjpylint.setup()
kjpylint.lint(data, pyg, context)
where data is the text of a python program.
You can build your own context structure by
subclassing GlobalContext, and redefining
GlobalContext.complain(string) for example.
You could do a lot more than that too...
Also, to lint all *.py files recursively contained
in a directory hierarchy use
kjpylint.lintdir("/usr/local/lib/python") # for example
FEATURES:
Lint expects
1) a newline or two at the end of the data;
2) consistent indenting (and inconsistency may be invisible)
[eg " \t" and "\t" are not the same indent
to Lint, but Python sees them the same.]
If (1) or (2) are not satisfied Lint will raise
an exception.
Buglets: lambdas and for loops on one line generate
extraneous warnings.
Notes:
======
The lint process works, in outline, like this.
Scan over a python program
x = 1
def f(a):
a = x
d.x, y = b
z = w
and build annotations like
[ set("x", 1),
[
get("x", 4)
set("a", 4)
get("b", 5)
get("d", 5)
set("y", 5)
pop_local()
]
get("w", 7)
set("z", 7) ]
from this stream conclude
warning on line 5: b used before set
warning on line 5: d used before set
warning on line 5: y set, never used
etc. using simple one pass approximate flow
analysis.
Imported modules
|
|
from pygram import newlineresult
|
Functions
|
|
|
|
BindRules
|
BindRules ( pyg )
first test
|
|
Class
|
Class (
name,
testlist,
suite,
context,
)
|
|
all1
|
all1 ( list, context )
the highest level reduction!
all1 :: all >> file_input DEDENT
|
|
analyse_scope
|
analyse_scope (
sname,
var_accesses,
context,
unused_ok=0,
)
|
|
aname
|
aname ( list, context )
|
|
arg2
|
arg2 ( l, c )
@R arg2 :: arg >> fpdef
|
|
arg3
|
arg3 ( l, c )
@R arg3 :: arg >> * NAME
|
|
arg4
|
arg4 ( l, c )
@R arg4 :: arg >> ** NAME
|
|
argd
|
argd ( l, c )
@R argd :: arg >> NAME = test
|
|
assn
|
assn ( left, right )
|
|
assn1
|
assn1 ( list, context )
|
|
assn1c
|
assn1c ( list, context )
@R assn1c :: assn >> testlist , = testlist
|
|
assn1c2
|
assn1c2 ( list, context )
@R assn1c2 :: assn >> testlist , = testlist ,
|
|
assnn
|
assnn ( list, context )
|
|
assnnc
|
assnnc ( list, context )
@R assnnc :: assn >> testlist , = assn
|
|
class1
|
class1 ( list, context )
@R class1 :: classdef >> class NAME : suite
|
|
class2
|
class2 ( list, context )
@R class2 :: classdef >> class NAME ( testlist ) : suite
|
|
default_reduction
|
default_reduction ( list, context )
|
|
dn1
|
dn1 ( list, context )
|
|
except2
|
except2 ( list, context )
@R except2 :: except_clause >> except test , test
|
|
for1
|
for1 ( list, context )
|
|
for2
|
for2 ( list, context )
|
|
fpdef1
|
fpdef1 ( l, c )
@R fpdef1 :: fpdef >> NAME
|
|
fpdef2
|
fpdef2 ( l, c )
@R fpdef2 :: fpdef >> ( fplist )
|
|
fplist1
|
fplist1 ( l, c )
31
@R fplist1 :: fplist >> fpdef
|
|
global1
|
global1 ( list, context )
|
|
globaln
|
globaln ( list, context )
|
|
go
|
go ()
|
|
lint
|
lint (
data,
pygin=None,
contextin=None,
)
|
|
lintdir
|
lintdir ( directory_name )
lint all files recursively in directory
|
|
mark
|
mark (
kind,
thing,
context,
)
|
|
namearg
|
namearg ( list, context )
ignore lhs in calls with keywords.
@R namearg :: argument >> test = test
|
|
nlist1
|
nlist1 ( list, context )
|
|
nlistn
|
nlistn ( list, context )
|
|
params1
|
params1 ( l, c )
vararsglist requires special treatment.
return (innerscope, outerscope) pair of lists
@R params1 :: parameters >> ( varargslist )
|
|
params2
|
params2 ( l, c )
@R params2 :: varargslist >>
|
|
params3
|
params3 ( l, c )
@R params3 :: varargslist >> arg
|
|
params4
|
params4 ( l, c )
@R params4 :: varargslist >> varargslist , arg
|
|
powera
|
powera ( list, context )
|
|
rdef
|
rdef ( list, context )
@R rdef :: funcdef >> def NAME parameters : suite
|
|
rfrom
|
rfrom ( list, context )
|
|
rfromc
|
rfromc ( list, context )
|
|
setup
|
setup ()
|
|
testlambda1
|
testlambda1 ( list, context )
@R testlambda1 :: test >> lambda varargslist : test
|
|
trailerlist0
|
trailerlist0 ( list, context )
@R trailerlist0 :: trailerlist >>
|
|
trailerlistn
|
trailerlistn ( list, context )
@R trailerlistn :: trailerlist >> trailer trailerlist
|
Classes
|
|
|
|