@@ -78,6 +78,51 @@ def getAlternatives(n):
78
78
LOOP_TYPES = (ast .While , ast .For )
79
79
FUNCTION_TYPES = (ast .FunctionDef ,)
80
80
81
+
82
+ if PY38_PLUS :
83
+ def _is_singleton (node ): # type: (ast.AST) -> bool
84
+ return (
85
+ isinstance (node , ast .Constant ) and
86
+ isinstance (node .value , (bool , type (Ellipsis ), type (None )))
87
+ )
88
+ elif not PY2 :
89
+ def _is_singleton (node ): # type: (ast.AST) -> bool
90
+ return isinstance (node , (ast .NameConstant , ast .Ellipsis ))
91
+ else :
92
+ def _is_singleton (node ): # type: (ast.AST) -> bool
93
+ return (
94
+ isinstance (node , ast .Name ) and
95
+ node .id in {'True' , 'False' , 'Ellipsis' , 'None' }
96
+ )
97
+
98
+
99
+ def _is_tuple_constant (node ): # type: (ast.AST) -> bool
100
+ return (
101
+ isinstance (node , ast .Tuple ) and
102
+ all (_is_constant (elt ) for elt in node .elts )
103
+ )
104
+
105
+
106
+ if PY38_PLUS :
107
+ def _is_constant (node ):
108
+ return isinstance (node , ast .Constant ) or _is_tuple_constant (node )
109
+ else :
110
+ _const_tps = (ast .Str , ast .Num )
111
+ if not PY2 :
112
+ _const_tps += (ast .Bytes ,)
113
+
114
+ def _is_constant (node ):
115
+ return (
116
+ isinstance (node , _const_tps ) or
117
+ _is_singleton (node ) or
118
+ _is_tuple_constant (node )
119
+ )
120
+
121
+
122
+ def _is_const_non_singleton (node ): # type: (ast.AST) -> bool
123
+ return _is_constant (node ) and not _is_singleton (node )
124
+
125
+
81
126
# https://github.com/python/typed_ast/blob/1.4.0/ast27/Parser/tokenizer.c#L102-L104
82
127
TYPE_COMMENT_RE = re .compile (r'^#\s*type:\s*' )
83
128
# https://github.com/python/typed_ast/blob/1.4.0/ast27/Parser/tokenizer.c#L1408-L1413
@@ -2124,14 +2169,14 @@ def ANNASSIGN(self, node):
2124
2169
self .handleNode (node .value , node )
2125
2170
2126
2171
def COMPARE (self , node ):
2127
- literals = (ast .Str , ast .Num )
2128
- if not PY2 :
2129
- literals += (ast .Bytes ,)
2130
-
2131
2172
left = node .left
2132
2173
for op , right in zip (node .ops , node .comparators ):
2133
- if (isinstance (op , (ast .Is , ast .IsNot )) and
2134
- (isinstance (left , literals ) or isinstance (right , literals ))):
2174
+ if (
2175
+ isinstance (op , (ast .Is , ast .IsNot )) and (
2176
+ _is_const_non_singleton (left ) or
2177
+ _is_const_non_singleton (right )
2178
+ )
2179
+ ):
2135
2180
self .report (messages .IsLiteral , node )
2136
2181
left = right
2137
2182
0 commit comments