Skip to content

Commit e2639aa

Browse files
author
Dylan Trotter
committed
Add pylint action to make and include it in precommit. Clean up pylint warnings.
1 parent afaf300 commit e2639aa

File tree

7 files changed

+65
-23
lines changed

7 files changed

+65
-23
lines changed

.pylintrc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[BASIC]
2+
argument-rgx=^[a-z][a-z0-9_]*$
3+
attr-rgx=^_{0,2}[a-z][a-z0-9_]*$
4+
const-rgx=^(_?[A-Z][A-Z0-9_]*|__[a-z0-9_]+__|_?[a-z][a-z0-9_]*)$
5+
docstring-min-length=10
6+
function-rgx=^(?:(?P<camel_case>_?[A-Z][a-zA-Z0-9]*)|(?P<snake_case>_?[a-z][a-z0-9_]*))$
7+
good-names=main,_
8+
method-rgx=^(?:(?P<exempt>__[a-z0-9_]+__|next|test[A-Z_][A-Za-z0-9_]*)|(?P<camel_case>_{0,2}[A-Z][a-zA-Z0-9]*)|(?P<snake_case>_{0,2}[a-z][a-z0-9_]*))$
9+
no-docstring-rgx=(__.*__|main|test[A-Z_][A-Za-z0-9_]*|[A-Z][A-Za-z0-9]*Test)
10+
variable-rgx=^[a-z][a-z0-9_]*$
11+
12+
[FORMAT]
13+
indent-string=' '
14+
max-line-length=80
15+
16+
[MESSAGES CONTROL]
17+
# TODO: Remove cyclic-import once expr_visitor.py -> stmt.py is resolved.
18+
disable=design,similarities,no-self-use,attribute-defined-outside-init,locally-disabled,star-args,pointless-except,bad-option-value,global-statement,fixme,suppressed-message,useless-suppression,locally-enabled,file-ignored,cyclic-import
19+
20+
[REPORTS]
21+
msg-template={path}:{line}: {msg} ({symbol})
22+
reports=no

Makefile

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ GOOS ?= $(word 1,$(GO_ENV))
2121
GOARCH ?= $(word 2,$(GO_ENV))
2222
ROOT_DIR := $(realpath .)
2323
PKG_DIR := build/pkg/$(GOOS)_$(GOARCH)
24-
PY_DIR := build/site-packages
24+
PY_DIR := build/lib/python2.7/site-packages
2525

2626
export GOPATH := $(ROOT_DIR)/build
2727
export PYTHONPATH := $(ROOT_DIR)/$(PY_DIR)
@@ -31,10 +31,10 @@ COMPILER_BIN := build/bin/grumpc
3131
COMPILER_SRCS := $(addprefix $(PY_DIR)/grumpy/compiler/,$(notdir $(shell find compiler -name '*.py' -not -name '*_test.py'))) $(PY_DIR)/grumpy/__init__.py
3232
COMPILER_TESTS := $(patsubst %.py,grumpy/%,$(filter-out compiler/expr_visitor_test.py compiler/stmt_test.py,$(wildcard compiler/*_test.py)))
3333
COMPILER_TEST_SRCS := $(patsubst %,$(PY_DIR)/%.py,$(COMPILER_TESTS))
34-
COMPILER_SHARDED_TEST_SRCS := $(patsubst %,build/site-packages/grumpy/compiler/%,expr_visitor_test.py stmt_test.py)
34+
COMPILER_SHARDED_TEST_SRCS := $(patsubst %,$(PY_DIR)/grumpy/compiler/%,expr_visitor_test.py stmt_test.py)
3535
COMPILER_PASS_FILES := $(patsubst %,$(PY_DIR)/%.pass,$(COMPILER_TESTS))
36-
COMPILER_EXPR_VISITOR_PASS_FILES := $(patsubst %,build/site-packages/grumpy/compiler/expr_visitor_test.%of32.pass,$(shell seq 32))
37-
COMPILER_STMT_PASS_FILES := $(patsubst %,build/site-packages/grumpy/compiler/stmt_test.%of16.pass,$(shell seq 16))
36+
COMPILER_EXPR_VISITOR_PASS_FILES := $(patsubst %,$(PY_DIR)/grumpy/compiler/expr_visitor_test.%of32.pass,$(shell seq 32))
37+
COMPILER_STMT_PASS_FILES := $(patsubst %,$(PY_DIR)/grumpy/compiler/stmt_test.%of16.pass,$(shell seq 16))
3838
COMPILER_D_FILES := $(patsubst %,$(PY_DIR)/%.d,$(COMPILER_TESTS))
3939
COMPILER := $(COMPILER_BIN) $(COMPILER_SRCS)
4040

@@ -64,6 +64,7 @@ BENCHMARK_BINS := $(patsubst %,build/%_benchmark,$(BENCHMARKS))
6464
TOOL_BINS = $(patsubst %,build/bin/%,benchcmp coverparse diffrange)
6565

6666
GOLINT_BIN = build/bin/golint
67+
PYLINT_BIN = build/bin/pylint
6768

6869
all: $(COMPILER) $(RUNTIME) $(STDLIB) $(TOOL_BINS)
6970

@@ -80,7 +81,7 @@ test: $(ACCEPT_PASS_FILES) $(COMPILER_PASS_FILES) $(COMPILER_EXPR_VISITOR_PASS_F
8081

8182
precommit: cover lint test
8283

83-
.PHONY: all benchmarks clean cover lint precommit run test
84+
.PHONY: all benchmarks clean cover golint lint precommit pylint run test
8485

8586
# ------------------------------------------------------------------------------
8687
# grumpc compiler
@@ -105,13 +106,13 @@ $(COMPILER_D_FILES): $(PY_DIR)/%.d: $(PY_DIR)/%.py $(COMPILER_SRCS)
105106
-include $(COMPILER_D_FILES)
106107

107108
# Does not depend on stdlibs since it makes minimal use of them.
108-
$(COMPILER_EXPR_VISITOR_PASS_FILES): build/site-packages/grumpy/compiler/expr_visitor_test.%.pass: build/site-packages/grumpy/compiler/expr_visitor_test.py $(RUNNER_BIN) $(COMPILER) $(RUNTIME)
109+
$(COMPILER_EXPR_VISITOR_PASS_FILES): $(PY_DIR)/grumpy/compiler/expr_visitor_test.%.pass: $(PY_DIR)/grumpy/compiler/expr_visitor_test.py $(RUNNER_BIN) $(COMPILER) $(RUNTIME)
109110
@python $< --shard=$*
110111
@touch $@
111112
@echo 'compiler/expr_visitor_test $* PASS'
112113

113114
# Does not depend on stdlibs since it makes minimal use of them.
114-
$(COMPILER_STMT_PASS_FILES): build/site-packages/grumpy/compiler/stmt_test.%.pass: build/site-packages/grumpy/compiler/stmt_test.py $(RUNNER_BIN) $(COMPILER) $(RUNTIME)
115+
$(COMPILER_STMT_PASS_FILES): $(PY_DIR)/grumpy/compiler/stmt_test.%.pass: $(PY_DIR)/grumpy/compiler/stmt_test.py $(RUNNER_BIN) $(COMPILER) $(RUNTIME)
115116
@python $< --shard=$*
116117
@touch $@
117118
@echo 'compiler/stmt_test $* PASS'
@@ -143,9 +144,17 @@ cover: $(RUNTIME_COVER_FILE) $(TOOL_BINS)
143144
$(GOLINT_BIN):
144145
@go get -u github.com/golang/lint/golint
145146

146-
lint: $(GOLINT_BIN)
147+
golint: $(GOLINT_BIN) $(PYLINT_BIN)
147148
@$(GOLINT_BIN) -set_exit_status runtime
148149

150+
$(PYLINT_BIN):
151+
@pip install --prefix=$(ROOT_DIR)/build pylint
152+
153+
pylint: $(PYLINT_BIN)
154+
@$(PYLINT_BIN) compiler/*.py tools/{benchcmp,coverparse,diffrange,grumpc,grumprun}
155+
156+
lint: golint pylint
157+
149158
# ------------------------------------------------------------------------------
150159
# Standard library
151160
# ------------------------------------------------------------------------------

compiler/block.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ class Block(object):
5353

5454
__metaclass__ = abc.ABCMeta
5555

56+
# These are ModuleBlock attributes. Avoid pylint errors for accessing them on
57+
# Block objects by defining them here.
58+
_filename = None
59+
_full_package_name = None
60+
_libroot = None
61+
_lines = None
62+
_runtime = None
63+
_strings = None
64+
imports = None
65+
5666
def __init__(self, parent_block, name):
5767
self.parent_block = parent_block
5868
self.name = name

compiler/expr_visitor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def visit_GeneratorExp(self, node):
202202
body = ast.Expr(value=ast.Yield(node.elt), lineno=None)
203203
for comp_node in reversed(node.generators):
204204
for if_node in reversed(comp_node.ifs):
205-
body = ast.If(test=if_node, body=[body], orelse=[], lineno=None)
205+
body = ast.If(test=if_node, body=[body], orelse=[], lineno=None) # pylint: disable=redefined-variable-type
206206
body = ast.For(target=comp_node.target, iter=comp_node.iter,
207207
body=[body], orelse=[], lineno=None)
208208

compiler/expr_visitor_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def Test(self):
4343

4444

4545
def _MakeSliceTest(subscript, want):
46+
"""Define a test function that evaluates a slice expression."""
4647
def Test(self):
4748
code = textwrap.dedent("""\
4849
class Slicer(object):

compiler/stmt_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ def testWriteExceptDispatcherBareExcept(self):
423423
visitor = stmt.StatementVisitor(_MakeModuleBlock())
424424
handlers = [ast.ExceptHandler(type=ast.Name(id='foo')),
425425
ast.ExceptHandler(type=None)]
426-
self.assertEqual(visitor._write_except_dispatcher(
426+
self.assertEqual(visitor._write_except_dispatcher( # pylint: disable=protected-access
427427
'exc', 'tb', handlers), [1, 2])
428428
expected = re.compile(r'ResolveGlobal\(.*foo.*\bIsInstance\(.*'
429429
r'goto Label1.*goto Label2', re.DOTALL)
@@ -434,14 +434,14 @@ def testWriteExceptDispatcherBareExceptionNotLast(self):
434434
handlers = [ast.ExceptHandler(type=None),
435435
ast.ExceptHandler(type=ast.Name(id='foo'))]
436436
self.assertRaisesRegexp(util.ParseError, r"default 'except:' must be last",
437-
visitor._write_except_dispatcher,
437+
visitor._write_except_dispatcher, # pylint: disable=protected-access
438438
'exc', 'tb', handlers)
439439

440440
def testWriteExceptDispatcherMultipleExcept(self):
441441
visitor = stmt.StatementVisitor(_MakeModuleBlock())
442442
handlers = [ast.ExceptHandler(type=ast.Name(id='foo')),
443443
ast.ExceptHandler(type=ast.Name(id='bar'))]
444-
self.assertEqual(visitor._write_except_dispatcher(
444+
self.assertEqual(visitor._write_except_dispatcher( # pylint: disable=protected-access
445445
'exc', 'tb', handlers), [1, 2])
446446
expected = re.compile(
447447
r'ResolveGlobal\(.*foo.*\bif .*\bIsInstance\(.*\{.*goto Label1.*'

tools/diffrange

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,28 @@ class _LineBuffer(object):
2323
"""Iterator over lines in a file supporting one-step rewind."""
2424

2525
def __init__(self, f):
26-
self.f = f
27-
self.prev = None
28-
self.next = None
26+
self._f = f
27+
self._prev = None
28+
self._next = None
2929

3030
def __iter__(self):
3131
return self
3232

3333
def next(self):
34-
if self.next is not None:
35-
cur = self.next
34+
if self._next is not None:
35+
cur = self._next
3636
else:
37-
cur = self.f.readline()
37+
cur = self._f.readline()
3838
if not cur:
3939
raise StopIteration
40-
self.next = None
41-
self.prev = cur
40+
self._next = None
41+
self._prev = cur
4242
return cur
4343

4444
def Rewind(self):
45-
assert self.prev is not None
46-
self.next = self.prev
47-
self.prev = None
45+
assert self._prev is not None
46+
self._next = self._prev
47+
self._prev = None
4848

4949

5050
def _ReadHunks(buf):

0 commit comments

Comments
 (0)