26
26
from ._compat import IS_PY3
27
27
from ._compat import IS_PY34_OR_GREATER
28
28
from ._compat import IS_PY35_OR_GREATER
29
+ from ._compat import IS_PY38_OR_GREATER
29
30
30
31
import ast
31
32
import contextlib
@@ -541,27 +542,65 @@ def node_contents_visit(self, node):
541
542
542
543
# ast for Literals
543
544
544
- def visit_Num (self , node ):
545
- """Allow integer numbers without restrictions.
545
+ if IS_PY38_OR_GREATER :
546
546
547
- Replaced by Constant in Python 3.8.
548
- """
549
- return self .node_contents_visit (node )
547
+ def visit_Constant (self , node ):
548
+ """Allow constant literals with restriction for Ellipsis.
550
549
551
- def visit_Str (self , node ):
552
- """Allow string literals without restrictions.
550
+ Constant replaces Num, Str, Bytes, NameConstant and Ellipsis in
551
+ Python 3.8+.
552
+ :see: https://docs.python.org/dev/whatsnew/3.8.html#deprecated
553
+ """
554
+ if node .value is Ellipsis :
555
+ # Deny using `...`.
556
+ # Special handling necessary as ``self.not_allowed(node)``
557
+ # would return the Error Message:
558
+ # 'Constant statements are not allowed.'
559
+ # which is only partial true.
560
+ self .error (node , 'Ellipsis statements are not allowed.' )
561
+ return
562
+ return self .node_contents_visit (node )
553
563
554
- Replaced by Constant in Python 3.8.
555
- """
556
- return self .node_contents_visit (node )
564
+ else :
557
565
558
- def visit_Bytes (self , node ):
559
- """Allow bytes literals without restrictions.
566
+ def visit_Num (self , node ):
567
+ """Allow integer numbers without restrictions.
560
568
561
- Bytes is Python 3 only.
562
- Replaced by Constant in Python 3.8.
563
- """
564
- return self .node_contents_visit (node )
569
+ Replaced by Constant in Python 3.8.
570
+ """
571
+ return self .node_contents_visit (node )
572
+
573
+ def visit_Str (self , node ):
574
+ """Allow string literals without restrictions.
575
+
576
+ Replaced by Constant in Python 3.8.
577
+ """
578
+ return self .node_contents_visit (node )
579
+
580
+ def visit_Bytes (self , node ):
581
+ """Allow bytes literals without restrictions.
582
+
583
+ Bytes is Python 3 only.
584
+ Replaced by Constant in Python 3.8.
585
+ """
586
+ return self .node_contents_visit (node )
587
+
588
+ def visit_Ellipsis (self , node ):
589
+ """Deny using `...`.
590
+
591
+ Ellipsis exists only in Python 3.
592
+ Replaced by Constant in Python 3.8.
593
+ """
594
+ return self .not_allowed (node )
595
+
596
+ def visit_NameConstant (self , node ):
597
+ """Allow constant literals (True, False, None) without ...
598
+
599
+ restrictions.
600
+
601
+ Replaced by Constant in Python 3.8.
602
+ """
603
+ return self .node_contents_visit (node )
565
604
566
605
def visit_List (self , node ):
567
606
"""Allow list literals without restrictions."""
@@ -587,38 +626,6 @@ def visit_JoinedStr(self, node):
587
626
"""Allow joined string without restrictions."""
588
627
return self .node_contents_visit (node )
589
628
590
- def visit_Constant (self , node ):
591
- """Allow constant literals with restriction for Ellipsis.
592
-
593
- Constant replaces Num, Str, Bytes, NameConstant and Ellipsis in
594
- Python 3.8+.
595
- :see: https://docs.python.org/dev/whatsnew/3.8.html#deprecated
596
- """
597
- if node .value is Ellipsis :
598
- # Deny using `...`.
599
- # Special handling necessary as ``self.not_allowed(node)``
600
- # would return the Error Message:
601
- # 'Constant statements are not allowed.'
602
- # which is only partial true.
603
- self .error (node , 'Ellipsis statements are not allowed.' )
604
- return
605
- return self .node_contents_visit (node )
606
-
607
- def visit_Ellipsis (self , node ):
608
- """Deny using `...`.
609
-
610
- Ellipsis is exists only in Python 3.
611
- Replaced by Constant in Python 3.8.
612
- """
613
- return self .not_allowed (node )
614
-
615
- def visit_NameConstant (self , node ):
616
- """Allow constant literals (True, False, None) without restrictions.
617
-
618
- Replaced by Constant in Python 3.8.
619
- """
620
- return self .node_contents_visit (node )
621
-
622
629
# ast for Variables
623
630
624
631
def visit_Name (self , node ):
0 commit comments