2
2
3
3
import ast
4
4
import string
5
- import sys
6
- import textwrap
7
5
import tokenize as tk
8
6
from collections import namedtuple
9
7
from itertools import chain , takewhile
28
26
from .utils import (
29
27
common_prefix_length ,
30
28
is_blank ,
29
+ is_fstring ,
31
30
log ,
32
31
pairwise ,
32
+ safe_literal_eval ,
33
33
strip_non_alphanumeric ,
34
34
)
35
35
from .wordlists import IMPERATIVE_BLACKLIST , IMPERATIVE_VERBS , stem
@@ -46,14 +46,6 @@ def decorator(f):
46
46
return decorator
47
47
48
48
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
-
57
49
class ConventionChecker :
58
50
"""Checker for PEP 257, NumPy and Google conventions.
59
51
@@ -201,7 +193,7 @@ def check_docstring_fstring(self, definition, docstring):
201
193
and users may attempt to use them as docstrings. This is an
202
194
outright mistake so we issue a specific error code.
203
195
"""
204
- if _is_fstring (docstring ):
196
+ if is_fstring (docstring ):
205
197
return violations .D303 ()
206
198
207
199
@check_for (Definition , terminal = True )
@@ -222,7 +214,7 @@ def check_docstring_missing(self, definition, docstring):
222
214
not docstring
223
215
and definition .is_public
224
216
or docstring
225
- and is_blank (docstring , literal_eval = True )
217
+ and is_blank (safe_literal_eval ( docstring ) )
226
218
):
227
219
codes = {
228
220
Module : violations .D100 ,
@@ -252,7 +244,7 @@ def check_one_liners(self, definition, docstring):
252
244
253
245
"""
254
246
if docstring :
255
- lines = ast . literal_eval (docstring ).split ('\n ' )
247
+ lines = safe_literal_eval (docstring ).split ('\n ' )
256
248
if len (lines ) > 1 :
257
249
non_empty_lines = sum (1 for l in lines if not is_blank (l ))
258
250
if non_empty_lines == 1 :
@@ -328,7 +320,7 @@ def check_blank_after_summary(self, definition, docstring):
328
320
329
321
"""
330
322
if docstring :
331
- lines = ast . literal_eval (docstring ).strip ().split ('\n ' )
323
+ lines = safe_literal_eval (docstring ).strip ().split ('\n ' )
332
324
if len (lines ) > 1 :
333
325
post_summary_blanks = list (map (is_blank , lines [1 :]))
334
326
blanks_count = sum (takewhile (bool , post_summary_blanks ))
@@ -381,7 +373,7 @@ def check_newline_after_last_paragraph(self, definition, docstring):
381
373
if docstring :
382
374
lines = [
383
375
l
384
- for l in ast . literal_eval (docstring ).split ('\n ' )
376
+ for l in safe_literal_eval (docstring ).split ('\n ' )
385
377
if not is_blank (l )
386
378
]
387
379
if len (lines ) > 1 :
@@ -392,7 +384,7 @@ def check_newline_after_last_paragraph(self, definition, docstring):
392
384
def check_surrounding_whitespaces (self , definition , docstring ):
393
385
"""D210: No whitespaces allowed surrounding docstring text."""
394
386
if docstring :
395
- lines = ast . literal_eval (docstring ).split ('\n ' )
387
+ lines = safe_literal_eval (docstring ).split ('\n ' )
396
388
if (
397
389
lines [0 ].startswith (' ' )
398
390
or len (lines ) == 1
@@ -420,7 +412,7 @@ def check_multi_line_summary_start(self, definition, docstring):
420
412
"ur'''" ,
421
413
]
422
414
423
- lines = ast . literal_eval (docstring ).split ('\n ' )
415
+ lines = safe_literal_eval (docstring ).split ('\n ' )
424
416
if len (lines ) > 1 :
425
417
first = docstring .split ("\n " )[0 ].strip ().lower ()
426
418
if first in start_triple :
@@ -442,7 +434,7 @@ def check_triple_double_quotes(self, definition, docstring):
442
434
443
435
'''
444
436
if docstring :
445
- if '"""' in ast . literal_eval (docstring ):
437
+ if '"""' in safe_literal_eval (docstring ):
446
438
# Allow ''' quotes if docstring contains """, because
447
439
# otherwise """ quotes could not be expressed inside
448
440
# docstring. Not in PEP 257.
@@ -486,7 +478,7 @@ def _check_ends_with(docstring, chars, violation):
486
478
487
479
"""
488
480
if docstring :
489
- summary_line = ast . literal_eval (docstring ).strip ().split ('\n ' )[0 ]
481
+ summary_line = safe_literal_eval (docstring ).strip ().split ('\n ' )[0 ]
490
482
if not summary_line .endswith (chars ):
491
483
return violation (summary_line [- 1 ])
492
484
@@ -521,7 +513,7 @@ def check_imperative_mood(self, function, docstring): # def context
521
513
522
514
"""
523
515
if docstring and not function .is_test :
524
- stripped = ast . literal_eval (docstring ).strip ()
516
+ stripped = safe_literal_eval (docstring ).strip ()
525
517
if stripped :
526
518
first_word = strip_non_alphanumeric (stripped .split ()[0 ])
527
519
check_word = first_word .lower ()
@@ -547,7 +539,7 @@ def check_no_signature(self, function, docstring): # def context
547
539
548
540
"""
549
541
if docstring :
550
- first_line = ast . literal_eval (docstring ).strip ().split ('\n ' )[0 ]
542
+ first_line = safe_literal_eval (docstring ).strip ().split ('\n ' )[0 ]
551
543
if function .name + '(' in first_line .replace (' ' , '' ):
552
544
return violations .D402 ()
553
545
@@ -559,7 +551,7 @@ def check_capitalized(self, function, docstring):
559
551
560
552
"""
561
553
if docstring :
562
- first_word = ast . literal_eval (docstring ).split ()[0 ]
554
+ first_word = safe_literal_eval (docstring ).split ()[0 ]
563
555
if first_word == first_word .upper ():
564
556
return
565
557
for char in first_word :
@@ -579,7 +571,7 @@ def check_starts_with_this(self, function, docstring):
579
571
if not docstring :
580
572
return
581
573
582
- stripped = ast . literal_eval (docstring ).strip ()
574
+ stripped = safe_literal_eval (docstring ).strip ()
583
575
if not stripped :
584
576
return
585
577
0 commit comments