35
35
36
36
__all__ = ('check' ,)
37
37
38
+ FSTRING_REGEX = re (r'^([rR]?)[fF]' )
39
+
40
+
41
+ def eval_docstring (docstring ):
42
+ """Safely evaluate docstring."""
43
+ if FSTRING_REGEX .match (str (docstring )):
44
+ return ""
45
+ return ast .literal_eval (docstring )
46
+
38
47
39
48
def check_for (kind , terminal = False ):
40
49
def decorator (f ):
@@ -241,7 +250,7 @@ def check_docstring_empty(self, definition, docstring):
241
250
NOTE: This used to report as D10X errors.
242
251
243
252
"""
244
- if docstring and is_blank (ast . literal_eval (docstring )):
253
+ if docstring and is_blank (eval_docstring (docstring )):
245
254
return violations .D419 ()
246
255
247
256
@check_for (Definition )
@@ -253,7 +262,7 @@ def check_one_liners(self, definition, docstring):
253
262
254
263
"""
255
264
if docstring :
256
- lines = ast . literal_eval (docstring ).split ('\n ' )
265
+ lines = eval_docstring (docstring ).split ('\n ' )
257
266
if len (lines ) > 1 :
258
267
non_empty_lines = sum (1 for l in lines if not is_blank (l ))
259
268
if non_empty_lines == 1 :
@@ -329,7 +338,7 @@ def check_blank_after_summary(self, definition, docstring):
329
338
330
339
"""
331
340
if docstring :
332
- lines = ast . literal_eval (docstring ).strip ().split ('\n ' )
341
+ lines = eval_docstring (docstring ).strip ().split ('\n ' )
333
342
if len (lines ) > 1 :
334
343
post_summary_blanks = list (map (is_blank , lines [1 :]))
335
344
blanks_count = sum (takewhile (bool , post_summary_blanks ))
@@ -382,7 +391,7 @@ def check_newline_after_last_paragraph(self, definition, docstring):
382
391
if docstring :
383
392
lines = [
384
393
l
385
- for l in ast . literal_eval (docstring ).split ('\n ' )
394
+ for l in eval_docstring (docstring ).split ('\n ' )
386
395
if not is_blank (l )
387
396
]
388
397
if len (lines ) > 1 :
@@ -393,7 +402,7 @@ def check_newline_after_last_paragraph(self, definition, docstring):
393
402
def check_surrounding_whitespaces (self , definition , docstring ):
394
403
"""D210: No whitespaces allowed surrounding docstring text."""
395
404
if docstring :
396
- lines = ast . literal_eval (docstring ).split ('\n ' )
405
+ lines = eval_docstring (docstring ).split ('\n ' )
397
406
if (
398
407
lines [0 ].startswith (' ' )
399
408
or len (lines ) == 1
@@ -421,7 +430,7 @@ def check_multi_line_summary_start(self, definition, docstring):
421
430
"ur'''" ,
422
431
]
423
432
424
- lines = ast . literal_eval (docstring ).split ('\n ' )
433
+ lines = eval_docstring (docstring ).split ('\n ' )
425
434
if len (lines ) > 1 :
426
435
first = docstring .split ("\n " )[0 ].strip ().lower ()
427
436
if first in start_triple :
@@ -443,7 +452,7 @@ def check_triple_double_quotes(self, definition, docstring):
443
452
444
453
'''
445
454
if docstring :
446
- if '"""' in ast . literal_eval (docstring ):
455
+ if '"""' in eval_docstring (docstring ):
447
456
# Allow ''' quotes if docstring contains """, because
448
457
# otherwise """ quotes could not be expressed inside
449
458
# docstring. Not in PEP 257.
@@ -487,7 +496,7 @@ def _check_ends_with(docstring, chars, violation):
487
496
488
497
"""
489
498
if docstring :
490
- summary_line = ast . literal_eval (docstring ).strip ().split ('\n ' )[0 ]
499
+ summary_line = eval_docstring (docstring ).strip ().split ('\n ' )[0 ]
491
500
if not summary_line .endswith (chars ):
492
501
return violation (summary_line [- 1 ])
493
502
@@ -526,7 +535,7 @@ def check_imperative_mood(self, function, docstring): # def context
526
535
and not function .is_test
527
536
and not function .is_property (self .property_decorators )
528
537
):
529
- stripped = ast . literal_eval (docstring ).strip ()
538
+ stripped = eval_docstring (docstring ).strip ()
530
539
if stripped :
531
540
first_word = strip_non_alphanumeric (stripped .split ()[0 ])
532
541
check_word = first_word .lower ()
@@ -552,7 +561,7 @@ def check_no_signature(self, function, docstring): # def context
552
561
553
562
"""
554
563
if docstring :
555
- first_line = ast . literal_eval (docstring ).strip ().split ('\n ' )[0 ]
564
+ first_line = eval_docstring (docstring ).strip ().split ('\n ' )[0 ]
556
565
if function .name + '(' in first_line .replace (' ' , '' ):
557
566
return violations .D402 ()
558
567
@@ -564,7 +573,7 @@ def check_capitalized(self, function, docstring):
564
573
565
574
"""
566
575
if docstring :
567
- first_word = ast . literal_eval (docstring ).split ()[0 ]
576
+ first_word = eval_docstring (docstring ).split ()[0 ]
568
577
if first_word == first_word .upper ():
569
578
return
570
579
for char in first_word :
@@ -596,7 +605,7 @@ def check_starts_with_this(self, function, docstring):
596
605
if not docstring :
597
606
return
598
607
599
- stripped = ast . literal_eval (docstring ).strip ()
608
+ stripped = eval_docstring (docstring ).strip ()
600
609
if not stripped :
601
610
return
602
611
0 commit comments