File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 192
192
"_collections_abc.dict_keys" ,
193
193
"_collections_abc.dict_items" ,
194
194
]
195
+ OVERLAPPING_BYTES_ALLOWLIST : Final = {
196
+ "builtins.bytes" ,
197
+ "builtins.bytearray" ,
198
+ "builtins.memoryview" ,
199
+ }
195
200
196
201
197
202
class TooManyUnions (Exception ):
@@ -3164,6 +3169,10 @@ def dangerous_comparison(
3164
3169
return self .dangerous_comparison (left .args [0 ], right .args [0 ])
3165
3170
elif left_name in ("builtins.list" , "builtins.tuple" ) and right_name == left_name :
3166
3171
return self .dangerous_comparison (left .args [0 ], right .args [0 ])
3172
+ elif left_name in OVERLAPPING_BYTES_ALLOWLIST and right_name in (
3173
+ OVERLAPPING_BYTES_ALLOWLIST
3174
+ ):
3175
+ return False
3167
3176
if isinstance (left , LiteralType ) and isinstance (right , LiteralType ):
3168
3177
if isinstance (left .value , bool ) and isinstance (right .value , bool ):
3169
3178
# Comparing different booleans is not dangerous.
Original file line number Diff line number Diff line change @@ -2148,3 +2148,29 @@ def f(x: bytes) -> None: ...
2148
2148
f(bytearray(b"asdf"))
2149
2149
f(memoryview(b"asdf")) # E: Argument 1 to "f" has incompatible type "memoryview"; expected "bytes"
2150
2150
[builtins fixtures/primitives.pyi]
2151
+
2152
+ [case testDisableBytearrayMemoryviewPromotionStrictEquality]
2153
+ # flags: --disable-bytearray-promotion --disable-memoryview-promotion --strict-equality
2154
+ def f(x: bytes, y: bytearray, z: memoryview) -> None:
2155
+ x == y
2156
+ y == z
2157
+ x == z
2158
+ 97 in x
2159
+ 97 in y
2160
+ 97 in z
2161
+ x in y
2162
+ x in z
2163
+ [builtins fixtures/primitives.pyi]
2164
+
2165
+ [case testEnableBytearrayMemoryviewPromotionStrictEquality]
2166
+ # flags: --strict-equality
2167
+ def f(x: bytes, y: bytearray, z: memoryview) -> None:
2168
+ x == y
2169
+ y == z
2170
+ x == z
2171
+ 97 in x
2172
+ 97 in y
2173
+ 97 in z
2174
+ x in y
2175
+ x in z
2176
+ [builtins fixtures/primitives.pyi]
You can’t perform that action at this time.
0 commit comments