22
33import ast
44import string
5- import sys
6- import textwrap
75import tokenize as tk
86from collections import namedtuple
97from itertools import chain , takewhile
2826from .utils import (
2927 common_prefix_length ,
3028 is_blank ,
29+ is_fstring ,
3130 log ,
3231 pairwise ,
32+ safe_literal_eval ,
3333 strip_non_alphanumeric ,
3434)
3535from .wordlists import IMPERATIVE_BLACKLIST , IMPERATIVE_VERBS , stem
@@ -46,14 +46,6 @@ def decorator(f):
4646 return decorator
4747
4848
49- _FSTRING_REGEX = re (r'^[rR]?[fF]' )
50-
51-
52- def _is_fstring (docstring ):
53- """Return True if docstring is an f-string."""
54- return _FSTRING_REGEX .match (str (docstring ))
55-
56-
5749class ConventionChecker :
5850 """Checker for PEP 257, NumPy and Google conventions.
5951
@@ -201,7 +193,7 @@ def check_docstring_fstring(self, definition, docstring):
201193 and users may attempt to use them as docstrings. This is an
202194 outright mistake so we issue a specific error code.
203195 """
204- if _is_fstring (docstring ):
196+ if is_fstring (docstring ):
205197 return violations .D303 ()
206198
207199 @check_for (Definition , terminal = True )
@@ -222,7 +214,7 @@ def check_docstring_missing(self, definition, docstring):
222214 not docstring
223215 and definition .is_public
224216 or docstring
225- and is_blank (docstring , literal_eval = True )
217+ and is_blank (safe_literal_eval ( docstring ) )
226218 ):
227219 codes = {
228220 Module : violations .D100 ,
@@ -252,7 +244,7 @@ def check_one_liners(self, definition, docstring):
252244
253245 """
254246 if docstring :
255- lines = ast . literal_eval (docstring ).split ('\n ' )
247+ lines = safe_literal_eval (docstring ).split ('\n ' )
256248 if len (lines ) > 1 :
257249 non_empty_lines = sum (1 for l in lines if not is_blank (l ))
258250 if non_empty_lines == 1 :
@@ -328,7 +320,7 @@ def check_blank_after_summary(self, definition, docstring):
328320
329321 """
330322 if docstring :
331- lines = ast . literal_eval (docstring ).strip ().split ('\n ' )
323+ lines = safe_literal_eval (docstring ).strip ().split ('\n ' )
332324 if len (lines ) > 1 :
333325 post_summary_blanks = list (map (is_blank , lines [1 :]))
334326 blanks_count = sum (takewhile (bool , post_summary_blanks ))
@@ -381,7 +373,7 @@ def check_newline_after_last_paragraph(self, definition, docstring):
381373 if docstring :
382374 lines = [
383375 l
384- for l in ast . literal_eval (docstring ).split ('\n ' )
376+ for l in safe_literal_eval (docstring ).split ('\n ' )
385377 if not is_blank (l )
386378 ]
387379 if len (lines ) > 1 :
@@ -392,7 +384,7 @@ def check_newline_after_last_paragraph(self, definition, docstring):
392384 def check_surrounding_whitespaces (self , definition , docstring ):
393385 """D210: No whitespaces allowed surrounding docstring text."""
394386 if docstring :
395- lines = ast . literal_eval (docstring ).split ('\n ' )
387+ lines = safe_literal_eval (docstring ).split ('\n ' )
396388 if (
397389 lines [0 ].startswith (' ' )
398390 or len (lines ) == 1
@@ -420,7 +412,7 @@ def check_multi_line_summary_start(self, definition, docstring):
420412 "ur'''" ,
421413 ]
422414
423- lines = ast . literal_eval (docstring ).split ('\n ' )
415+ lines = safe_literal_eval (docstring ).split ('\n ' )
424416 if len (lines ) > 1 :
425417 first = docstring .split ("\n " )[0 ].strip ().lower ()
426418 if first in start_triple :
@@ -442,7 +434,7 @@ def check_triple_double_quotes(self, definition, docstring):
442434
443435 '''
444436 if docstring :
445- if '"""' in ast . literal_eval (docstring ):
437+ if '"""' in safe_literal_eval (docstring ):
446438 # Allow ''' quotes if docstring contains """, because
447439 # otherwise """ quotes could not be expressed inside
448440 # docstring. Not in PEP 257.
@@ -486,7 +478,7 @@ def _check_ends_with(docstring, chars, violation):
486478
487479 """
488480 if docstring :
489- summary_line = ast . literal_eval (docstring ).strip ().split ('\n ' )[0 ]
481+ summary_line = safe_literal_eval (docstring ).strip ().split ('\n ' )[0 ]
490482 if not summary_line .endswith (chars ):
491483 return violation (summary_line [- 1 ])
492484
@@ -521,7 +513,7 @@ def check_imperative_mood(self, function, docstring): # def context
521513
522514 """
523515 if docstring and not function .is_test :
524- stripped = ast . literal_eval (docstring ).strip ()
516+ stripped = safe_literal_eval (docstring ).strip ()
525517 if stripped :
526518 first_word = strip_non_alphanumeric (stripped .split ()[0 ])
527519 check_word = first_word .lower ()
@@ -547,7 +539,7 @@ def check_no_signature(self, function, docstring): # def context
547539
548540 """
549541 if docstring :
550- first_line = ast . literal_eval (docstring ).strip ().split ('\n ' )[0 ]
542+ first_line = safe_literal_eval (docstring ).strip ().split ('\n ' )[0 ]
551543 if function .name + '(' in first_line .replace (' ' , '' ):
552544 return violations .D402 ()
553545
@@ -559,7 +551,7 @@ def check_capitalized(self, function, docstring):
559551
560552 """
561553 if docstring :
562- first_word = ast . literal_eval (docstring ).split ()[0 ]
554+ first_word = safe_literal_eval (docstring ).split ()[0 ]
563555 if first_word == first_word .upper ():
564556 return
565557 for char in first_word :
@@ -579,7 +571,7 @@ def check_starts_with_this(self, function, docstring):
579571 if not docstring :
580572 return
581573
582- stripped = ast . literal_eval (docstring ).strip ()
574+ stripped = safe_literal_eval (docstring ).strip ()
583575 if not stripped :
584576 return
585577
0 commit comments