Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gettz on Python 3 fails with TypeError when passed bytes #927

Closed
labrys opened this issue Jun 23, 2019 · 3 comments · Fixed by #935
Closed

gettz on Python 3 fails with TypeError when passed bytes #927

labrys opened this issue Jun 23, 2019 · 3 comments · Fixed by #935

Comments

@labrys
Copy link
Contributor

labrys commented Jun 23, 2019

Given the following code on Python 3.7:

import dateutil.tz
dateutil.tz.gettz(b'America/New_York')

A TypeError is raised:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "lib/python3.7/site-packages/dateutil/tz/tz.py", line 1551, in __call__
    rv = self.nocache(name=name)
  File "lib/python3.7/site-packages/dateutil/tz/tz.py", line 1612, in nocache
    if name.startswith(":"):
TypeError: startswith first arg must be bytes or a tuple of bytes, not str
@labrys
Copy link
Contributor Author

labrys commented Jun 23, 2019

To be clear, a TypeError being raised for bytes isn't the problem since the docs say "Retrieve a time zone object from a string representation".

The problem is more that the raised error is unhelpful and should consider raising the TypeError with a more helpful message such as TypeError: gettz arg should be str, not bytes

@pganssle
Copy link
Member

pganssle commented Jul 7, 2019

@labrys Thanks for the report, sorry for the delay in responding. I agree with the proposed solution.

For anyone who is interested in implementing this - please benchmark your solution for the non-failing condition. I am not sure having a nice error message for this is worth a significant performance hit when someone is properly passing str.

@labrys
Copy link
Contributor Author

labrys commented Jul 8, 2019

@pganssle A try-except-raise should have little to no effect for valid values, but definitely no harm in profiling it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants