Skip to content

Commit 1c03669

Browse files
author
John Vandenberg
committed
Miscellaneous linting changes and enable pylint
1 parent a3022dc commit 1c03669

30 files changed

+122
-85
lines changed

.pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ ignore=tests
33

44
[MESSAGES CONTROL]
55
# messages up to fixme should probably be fixed somehow
6-
disable = redefined-builtin,attribute-defined-outside-init,anomalous-backslash-in-string,no-self-use,redefined-outer-name,bad-continuation,wrong-import-order,superfluous-parens,no-member,duplicate-code,super-init-not-called,abstract-method,property-on-old-class,wrong-import-position,no-name-in-module,no-init,bad-mcs-classmethod-argument,bad-classmethod-argument,fixme,invalid-name,import-error,too-few-public-methods,too-many-ancestors,too-many-arguments,too-many-boolean-expressions,too-many-branches,too-many-instance-attributes,too-many-locals,too-many-lines,too-many-public-methods,too-many-return-statements,too-many-statements,missing-docstring,line-too-long,locally-disabled,locally-enabled,bad-builtin,deprecated-lambda
6+
disable = redefined-builtin,attribute-defined-outside-init,anomalous-backslash-in-string,no-self-use,redefined-outer-name,bad-continuation,wrong-import-order,superfluous-parens,no-member,duplicate-code,super-init-not-called,abstract-method,property-on-old-class,wrong-import-position,no-name-in-module,no-init,bad-mcs-classmethod-argument,bad-classmethod-argument,fixme,invalid-name,import-error,too-few-public-methods,too-many-ancestors,too-many-arguments,too-many-boolean-expressions,too-many-branches,too-many-instance-attributes,too-many-locals,too-many-lines,too-many-public-methods,too-many-return-statements,too-many-statements,missing-docstring,line-too-long,locally-disabled,locally-enabled,bad-builtin,deprecated-lambda,bad-option-value,star-args,abstract-class-little-used,abstract-class-not-used
77

88
[FORMAT]
99
max-line-length=139

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ script:
2727
- if [[ $TRAVIS_PYTHON_VERSION == pypy* ]]; then py.test; fi
2828
- if [[ $TRAVIS_PYTHON_VERSION != pypy* ]]; then coverage run -m pytest; fi
2929
- bash flake8-run.sh
30+
- pylint --rcfile=.pylintrc html5lib
3031

3132
after_script:
3233
- python debug-info.py

debug-info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from __future__ import print_function, unicode_literals
1+
from __future__ import absolute_import, division, print_function, unicode_literals
22

33
import platform
44
import sys

flake8-run.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ if [[ ! -x $(which flake8) ]]; then
55
exit 1
66
fi
77

8+
flake8 --version
89
flake8 `dirname $0`
910
exit $?

html5lib/_inputstream.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,9 @@ def position(self):
238238
return (line + 1, col)
239239

240240
def char(self):
241-
""" Read one character from the stream or queue if available. Return
242-
EOF when EOF is reached.
241+
"""Read one character from the stream or queue if available.
242+
243+
Return EOF when EOF is reached.
243244
"""
244245
# Read a new chunk from the input stream if necessary
245246
if self.chunkOffset >= self.chunkSize:
@@ -318,7 +319,7 @@ def characterErrorsUCS2(self, data):
318319
self.errors.append("invalid-codepoint")
319320

320321
def charsUntil(self, characters, opposite=False):
321-
""" Returns a string of characters from the stream up to but not
322+
"""Returns a string of characters from the stream up to but not
322323
including any character in 'characters' or EOF. 'characters' must be
323324
a container that supports the 'in' method and iteration over its
324325
characters.
@@ -330,7 +331,7 @@ def charsUntil(self, characters, opposite=False):
330331
except KeyError:
331332
if __debug__:
332333
for c in characters:
333-
assert(ord(c) < 128)
334+
assert ord(c) < 128
334335
regex = "".join(["\\x%02x" % ord(c) for c in characters])
335336
if not opposite:
336337
regex = "^%s" % regex
@@ -449,7 +450,7 @@ def openStream(self, source):
449450

450451
try:
451452
stream.seek(stream.tell())
452-
except: # pylint:disable=bare-except
453+
except Exception: # pylint: disable=broad-except
453454
stream = BufferedStream(stream)
454455

455456
return stream
@@ -567,8 +568,7 @@ def detectBOM(self):
567568
return None
568569

569570
def detectEncodingMeta(self):
570-
"""Report the encoding declared by the meta element
571-
"""
571+
"""Report the encoding declared by the meta element."""
572572
buffer = self.rawStream.read(self.numBytesMeta)
573573
assert isinstance(buffer, bytes)
574574
parser = EncodingParser(buffer)
@@ -686,10 +686,12 @@ def jumpTo(self, bytes):
686686

687687

688688
class EncodingParser(object):
689-
"""Mini parser for detecting character encoding from meta elements"""
689+
"""Mini parser for detecting character encoding from meta elements."""
690690

691691
def __init__(self, data):
692-
"""string - the data to work on for encoding detection"""
692+
"""Constructor.
693+
694+
data - the data to work on for encoding detection"""
693695
self.data = EncodingBytes(data)
694696
self.encoding = None
695697

html5lib/_tokenizer.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020

2121
class HTMLTokenizer(object):
22-
""" This class takes care of tokenizing HTML.
22+
"""This class takes care of tokenizing HTML.
2323
2424
* self.currentToken
2525
Holds the token that is currently being processed.
@@ -47,7 +47,7 @@ def __init__(self, stream, parser=None, **kwargs):
4747
super(HTMLTokenizer, self).__init__()
4848

4949
def __iter__(self):
50-
""" This is where the magic happens.
50+
"""This is where the magic happens.
5151
5252
We do our usually processing through the states and when we have a token
5353
to return we yield the token which pauses processing until the next token
@@ -215,8 +215,7 @@ def consumeEntity(self, allowedChar=None, fromAttribute=False):
215215
self.tokenQueue.append({"type": tokenTypes[tokenType], "data": output})
216216

217217
def processEntityInAttribute(self, allowedChar):
218-
"""This method replaces the need for "entityInAttributeValueState".
219-
"""
218+
"""This method replaces the need for "entityInAttributeValueState"."""
220219
self.consumeEntity(allowedChar=allowedChar, fromAttribute=True)
221220

222221
def emitCurrentToken(self):
@@ -1686,8 +1685,7 @@ def bogusDoctypeState(self):
16861685
self.stream.unget(data)
16871686
self.tokenQueue.append(self.currentToken)
16881687
self.state = self.dataState
1689-
else:
1690-
pass
1688+
16911689
return True
16921690

16931691
def cdataSectionState(self):

html5lib/_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@
3030
# We need this with u"" because of http://bugs.jython.org/issue2039
3131
_x = eval('u"\\uD800"') # pylint:disable=eval-used
3232
assert isinstance(_x, text_type)
33-
except: # pylint:disable=bare-except
33+
except Exception: # pylint: disable=broad-except
3434
supports_lone_surrogates = False
3535
else:
3636
supports_lone_surrogates = True
3737

3838

3939
class MethodDispatcher(dict):
40-
"""Dict with 2 special properties:
40+
"""Dict with 2 special properties.
4141
4242
On initiation, keys that are lists, sets or tuples are converted to
4343
multiple keys so accessing any one of the items in the original

html5lib/filters/sanitizer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@
705705

706706

707707
class Filter(base.Filter):
708-
""" sanitization of XHTML+MathML+SVG and of inline style attributes."""
708+
"""Sanitization of XHTML+MathML+SVG and of inline style attributes."""
709709
def __init__(self,
710710
source,
711711
allowed_elements=allowed_elements,

html5lib/html5parser.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ def __new__(meta, classname, bases, classDict):
5555

5656
class HTMLParser(object):
5757
"""HTML parser. Generates a tree structure from a stream of (possibly
58-
malformed) HTML"""
58+
malformed) HTML"""
5959

6060
def __init__(self, tree=None, strict=False, namespaceHTMLElements=True, debug=False):
61-
"""
61+
"""Constructor.
62+
6263
strict - raise an exception when a parse error is encountered
6364
6465
tree - a treebuilder class controlling the type of tree that will be
@@ -108,10 +109,9 @@ def reset(self):
108109
self.tokenizer.state = self.tokenizer.rawtextState
109110
elif self.innerHTML == 'plaintext':
110111
self.tokenizer.state = self.tokenizer.plaintextState
111-
else:
112-
# state already is data state
113-
# self.tokenizer.state = self.tokenizer.dataState
114-
pass
112+
# else state already is data state
113+
# i.e. self.tokenizer.state = self.tokenizer.dataState
114+
115115
self.phase = self.phases["beforeHtml"]
116116
self.phase.insertHtmlElement()
117117
self.resetInsertionMode()
@@ -262,7 +262,7 @@ def parseError(self, errorcode="XXX-undefined-error", datavars=None):
262262
raise ParseError(E[errorcode] % datavars)
263263

264264
def normalizeToken(self, token):
265-
""" HTML5 specific normalizations to the token stream """
265+
"""HTML5 specific normalizations to the token stream."""
266266

267267
if token["type"] == tokenTypes["StartTag"]:
268268
raw = token["data"]
@@ -358,10 +358,7 @@ def log(function):
358358
def wrapped(self, *args, **kwargs):
359359
if function.__name__.startswith("process") and len(args) > 0:
360360
token = args[0]
361-
try:
362-
info = {"type": type_names[token['type']]}
363-
except:
364-
raise
361+
info = {"type": type_names[token['type']]}
365362
if token['type'] in tagTokenTypes:
366363
info["name"] = token['name']
367364

@@ -383,8 +380,7 @@ def getMetaclass(use_metaclass, metaclass_func):
383380

384381
# pylint:disable=unused-argument
385382
class Phase(with_metaclass(getMetaclass(debug, log))):
386-
"""Base class for helper object that implements each phase of processing
387-
"""
383+
"""Base class for helper object that implements each phase of processing."""
388384

389385
def __init__(self, parser, tree):
390386
self.parser = parser
@@ -1285,7 +1281,7 @@ def startTagSvg(self, token):
12851281
token["selfClosingAcknowledged"] = True
12861282

12871283
def startTagMisplaced(self, token):
1288-
""" Elements that should be children of other elements that have a
1284+
"""Elements that should be children of other elements that have a
12891285
different insertion mode; here they are ignored
12901286
"caption", "col", "colgroup", "frame", "frameset", "head",
12911287
"option", "optgroup", "tbody", "td", "tfoot", "th", "thead",
@@ -2730,4 +2726,3 @@ def impliedTagToken(name, type="EndTag", attributes=None,
27302726

27312727
class ParseError(Exception):
27322728
"""Error in parsed document"""
2733-
pass

html5lib/serializer.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,14 @@ def __init__(self, **kwargs):
166166
self.strict = False
167167

168168
def encode(self, string):
169-
assert(isinstance(string, text_type))
169+
assert isinstance(string, text_type)
170170
if self.encoding:
171171
return string.encode(self.encoding, "htmlentityreplace")
172172
else:
173173
return string
174174

175175
def encodeStrict(self, string):
176-
assert(isinstance(string, text_type))
176+
assert isinstance(string, text_type)
177177
if self.encoding:
178178
return string.encode(self.encoding, "strict")
179179
else:
@@ -331,4 +331,3 @@ def serializeError(self, data="XXX ERROR MESSAGE NEEDED"):
331331

332332
class SerializeError(Exception):
333333
"""Error in serialized tree"""
334-
pass

html5lib/tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import absolute_import, division, unicode_literals
2+
13
import os.path
24

35
import pkg_resources

html5lib/tests/support.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
}
5050

5151
try:
52-
import lxml.etree as lxml # noqa
52+
import lxml.etree as lxml
53+
del lxml
5354
except ImportError:
5455
treeTypes['lxml'] = None
5556
else:
@@ -60,7 +61,8 @@
6061

6162
# Genshi impls
6263
try:
63-
import genshi # noqa
64+
import genshi
65+
del genshi
6466
except ImportError:
6567
treeTypes["genshi"] = None
6668
else:
@@ -132,7 +134,7 @@ def normaliseOutput(self, data):
132134

133135
def convert(stripChars):
134136
def convertData(data):
135-
"""convert the output of str(document) to the format used in the testcases"""
137+
"""Convert the output of str(document) to the format used in the testcases"""
136138
data = data.split("\n")
137139
rv = []
138140
for line in data:

html5lib/tests/test_encoding.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from __future__ import absolute_import, division, unicode_literals
1+
from __future__ import absolute_import, division, print_function, unicode_literals
22

33
import os
44

@@ -105,7 +105,8 @@ def test_encoding():
105105

106106
# pylint:disable=wrong-import-position
107107
try:
108-
import chardet # noqa
108+
import chardet
109+
del chardet
109110
except ImportError:
110111
print("chardet not found, skipping chardet tests")
111112
else:

html5lib/tests/test_parser2.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
import io
66

7-
from . import support # noqa
7+
from . import support
8+
89

910
from html5lib.constants import namespaces, tokenTypes
1011
from html5lib import parse, parseFragment, HTMLParser
1112

13+
# above import has side-effects; mark it as used and del it
14+
del support
15+
1216

1317
# tests that aren't autogenerated from text files
1418
def test_assertDoctypeCloneable():

html5lib/tests/test_serializer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def _convertAttrib(self, attribs):
7474
attrs = {}
7575
for attrib in attribs:
7676
name = (attrib["namespace"], attrib["name"])
77-
assert(name not in attrs)
77+
assert name not in attrs
7878
attrs[name] = attrib["value"]
7979
return attrs
8080

@@ -93,7 +93,7 @@ def runSerializerTest(input, expected, options):
9393
encoding = options.get("encoding", None)
9494

9595
if encoding:
96-
expected = list(map(lambda x: x.encode(encoding), expected))
96+
expected = list(x.encode(encoding) for x in expected)
9797

9898
result = serialize_html(input, options)
9999
if len(expected) == 1:

html5lib/tests/test_stream.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from __future__ import absolute_import, division, unicode_literals
22

3-
from . import support # noqa
4-
53
import codecs
64
import sys
75
from io import BytesIO, StringIO
@@ -11,10 +9,15 @@
119
import six
1210
from six.moves import http_client, urllib
1311

12+
from . import support
13+
1414
from html5lib._inputstream import (BufferedStream, HTMLInputStream,
1515
HTMLUnicodeInputStream, HTMLBinaryInputStream)
1616
from html5lib._utils import supports_lone_surrogates
1717

18+
# above import has side-effects; mark it as used and del it
19+
del support
20+
1821

1922
def test_basic():
2023
s = b"abc"
@@ -182,8 +185,8 @@ def test_position2():
182185

183186

184187
def test_python_issue_20007():
185-
"""
186-
Make sure we have a work-around for Python bug #20007
188+
"""Ensure we have a work-around for Python bug #20007.
189+
187190
http://bugs.python.org/issue20007
188191
"""
189192
class FakeSocket(object):
@@ -198,8 +201,8 @@ def makefile(self, _mode, _bufsize=None):
198201

199202

200203
def test_python_issue_20007_b():
201-
"""
202-
Make sure we have a work-around for Python bug #20007
204+
"""Ensure we have a work-around for Python bug #20007 (part b).
205+
203206
http://bugs.python.org/issue20007
204207
"""
205208
if six.PY2:

0 commit comments

Comments
 (0)