refactor: change use of pyrfc3339 to use ciso8601#1243
refactor: change use of pyrfc3339 to use ciso8601#1243EdmilsonRodrigues wants to merge 2 commits intojuju:mainfrom
Conversation
|
What's your personal opinion on |
|
Adding @Aflynn50 for the Juju perspective. |
| # Note: fromtimestamp bizarrely produces a time without | ||
| # a time zone, so we need to use accept_naive. | ||
| go_cookie["Expires"] = pyrfc3339.generate(unix_time, accept_naive=True) | ||
| go_cookie["Expires"] = generate_rfc3339_from_unix_time(unix_time) |
There was a problem hiding this comment.
I see that there's a solid-looking set of tests in tests/unit/test_gocookies.py.
Could you clarify why the custom parses function was needed, what specific inputs could not be parsed?
There was a problem hiding this comment.
ciso8601 does not have a function to generate a rfc3339 timestamp from a datetime, as this one of the pyrfc3339 library, so I implemented my own.
There was a problem hiding this comment.
I wonder if datetime.isoformat() would be enough.
I'll need someone who knows about go-style cookies and why are those important, if Juju relies on those in some way.
|
/build |
|
Fixes #1101 |
Please run unit tests 🙇🏻 |
|
Re: What's your personal opinion on ciso8601 vs. backports.datetime_fromisoformat? My personal perspective is that backports.datetime_fromisoformat is a better use since it doesn't need an external library. But as I am new to the code base and still learning how it works, I wouldn't dare make this change by myself since it would need to change the rfc3339 format to use ISO format. I don't know if this would impact how the GoJuju library works, so I used the suggestion stated in the issue and used the ciso8601. But this is based in lack of experience with the library. |
|
Re: self = <tests.unit.test_gocookies.TestGoCookieJar testMethod=test_roundtrip>
tests/unit/test_gocookies.py:201: juju/client/gocookies.py:46: in save unix_time = datetime.datetime(2345, 11, 15, 18, 16, 8)
E ValueError: not enough values to unpack (expected 2, got 1) Thank you!! I didn't know yet how to run the unit tests. Fixing this. |
|
When #1101 was raised, I think we assumed that I'm +1 on using I haven't familiarised myself with the differences between the import datetime, time
timestamp = time.time()
timezone = datetime.timezone.utc
# or do we need local timezone?
# utcoffset = datetime.datetime.now().astimezone().utcoffset() # can be None apparently
# timezone = datetime.timezone(utcoffset) if utcoffset is not None else datetime.timezone.utc
datetime.datetime.fromtimestamp(timestamp, tz=timezone).isoformat()
# '2024-12-17T23:34:38.780403+00:00' |
|
@hpidcock commented on Matrix:
|
|
Here is a useful but overwhelming reference to the differences and overlap between I believe that this should be a perfectly valid datetime.datetime.fromtimestamp(timestamp, tz=timezone).isoformat()
# '2024-12-17T23:34:38.780403+00:00'For parsing the date time strings we get from Juju, I suggest that we try |
|
I don't know how to refer the part of the code the way you do, but I mean this: def test_expiry_time(self):
content = """[
{
"CanonicalHost": "bar.com",
"Creation": "2017-11-17T08:53:55.088820092Z",
"Domain": "bar.com",
"Expires": "2345-11-15T18:16:08Z",
"HostOnly": true,
"HttpOnly": false,
"LastAccess": "2017-11-17T08:53:55.088822562Z",
"Name": "bar",
"Path": "/",
"Persistent": true,
"Secure": false,
"Updated": "2017-11-17T08:53:55.088822562Z",
"Value": "bar-value"
}
]"""
jar = self.load_jar(content)
got_expires = tuple(jar)[0].expires
want_expires = int(ciso8601.parse_rfc3339("2345-11-15T18:16:08Z").timestamp())
self.assertEqual(got_expires, want_expires)In this test, I think this is an example of a juju/persitent-cookiejar cookie. This format is compatible with fromisoformat():
I agree with using the backports.datetime_fromisoformat and test generating using the .isoformat(). |
|
Would you consider making an alternative PR using backports? |
|
Sure! I will open it in some minutes! The tests are failing I'll fix this and open it tomorrow. |
|
Closing in favour of #1247 |
#1247 …() and datetime.datetime.isoformat() #### Description *I tried using backports.datetime_fromisoformat but than the tox tests broke, probably because it required an import in the unit tests. I tried than just using the usual builtin datetime.datetime.fromisoformat and it worked just well.* *<Fixes: >* #### QA Steps *<Commands / tests / steps to run to verify that the change works:>* ``` tox -e py3 -- tests/unit/... ``` ``` tox -e integration -- tests/integration/... ``` All CI tests need to pass. *<Please note that most likely an additional test will be required by the reviewers for any change that's not a one liner to land.>* #### Notes & Discussion *This is a secondary branch from the Pull Request #1243 *
Description
This change was mentioned in the issue #1101 . There is just one change that is not simply replacing, and that one is the generation of the rfc3339, but I created a simple function to handle this conversion.
\Replaced old pyrfc3339 that has not received an updated for the last 6 years and is not typed to use ciso8601 instead
QA Steps
<Commands / tests / steps to run to verify that the change works:>
All CI tests need to pass.
<Please note that most likely an additional test will be required by the reviewers for any change that's not a one liner to land.>
Notes & Discussion
Should I also write the tests for this one? Like assert pyrfc3339.parse(timestamp) == ciso8601.parse_rfc339(timestamp) and assert pyrfc3339.generate(datetime) == generate_rfc3339_from_unix_time(datetime)?