Skip to content

Commit

Permalink
Use more descriptive exceptions than AssertionError
Browse files Browse the repository at this point in the history
Better match Python conventions and the stdlib by using TypeError and
ValueError rather than AssertionError.

For example, in CPython:

    $ python -c 'int(object())'
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    TypeError: int() argument must be a string, a bytes-like object or a number, not 'object'
  • Loading branch information
jdufresne committed Dec 4, 2020
1 parent 83dffeb commit d2007da
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion piptools/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def read_cache_file(cache_file_path):

# Check version and load the contents
if doc["__format__"] != 1:
raise AssertionError("Unknown cache file format")
raise ValueError("Unknown cache file format")
return doc["dependencies"]


Expand Down
2 changes: 1 addition & 1 deletion piptools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def fs_str(string):
if isinstance(string, str):
return string
if isinstance(string, bytes):
raise AssertionError
raise TypeError("fs_str() argument must not be bytes")
return string.encode(_fs_encoding)


Expand Down
2 changes: 1 addition & 1 deletion tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_read_cache_file_wrong_format():
A cache file with a wrong "__format__" value should throw an assertion error.
"""
with _read_cache_file_helper('{"__format__": 2}') as cache_file_name:
with pytest.raises(AssertionError):
with pytest.raises(ValueError, match=r"^Unknown cache file format$"):
read_cache_file(cache_file_name)


Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def test_fs_str():

@pytest.mark.skipif(six.PY2, reason="Not supported in py2")
def test_fs_str_with_bytes():
with pytest.raises(AssertionError):
with pytest.raises(TypeError, match=r"^fs_str\(\) argument must not be bytes$"):
fs_str(b"whatever")


Expand Down

0 comments on commit d2007da

Please sign in to comment.