-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
gh-81148: Eliminate unnecessary check in _strptime when determining AM/PM #13428
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this contribution @GPHemsley, I think it's a definite improvement.
I've left two comments on the tests. I think adding the special case tests is important. Moving your ampm tests into datetimetester
is more of a "nice to have" (I have not looked into how difficult it would be, but I'm pretty sure there are strptime
tests in datetimetester
already, so it's probably pretty easy).
Thanks!
@pganssle I have updated the tests. Let me know if that is sufficient. |
I disagree here. The behavior we want to test is the user-facing behavior, which is the one invoked by calling |
@pganssle I'm not sure if this is what you were asking me to do, but I've now added a similar test to |
@GPHemsley Thank you for adding the tests in I am aware that a lot of |
1771b9f
to
644aa88
Compare
@pganssle Removed test from Note that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@GPHemsley Yeah, I agree, I am at a conference this weekend, but this came up in another context today, so when I get back home (and have a bit of time to recover from my jet lag), I'm going to open an issue on bpo to propose a strategy going forward with respect to the datetimetester / test_strptime split. Things are mildly complicated by the fact that I didn't fully appreciate the fact that _strptime
is in its own module because it's got some shared code between time
and datetime
(I grepped incorrectly for import _strptime
, when in fact timemodule.c
uses another mechanism to import it).
Anyway, developing a coherent strategy for how to organize the strptime
tests is way outside the scope of this PR, so I don't want that to block your contribution, for which I am grateful.
I have left one final comment suggesting a refactoring of the test, but otherwise this looks good to me.
Lib/test/datetimetester.py
Outdated
for hour in range(0, 24): | ||
with self.subTest(hour=hour): | ||
hour_date = (1999, 3, 17, hour, 44, 55, 2, 76, 0) | ||
self.assertEqual(self.theclass.strptime(_time.strftime("%I %p", hour_date), "%I %p").hour, hour) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is I think too long for PEP8. If I may suggest a refactoring for this function that I think makes it a bit cleaner:
def test_strptime_ampm(self):
dt = datetime(1999, 3, 17, 0, 44, 55, 2, 76, 0)
for hour in range(0, 24):
with self.subTest(hour=hour):
new_dt = dt.replace(hour=hour)
dt_str = new_dt.strftime("%I %p")
self.assertEqual(self.theclass.strptime(dt_str, "%I %p").hour,
hour)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I likely opted to ignore line length, since much of the rest of the file has PEP8 issues, but I can change that.
Out of curiosity, why do you feel that using dt.replace
is cleaner than generating a new datetime value directly each iteration?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity, why do you feel that using
dt.replace
is cleaner than generating a new datetime value directly each iteration?
Technically replace
does generate a new value every iteration (datetime
is immutable), but I get what you mean. I mainly thought it was more readable because:
- I find the
time.strftime
interface to be a bit more "low level" and harder to read than anstrftime
call on adatetime
object. - Using
replace(hour=hour)
makes it more obvious that we didn't have any sort of off-by-one error when constructing the time tuple - it would be hard to notice in a tuple if you were actually cycling through different values for the minute instead of hour.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
@vstinner @pablogsal Do you think this rises to the level of needing a NEWS entry? It's a pretty minor refactoring so I added "skip news", but if you think it would be better to add a changelog I will remove the label and add that to the requested changes. Other than the one little requested change and possibly the news entry, I think this is ready to merge. |
If the change doesn't modify datetime behavior, there is no need to annoy users reading the Changelog. "skip news" is correct here. |
644aa88
to
ade347d
Compare
@pganssle, this old PR is marked as "awaiting changes", but I'm not sure it actually is. Is this something you'd still be interested in reviewing, and potentially merging? |
@pganssle Is the only change left to do to the test? I don't the person who originally opened this pr has been active recently, I can finish this off :-) |
I have made the requested changes; please review again |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated and merged @pganssle's Requested Changes (with minor fixup).
LGTM
This also adds additional tests to improve the coverage in this area.
https://bugs.python.org/issue36967