@@ -413,9 +413,9 @@ def _check_if_assertion_pass_impl():
413
413
return True if util ._assertion_pass else False
414
414
415
415
416
- unary_map = {ast .Not : "not %s" , ast .Invert : "~%s" , ast .USub : "-%s" , ast .UAdd : "+%s" }
416
+ UNARY_MAP = {ast .Not : "not %s" , ast .Invert : "~%s" , ast .USub : "-%s" , ast .UAdd : "+%s" }
417
417
418
- binop_map = {
418
+ BINOP_MAP = {
419
419
ast .BitOr : "|" ,
420
420
ast .BitXor : "^" ,
421
421
ast .BitAnd : "&" ,
@@ -438,20 +438,8 @@ def _check_if_assertion_pass_impl():
438
438
ast .IsNot : "is not" ,
439
439
ast .In : "in" ,
440
440
ast .NotIn : "not in" ,
441
+ ast .MatMult : "@" ,
441
442
}
442
- # Python 3.5+ compatibility
443
- try :
444
- binop_map [ast .MatMult ] = "@"
445
- except AttributeError :
446
- pass
447
-
448
- # Python 3.4+ compatibility
449
- if hasattr (ast , "NameConstant" ):
450
- _NameConstant = ast .NameConstant
451
- else :
452
-
453
- def _NameConstant (c ):
454
- return ast .Name (str (c ), ast .Load ())
455
443
456
444
457
445
def set_location (node , lineno , col_offset ):
@@ -774,7 +762,7 @@ def visit_Assert(self, assert_):
774
762
variables = [
775
763
ast .Name (name , ast .Store ()) for name in self .format_variables
776
764
]
777
- clear_format = ast .Assign (variables , _NameConstant (None ))
765
+ clear_format = ast .Assign (variables , ast . NameConstant (None ))
778
766
self .statements .append (clear_format )
779
767
780
768
else : # Original assertion rewriting
@@ -800,7 +788,7 @@ def visit_Assert(self, assert_):
800
788
# Clear temporary variables by setting them to None.
801
789
if self .variables :
802
790
variables = [ast .Name (name , ast .Store ()) for name in self .variables ]
803
- clear = ast .Assign (variables , _NameConstant (None ))
791
+ clear = ast .Assign (variables , ast . NameConstant (None ))
804
792
self .statements .append (clear )
805
793
# Fix line numbers.
806
794
for stmt in self .statements :
@@ -880,13 +868,13 @@ def visit_BoolOp(self, boolop):
880
868
return ast .Name (res_var , ast .Load ()), self .explanation_param (expl )
881
869
882
870
def visit_UnaryOp (self , unary ):
883
- pattern = unary_map [unary .op .__class__ ]
871
+ pattern = UNARY_MAP [unary .op .__class__ ]
884
872
operand_res , operand_expl = self .visit (unary .operand )
885
873
res = self .assign (ast .UnaryOp (unary .op , operand_res ))
886
874
return res , pattern % (operand_expl ,)
887
875
888
876
def visit_BinOp (self , binop ):
889
- symbol = binop_map [binop .op .__class__ ]
877
+ symbol = BINOP_MAP [binop .op .__class__ ]
890
878
left_expr , left_expl = self .visit (binop .left )
891
879
right_expr , right_expl = self .visit (binop .right )
892
880
explanation = "({} {} {})" .format (left_expl , symbol , right_expl )
@@ -953,7 +941,7 @@ def visit_Compare(self, comp):
953
941
if isinstance (next_operand , (ast .Compare , ast .BoolOp )):
954
942
next_expl = "({})" .format (next_expl )
955
943
results .append (next_res )
956
- sym = binop_map [op .__class__ ]
944
+ sym = BINOP_MAP [op .__class__ ]
957
945
syms .append (ast .Str (sym ))
958
946
expl = "{} {} {}" .format (left_expl , sym , next_expl )
959
947
expls .append (ast .Str (expl ))
0 commit comments