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

xds/cleanup: fix error datetime fromisoformat not available #28953

Merged
merged 4 commits into from
Feb 25, 2022

Conversation

menghanl
Copy link
Contributor

datetime.fromisoformat is available in >=python3.7, kokoro is running python3.6

item['creationTimestamp']) <= get_expire_timestamp():
if datetime.datetime.strptime(
item['creationTimestamp'],
"%Y-%m-%dT%H:%M:%S.%f%z") <= get_expire_timestamp():
Copy link
Member

@sergiitk sergiitk Feb 23, 2022

Choose a reason for hiding this comment

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

TIL neither fromisoformat nor strptime are good enough ISO time parsers.

(1) fromisoformat: https://docs.python.org/3/library/datetime.html#datetime.datetime.fromisoformat

Caution This does not support parsing arbitrary ISO 8601 strings - it is only intended as the inverse operation of datetime.isoformat(). A more full-featured ISO 8601 parser, dateutil.parser.isoparse is available in the third-party package dateutil.

And it doesn't support Zulu time:

>>> datetime.fromisoformat("2008-09-03T20:56:35.450686Z")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Invalid isoformat string: '2008-09-03T20:56:35.450686Z'

(2) strptime does is pretty terrible with timezones in python3.6: https://stackoverflow.com/a/30696682/11697987

# zone with `:` separators not supported
>>> datetime.strptime("2008-09-03T20:56:35.450686+04:00", "%Y-%m-%dT%H:%M:%S.%f%z")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 362, in _strptime
    (data_string, format))
ValueError: time data '2008-09-03T20:56:35.450686+04:00' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'

# Zulu not supported
>>> datetime.strptime("2008-09-03T20:56:35.450686Z", "%Y-%m-%dT%H:%M:%S.%f%z")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 362, in _strptime
    (data_string, format))
ValueError: time data '2008-09-03T20:56:35.450686Z' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'

Let's use dateutil.parser.isoparse, as recommended in python docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated. Using parse, not isoparse. Let me know if isoparse is better.

@menghanl menghanl force-pushed the cleanup_date_format branch 2 times, most recently from ec3b238 to 70f77d8 Compare February 23, 2022 20:10
@@ -337,7 +338,7 @@ def main(argv):
compute = gcp.compute.ComputeV1(gcp.api.GcpApiManager(), project)
leakedHealthChecks = []
for item in compute.list_health_check()['items']:
if datetime.datetime.fromisoformat(
if dateutil.parser.parse(
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if dateutil.parser.parse(
if dateutil.parser.isoparse(

@@ -2,6 +2,7 @@ Mako~=1.1
PyYAML~=5.3
absl-py~=0.11
dataclasses~=0.8; python_version < '3.7'
dateutil~=2.8.2
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
dateutil~=2.8.2
dateutil~=2.8

It's not exactly semver. To allow patch-level updates, the last component needs to be omitted. See https://www.python.org/dev/peps/pep-0440/#compatible-release

~= 2.2
>= 2.2, == 2.*

~= 1.4.5
>= 1.4.5, == 1.4.*

@menghanl menghanl force-pushed the cleanup_date_format branch from 70f77d8 to 1569932 Compare February 23, 2022 20:11
@menghanl menghanl force-pushed the cleanup_date_format branch from 1569932 to 0658d6a Compare February 23, 2022 20:12
@menghanl
Copy link
Contributor Author

All fixed.
Test run: http://sponge2/fd0141fc-86a9-4270-8c15-27a7d2f159f8

@menghanl menghanl added the release notes: no Indicates if PR should not be in release notes label Feb 23, 2022
@menghanl
Copy link
Contributor Author

I added a commit to make keep_period configurable. The leaked resources on staging were less than 7 days old.

@menghanl menghanl force-pushed the cleanup_date_format branch from 21f13d3 to 04b614f Compare February 24, 2022 18:18
Copy link
Contributor

@lidizheng lidizheng left a comment

Choose a reason for hiding this comment

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

LGTM to unblock the merge.

@menghanl menghanl closed this Feb 24, 2022
@menghanl menghanl reopened this Feb 24, 2022
@menghanl menghanl force-pushed the cleanup_date_format branch from 210b466 to 113ebff Compare February 24, 2022 22:28
@menghanl menghanl merged commit 87068e0 into grpc:master Feb 25, 2022
@copybara-service copybara-service bot added the imported Specifies if the PR has been imported to the internal repository label Feb 25, 2022
sergiitk pushed a commit to sergiitk/grpc that referenced this pull request Dec 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
imported Specifies if the PR has been imported to the internal repository release notes: no Indicates if PR should not be in release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants