Skip to content

Commit

Permalink
Reject str as a primitive in to_hex()
Browse files Browse the repository at this point in the history
Adds an exception in `to_hex()` if str
type is provided to primitive argument

Relates to #85
  • Loading branch information
palash25 committed Apr 13, 2018
1 parent a1a2ee2 commit b4c14f9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion eth_utils/conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ def to_hex(primitive=None, hexstr=None, text=None):
if isinstance(primitive, (bytes, bytearray)):
return encode_hex(primitive)
elif is_string(primitive):
return to_hex(text=primitive)
raise TypeError(
"Unsupported type: The primitive argument must be one of: bytes,"
"bytearray, int or bool and not str"
)

if is_integer(primitive):
return hex(primitive)
Expand Down
5 changes: 5 additions & 0 deletions tests/conversion-utils/test_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,8 @@ def test_to_hex_text(val, expected):
)
def test_to_hex_cleanup_only(val, expected):
assert to_hex(hexstr=val) == expected


def test_to_hex_exception():
with pytest.raises(TypeError, match="Unsupported type: The primitive argument"):
to_hex(primitive='string')

0 comments on commit b4c14f9

Please sign in to comment.