Skip to content

Commit

Permalink
Merge 116a1fb into 96f4b1f
Browse files Browse the repository at this point in the history
  • Loading branch information
edaniszewski committed Dec 11, 2018
2 parents 96f4b1f + 116a1fb commit bca2a75
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
40 changes: 24 additions & 16 deletions pylint_quotes/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,19 +251,20 @@ def process_tokens(self, tokens):
Args:
tokens: the tokens from the token stream to process.
"""
for tok_type, token, (start_row, _), _, _ in tokens:
for tok_type, token, (start_row, start_col), _, _ in tokens:
if tok_type == tokenize.STRING:
# 'token' is the whole un-parsed token; we can look at the start
# of it to see whether it's a raw or unicode string etc.
self._process_string_token(token, start_row)
self._process_string_token(token, start_row, start_col)

def _process_string_token(self, token, start_row):
def _process_string_token(self, token, start_row, start_col):
"""Internal method for identifying and checking string tokens
from the token stream.
Args:
token: the token to check.
start_row: the line on which the token was found.
start_col: the column on which the token was found.
"""
for i, char in enumerate(token):
if char in QUOTES:
Expand All @@ -275,7 +276,7 @@ def _process_string_token(self, token, start_row):

# triple-quote strings
if len(norm_quote) >= 3 and norm_quote[:3] in TRIPLE_QUOTE_OPTS.values():
self._tokenized_triple_quotes[start_row] = (token, norm_quote[:3], start_row)
self._tokenized_triple_quotes[start_row] = (token, norm_quote[:3], start_row, start_col)
return

# single quote strings
Expand All @@ -293,19 +294,20 @@ def _process_string_token(self, token, start_row):
self._invalid_string_quote(
quote=norm_quote[0],
row=start_row,
correct_quote=preferred_quote
correct_quote=preferred_quote,
col=start_col,
)

def _check_triple_quotes(self, quote_record):
"""Check if the triple quote from tokenization is valid.
Args:
quote_record: a tuple containing the info about the string
from tokenization, giving the (token, quote, row number).
from tokenization, giving the (token, quote, row number, column).
"""
_, triple, row = quote_record
_, triple, row, col = quote_record
if triple != TRIPLE_QUOTE_OPTS.get(self.config.triple_quote):
self._invalid_triple_quote(triple, row)
self._invalid_triple_quote(triple, row, col)

def _check_docstring_quotes(self, quote_record):
"""Check if the docstring quote from tokenization is valid.
Expand All @@ -314,50 +316,56 @@ def _check_docstring_quotes(self, quote_record):
quote_record: a tuple containing the info about the string
from tokenization, giving the (token, quote, row number).
"""
_, triple, row = quote_record
_, triple, row, col = quote_record
if triple != TRIPLE_QUOTE_OPTS.get(self.config.docstring_quote):
self._invalid_docstring_quote(triple, row)
self._invalid_docstring_quote(triple, row, col)

def _invalid_string_quote(self, quote, row, correct_quote=None):
def _invalid_string_quote(self, quote, row, correct_quote=None, col=None):
"""Add a message for an invalid string literal quote.
Args:
quote: The quote characters that were found.
row: The row number the quote character was found on.
correct_quote: The quote characters that is required. If None
(default), will use the one from the config.
col: The column the quote characters were found on.
"""
if not correct_quote:
correct_quote = SMART_QUOTE_OPTS.get(self.config.string_quote)

self.add_message(
'invalid-string-quote',
line=row,
args=(quote, correct_quote)
args=(quote, correct_quote),
col_offset=col,
)

def _invalid_triple_quote(self, quote, row):
def _invalid_triple_quote(self, quote, row, col=None):
"""Add a message for an invalid triple quote.
Args:
quote: The quote characters that were found.
row: The row number the quote characters were found on.
col: The column the quote characters were found on.
"""
self.add_message(
'invalid-triple-quote',
line=row,
args=(quote, TRIPLE_QUOTE_OPTS.get(self.config.triple_quote))
args=(quote, TRIPLE_QUOTE_OPTS.get(self.config.triple_quote)),
col_offset=col,
)

def _invalid_docstring_quote(self, quote, row):
def _invalid_docstring_quote(self, quote, row, col=None):
"""Add a message for an invalid docstring quote.
Args:
quote: The quote characters that were found.
row: The row number the quote characters were found on.
col: The column the quote characters were found on.
"""
self.add_message(
'invalid-docstring-quote',
line=row,
args=(quote, TRIPLE_QUOTE_OPTS.get(self.config.docstring_quote))
args=(quote, TRIPLE_QUOTE_OPTS.get(self.config.docstring_quote)),
col_offset=col,
)
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ commands=
description=
run pylint with pylint-quotes on the example
commands=
pip install -e .
pylint --load-plugins pylint_quotes example

0 comments on commit bca2a75

Please sign in to comment.