seanpianka / Zipcodes

A simple library for querying U.S. zipcodes.
MIT License
78 stars 15 forks source link

Fix exception types on README #19

Closed rafascar closed 2 years ago

rafascar commented 2 years ago

Both examples zipcodes.matching('0646a') and zipcodes.matching('064690') raise a ValueError--instead of the documented TypeError--as they are strings and only fail on the _clean_zipcode step.

Reproducible on zipcodes==1.2.0

>>> zipcodes.matching('0646a')
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python3.9/site-packages/zipcodes/__init__.py", line 55, in decorator
    _clean(zipcode, min(len(zipcode), _valid_zipcode_length)), *args, **kwargs
  File "/usr/local/lib/python3.9/site-packages/zipcodes/__init__.py", line 122, in _clean
    raise ValueError('Invalid characters, zipcode may only contain digits and "-".')
ValueError: Invalid characters, zipcode may only contain digits and "-".

>>> zipcodes.matching('064690')
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python3.9/site-packages/zipcodes/__init__.py", line 55, in decorator
    _clean(zipcode, min(len(zipcode), _valid_zipcode_length)), *args, **kwargs
  File "/usr/local/lib/python3.9/site-packages/zipcodes/__init__.py", line 117, in _clean
    raise ValueError(
ValueError: Invalid format, zipcode must be of the format: "#####" or "#####-####"
seanpianka commented 2 years ago

Thanks for catching this! Given that this issue would only be seen by downstream users at run-time, it's particularly important that the docs specify the correct error to handle... :sweat_smile: