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

Commit

Permalink
Merge pull request #243 from Nurdok/feature/parser-tests-from-jedi-br…
Browse files Browse the repository at this point in the history
…anch

Backport parser tests and some changes from Jedi branch
  • Loading branch information
Nurdok committed Apr 3, 2017
2 parents 22e6b88 + b3a241e commit 876d752
Show file tree
Hide file tree
Showing 8 changed files with 343 additions and 279 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package_data={'pydocstyle': ['data/*.txt']},
install_requires=[
'snowballstemmer',
'six',
],
entry_points={
'console_scripts': [
Expand Down
10 changes: 6 additions & 4 deletions src/pydocstyle/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from . import violations
from .config import IllegalConfiguration
from .parser import (Package, Module, Class, NestedClass, Definition, AllError,
Method, Function, NestedFunction, Parser, StringIO)
Method, Function, NestedFunction, Parser, StringIO,
ParseError)
from .utils import log, is_blank, pairwise
from .wordlists import IMPERATIVE_VERBS, IMPERATIVE_BLACKLIST, stem

Expand Down Expand Up @@ -331,7 +332,7 @@ def check_unicode_docstring(self, definition, docstring):
For Unicode docstrings, use u"""Unicode triple-quoted strings""".
'''
if definition.module.future_imports['unicode_literals']:
if 'unicode_literals' in definition.module.future_imports:
return

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

Expand Down
3 changes: 2 additions & 1 deletion src/pydocstyle/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def run_pydocstyle():

count = 0
for error in errors:
sys.stdout.write('%s\n' % error)
if hasattr(error, 'code'):
sys.stdout.write('%s\n' % error)
count += 1
if count == 0:
exit_code = ReturnCode.no_violations_found
Expand Down
18 changes: 13 additions & 5 deletions src/pydocstyle/parser.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"""Python code parser."""

import logging
import sys
import six
import textwrap
import tokenize as tk
from collections import defaultdict
from itertools import chain, dropwhile
from re import compile as re

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

__all__ = ('Parser', 'Definition', 'Module', 'Package', 'Function',
'NestedFunction', 'Method', 'Class', 'NestedClass', 'AllError',
'StringIO')
'StringIO', 'ParseError')


class ParseError(Exception):
def __str__(self):
return "Cannot parse file."


def humanize(string):
Expand Down Expand Up @@ -270,10 +274,14 @@ def parse(self, filelike, filename):
self.log = logging.getLogger()
self.source = filelike.readlines()
src = ''.join(self.source)
try:
compile(src, filename, 'exec')
except SyntaxError as error:
six.raise_from(ParseError(), error)
self.stream = TokenStream(StringIO(src))
self.filename = filename
self.all = None
self.future_imports = defaultdict(lambda: False)
self.future_imports = set()
self._accumulated_decorators = []
return self.parse_module()

Expand Down Expand Up @@ -557,7 +565,7 @@ def _parse_from_import_names(self, is_future_import):
self.current.kind, self.current.value)
if is_future_import:
self.log.debug('found future import: %s', self.current.value)
self.future_imports[self.current.value] = True
self.future_imports.add(self.current.value)
self.consume(tk.NAME)
self.log.debug("parsing import, token is %r (%s)",
self.current.kind, self.current.value)
Expand Down

0 comments on commit 876d752

Please sign in to comment.