Skip to content
This repository was archived by the owner on Nov 3, 2023. It is now read-only.

Commit 876d752

Browse files
authored
Merge pull request #243 from Nurdok/feature/parser-tests-from-jedi-branch
Backport parser tests and some changes from Jedi branch
2 parents 22e6b88 + b3a241e commit 876d752

File tree

8 files changed

+343
-279
lines changed

8 files changed

+343
-279
lines changed

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
package_data={'pydocstyle': ['data/*.txt']},
2929
install_requires=[
3030
'snowballstemmer',
31+
'six',
3132
],
3233
entry_points={
3334
'console_scripts': [

src/pydocstyle/checker.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
from . import violations
1212
from .config import IllegalConfiguration
1313
from .parser import (Package, Module, Class, NestedClass, Definition, AllError,
14-
Method, Function, NestedFunction, Parser, StringIO)
14+
Method, Function, NestedFunction, Parser, StringIO,
15+
ParseError)
1516
from .utils import log, is_blank, pairwise
1617
from .wordlists import IMPERATIVE_VERBS, IMPERATIVE_BLACKLIST, stem
1718

@@ -331,7 +332,7 @@ def check_unicode_docstring(self, definition, docstring):
331332
For Unicode docstrings, use u"""Unicode triple-quoted strings""".
332333
333334
'''
334-
if definition.module.future_imports['unicode_literals']:
335+
if 'unicode_literals' in definition.module.future_imports:
335336
return
336337

337338
# Just check that docstring is unicode, check_triple_double_quotes
@@ -695,8 +696,9 @@ def check(filenames, select=None, ignore=None, ignore_decorators=None):
695696
code = getattr(error, 'code', None)
696697
if code in checked_codes:
697698
yield error
698-
except (EnvironmentError, AllError):
699-
yield sys.exc_info()[1]
699+
except (EnvironmentError, AllError, ParseError) as error:
700+
log.warning('Error in file %s: %s', filename, error)
701+
yield error
700702
except tk.TokenError:
701703
yield SyntaxError('invalid syntax in file %s' % filename)
702704

src/pydocstyle/cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ def run_pydocstyle():
4949

5050
count = 0
5151
for error in errors:
52-
sys.stdout.write('%s\n' % error)
52+
if hasattr(error, 'code'):
53+
sys.stdout.write('%s\n' % error)
5354
count += 1
5455
if count == 0:
5556
exit_code = ReturnCode.no_violations_found

src/pydocstyle/parser.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
"""Python code parser."""
22

33
import logging
4-
import sys
4+
import six
55
import textwrap
66
import tokenize as tk
7-
from collections import defaultdict
87
from itertools import chain, dropwhile
98
from re import compile as re
109

@@ -30,7 +29,12 @@ def next(obj, default=nothing):
3029

3130
__all__ = ('Parser', 'Definition', 'Module', 'Package', 'Function',
3231
'NestedFunction', 'Method', 'Class', 'NestedClass', 'AllError',
33-
'StringIO')
32+
'StringIO', 'ParseError')
33+
34+
35+
class ParseError(Exception):
36+
def __str__(self):
37+
return "Cannot parse file."
3438

3539

3640
def humanize(string):
@@ -270,10 +274,14 @@ def parse(self, filelike, filename):
270274
self.log = logging.getLogger()
271275
self.source = filelike.readlines()
272276
src = ''.join(self.source)
277+
try:
278+
compile(src, filename, 'exec')
279+
except SyntaxError as error:
280+
six.raise_from(ParseError(), error)
273281
self.stream = TokenStream(StringIO(src))
274282
self.filename = filename
275283
self.all = None
276-
self.future_imports = defaultdict(lambda: False)
284+
self.future_imports = set()
277285
self._accumulated_decorators = []
278286
return self.parse_module()
279287

@@ -557,7 +565,7 @@ def _parse_from_import_names(self, is_future_import):
557565
self.current.kind, self.current.value)
558566
if is_future_import:
559567
self.log.debug('found future import: %s', self.current.value)
560-
self.future_imports[self.current.value] = True
568+
self.future_imports.add(self.current.value)
561569
self.consume(tk.NAME)
562570
self.log.debug("parsing import, token is %r (%s)",
563571
self.current.kind, self.current.value)

0 commit comments

Comments
 (0)