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

Fix tz.gettz() not returning local time for empty string #1024

Merged
merged 2 commits into from Apr 24, 2020

Conversation

ffe4
Copy link
Member

@ffe4 ffe4 commented Apr 3, 2020

Summary of changes

  • tz.gettz() now properly checks for empty string and returns local time
  • added test for tz.gettz() returning the same result when passed empty string or None

nocache checks the gettz(name) parameter with if not name and tries to get the local time from the TZ environment variable. When the environment variable does not exist, it only checked for name is None, skipping the logic that searches for the local time tzfile when passed an empty string.

Closes #925
Closes #926

Pull Request Checklist

  • Changes have tests
  • Authors have been added to AUTHORS.md
  • News fragment added in changelog.d. See CONTRIBUTING.md for details

@ffe4 ffe4 force-pushed the issue_926 branch 2 times, most recently from a6f8698 to 841cc6c Compare April 3, 2020 20:19
@ffe4 ffe4 marked this pull request as ready for review April 3, 2020 20:35
@ffe4 ffe4 closed this Apr 3, 2020
@ffe4 ffe4 reopened this Apr 3, 2020
dateutil/test/test_tz.py Outdated Show resolved Hide resolved
@ffe4 ffe4 changed the title Fix tz.gettz() not returning local time for empty string WIP: Fix tz.gettz() not returning local time for empty string Apr 5, 2020
@ffe4 ffe4 force-pushed the issue_926 branch 2 times, most recently from 14d4328 to 167bcf0 Compare April 5, 2020 12:53
dateutil/test/test_tz.py Outdated Show resolved Hide resolved
dateutil/test/test_tz.py Outdated Show resolved Hide resolved
@ffe4 ffe4 requested a review from pganssle April 5, 2020 13:23
Copy link
Member

@pganssle pganssle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is turning out trickier than I expected. Thanks for putting in the work on this @ffe4!

One additional comment: I've been putting property tests in their own file in the tests/property directory to make it easier to run them separately from the main test suite. Can we move the property test into a new tests/property/test_tz_prop.py file?

dateutil/test/test_tz.py Outdated Show resolved Hide resolved
dateutil/test/test_tz.py Outdated Show resolved Hide resolved
dateutil/test/test_tz.py Outdated Show resolved Hide resolved
@ffe4 ffe4 requested a review from pganssle April 8, 2020 19:55
@ffe4 ffe4 changed the title WIP: Fix tz.gettz() not returning local time for empty string Fix tz.gettz() not returning local time for empty string Apr 8, 2020
@ffe4 ffe4 force-pushed the issue_926 branch 3 times, most recently from 492334b to df29e69 Compare April 13, 2020 15:40
@@ -1596,7 +1596,7 @@ def nocache(name=None):
name = os.environ["TZ"]
except KeyError:
pass
if name is None or name == ":":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll note that this will suddenly allow tz.gettz(0) without throwing an exception.

Maybe we should go with if name is None or name in ("", ":"):?

In a potential future version we may want to explicitly raise TypeError if name is not an instance of str or None, but I'm inclined to defer the discussion about duck typing/leniency and just implement this in such a way that we don't have to address it right now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, makes sense. Although I would be interested to hear what you think about how we should do typing (#383) after the deprecation #653

Also, should I squash my commits to clean up the commit history, will you just squash them all when merging, or what are your preferences in that regard?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally rewrite my history so that each commit is an atomic change where the tests pass. If that's too much of a pain, squashing it down is also a valid approach.

Copy link
Member

@pganssle pganssle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've gone ahead and squashed this PR down to 2 commits, looks good to me, we can merge on green!

Thanks @ffe4!

The documented behavior of the function is to return a local time zone
when the argument is None or an empty string.

This was working if the TZ environment variable was set, but not working
otherwise.

Fixes dateutil#925, dateutil#926
The current tests only check that `tz.gettz()` and `tz.gettz("")` do
the same thing, but not that the thing that they do is equivalent to a
local time zone.

Defining what is a "local time" is a bit complicated, considering that
`tz.gettz()` may return a `tzfile` or a `tzlocal`, and neither of those
have the same behavior as Python's built-in local time support via
`.astimezone()`. This property test checks only the datetimes and
properties that are expected to be the same between the various methods
of getting a "local" time zone.
@pganssle pganssle merged commit 0905e27 into dateutil:master Apr 24, 2020
@ffe4 ffe4 deleted the issue_926 branch June 16, 2020 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

gettz('') result is incorrect per the docs gettz('') fails on windows
2 participants