Skip to content

Commit aa706fc

Browse files
committed
allow bytes, fix literal check
1 parent 1c13be9 commit aa706fc

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

dirty_equals/_other.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,27 +166,34 @@ def __init__(self, hash_type: HashTypes):
166166
from dirty_equals import IsHash
167167
168168
assert 'f1e069787ece74531d112559945c6871' == IsHash('md5')
169+
assert b'f1e069787ece74531d112559945c6871' == IsHash('md5')
169170
assert 'f1e069787ece74531d112559945c6871' != IsHash('sha-256')
170171
assert 'F1E069787ECE74531D112559945C6871' == IsHash('md5')
171172
assert '40bd001563085fc35165329ea1ff5c5ecbdbbeef' == IsHash('sha-1')
172173
assert 'a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3' == IsHash('sha-256')
173174
```
174175
"""
175176

176-
allowed_hashes = 'md5', 'sha-1', 'sha-256'
177-
if hash_type not in HashTypes.__args__: # type: ignore[attr-defined]
177+
allowed_hashes = HashTypes.__args__ # type: ignore[attr-defined]
178+
if hash_type not in allowed_hashes:
178179
raise ValueError(f"Hash type must be one of the following values: {', '.join(allowed_hashes)}")
179180

180181
self.hash_type = hash_type
181182
super().__init__(hash_type)
182183

183184
def equals(self, other: Any) -> bool:
185+
if isinstance(other, str):
186+
s = other
187+
elif isinstance(other, (bytes, bytearray)):
188+
s = other.decode()
189+
else:
190+
return False
184191
hash_type_regex_patterns = {
185192
'md5': r'[a-fA-F\d]{32}',
186193
'sha-1': r'[a-fA-F\d]{40}',
187194
'sha-256': r'[a-fA-F\d]{64}',
188195
}
189-
return bool(re.fullmatch(hash_type_regex_patterns[self.hash_type], other))
196+
return bool(re.fullmatch(hash_type_regex_patterns[self.hash_type], s))
190197

191198

192199
IP = TypeVar('IP', IPv4Address, IPv4Network, IPv6Address, IPv6Network, Union[str, int, bytes])

tests/test_other.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ def test_ip_bad_netmask():
192192
('f1e069787ECE74531d112559945c6871', IsHash('md5')),
193193
('40bd001563085fc35165329ea1FF5c5ecbdbbeef', IsHash('sha-1')),
194194
('a665a45920422f9d417e4867eFDC4fb8a04a1f3fff1fa07e998e86f7f7a27ae3', IsHash('sha-256')),
195+
(b'f1e069787ECE74531d112559945c6871', IsHash('md5')),
196+
(bytearray(b'f1e069787ECE74531d112559945c6871'), IsHash('md5')),
195197
],
196198
)
197199
def test_is_hash_true(other, dirty):
@@ -202,6 +204,7 @@ def test_is_hash_true(other, dirty):
202204
'other,dirty',
203205
[
204206
('foobar', IsHash('md5')),
207+
(b'\x81 UnicodeDecodeError', IsHash('md5')),
205208
([1, 2, 3], IsHash('sha-1')),
206209
('f1e069787ECE74531d112559945c6871d', IsHash('md5')),
207210
('400bd001563085fc35165329ea1FF5c5ecbdbbeef', IsHash('sha-1')),

0 commit comments

Comments
 (0)