@@ -2136,8 +2136,9 @@ def dangerous_comparison(self, left: Type, right: Type,
21362136 if isinstance (left , UnionType ) and isinstance (right , UnionType ):
21372137 left = remove_optional (left )
21382138 right = remove_optional (right )
2139- if (original_container and has_bytes_component (original_container ) and
2140- has_bytes_component (left )):
2139+ py2 = self .chk .options .python_version < (3 , 0 )
2140+ if (original_container and has_bytes_component (original_container , py2 ) and
2141+ has_bytes_component (left , py2 )):
21412142 # We need to special case bytes and bytearray, because 97 in b'abc', b'a' in b'abc',
21422143 # b'a' in bytearray(b'abc') etc. all return True (and we want to show the error only
21432144 # if the check can _never_ be True).
@@ -4179,13 +4180,16 @@ def custom_equality_method(typ: Type) -> bool:
41794180 return False
41804181
41814182
4182- def has_bytes_component (typ : Type ) -> bool :
4183+ def has_bytes_component (typ : Type , py2 : bool = False ) -> bool :
41834184 """Is this one of builtin byte types, or a union that contains it?"""
41844185 typ = get_proper_type (typ )
4186+ if py2 :
4187+ byte_types = {'builtins.str' , 'builtins.bytearray' }
4188+ else :
4189+ byte_types = {'builtins.bytes' , 'builtins.bytearray' }
41854190 if isinstance (typ , UnionType ):
41864191 return any (has_bytes_component (t ) for t in typ .items )
4187- if isinstance (typ , Instance ) and typ .type .fullname () in {'builtins.bytes' ,
4188- 'builtins.bytearray' }:
4192+ if isinstance (typ , Instance ) and typ .type .fullname () in byte_types :
41894193 return True
41904194 return False
41914195
0 commit comments