Skip to content

bpo-34903: For datetime.datetime.strptime(), the leading zero for some two-digit formats is optional #9747

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

Closed
wants to merge 11 commits into from

Conversation

mikegleen
Copy link
Contributor

@mikegleen mikegleen commented Oct 7, 2018

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Unfortunately we couldn't find an account corresponding to your GitHub username on bugs.python.org (b.p.o) to verify you have signed the CLA (this might be simply due to a missing "GitHub Name" entry in your b.p.o account settings). This is necessary for legal reasons before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

You can check yourself to see if the CLA has been received.

Thanks again for your contribution, we look forward to reviewing it!

@bedevere-bot bedevere-bot added the docs Documentation in the Doc dir label Oct 7, 2018
@mikegleen
Copy link
Contributor Author

mikegleen commented Oct 7, 2018

Sorry for being so lame at creating this pull request. The title should be:
bpo-34903: For datetime.datetime.strptime, the leading zero for some two-digit formats is optional.
Test code:
import datetime
import sys
import unittest

class TestStrptime(unittest.TestCase):
#print(sys.version)

def test_y(self):
    # print('test %y requires leading zero ')
    with self.assertRaises(ValueError):
        newdate = datetime.datetime.strptime('1/2/3 4:5:6', '%d/%m/%y %H:%M:%S')

def test_dmyHMS(self):
    # print('test %d %m %y %H %M %S ')
    newdate = datetime.datetime.strptime('1/2/03 4:5:6', '%d/%m/%y %H:%M:%S')
    self.assertEqual('2003-02-01T04:05:06', newdate.isoformat(timespec='seconds'))

def test_Ij(self):
    # print('test %I %j')
    newdate = datetime.datetime.strptime('2/03 4am:5:6', '%j/%y %I%p:%M:%S')
    self.assertEqual('2003-01-02T04:05:06', newdate.isoformat(timespec='seconds'))

def test_U(self):
    # print('test %U')
    newdate = datetime.datetime.strptime('6/4/03', '%w/%U/%y')
    self.assertEqual('2003-02-01T00:00:00', newdate.isoformat(timespec='seconds'))

def test_W(self):
    # print('test %U')
    newdate = datetime.datetime.strptime('6/4/03', '%w/%W/%y')
    self.assertEqual('2003-02-01T00:00:00', newdate.isoformat(timespec='seconds'))

def test_V(self):
    # print('test %V')
    newdate = datetime.datetime.strptime('6/4/2003', '%u/%V/%G')
    self.assertEqual('2003-01-25T00:00:00', newdate.isoformat(timespec='seconds'))

def show():
print('test %d %m %y %H %M %S ')
print(datetime.datetime.strptime('1/2/03 4:5:6', '%d/%m/%y %H:%M:%S'))
print('test %I %j')
print(datetime.datetime.strptime('2/03 4am:5:6', '%j/%y %I%p:%M:%S'))
print('test %U')
print(datetime.datetime.strptime('6/4/03', '%w/%U/%y'))
print('test %W')
print(datetime.datetime.strptime('6/4/03', '%w/%W/%y'))
print('test %V')
print(datetime.datetime.strptime('6/04/2003', '%u/%V/%G'))

if name == 'main':
ver = sys.version_info
if ver.major != 3 or ver.minor < 6:
print(f'Requires at least 3.6, got {sys.version}')
sys.exit()
unittest.main()

@willingc
Copy link
Contributor

willingc commented Oct 8, 2018

@mikegleen You should be able to edit the title on the PR by clicking the Edit button to the right of the title. Please submit the CLA if you haven't yet done so. Thanks.

@mikegleen mikegleen changed the title ``` bpo-34903: For datetime.datetime.strptime, the leading zero for some two-digit formats is optional. Oct 8, 2018
@mikegleen
Copy link
Contributor Author

mikegleen commented Oct 8, 2018 via email

@willingc
Copy link
Contributor

willingc commented Oct 8, 2018

Thanks for the update @mikegleen. No worries on the button (IMHO the edit button is a bit to far to the right in the GitHub UI). 🍪

@augustogoulart
Copy link
Contributor

augustogoulart commented Oct 8, 2018

@mikegleen It would be interesting to have the tests you commented out on this thread added to your PR. Reach me out If you have any questions about that. Thanks!

@mikegleen
Copy link
Contributor Author

mikegleen commented Oct 9, 2018 via email

@augustogoulart
Copy link
Contributor

@mikegleen to make sure your tests are effective, they must be included in the Python test suite. Take a look at /Lib/test and see where you can fit them.
To accomplish that, add your tests to the test file you found, commit the changes and push the new commit to your pull request.

@mikegleen
Copy link
Contributor Author

mikegleen commented Oct 23, 2018 via email

@willingc
Copy link
Contributor

Hi @mikegleen,

Have you filled out the CLA?

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Unfortunately we couldn't find an account corresponding to your GitHub username on bugs.python.org (b.p.o) to verify you have signed the CLA (this might be simply due to a missing "GitHub Name" entry in your b.p.o account settings). This is necessary for legal reasons before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

You can check yourself to see if the CLA has been received.

@brettcannon brettcannon changed the title bpo-34903: For datetime.datetime.strptime, the leading zero for some two-digit formats is optional. bpo-34903: For datetime.datetime.strptime(), the leading zero for some two-digit formats is optional Mar 29, 2019
@brettcannon
Copy link
Member

@mikegleen could you add the test code to the PR?

@mikegleen
Copy link
Contributor Author

When Gus Goulart initially mentioned including the test code, I checked datetimetester.py and saw that single digit dates were already tested. I replied about this and did not proceed any further. But now, looking again, I see that the testing is somewhat incidental and it would be better to have a well-defined test of this issue.

So I suggest that I create a separate PR for datetimetester.py, as so much time has passed since the original PR. Are you ok with this?

Mike Gleen

@brettcannon
Copy link
Member

@mikegleen Up to you. No one will take issue if you add it to this one or create a separate PR. If you do a separate PR then please reference it here so people know where to look for the tests. And I'll leave a Changes Requests review so no one loses track of needing the tests.

Copy link
Member

@brettcannon brettcannon left a comment

Choose a reason for hiding this comment

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

Please add tests.

@bedevere-bot
Copy link

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 I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be poked with soft cushions!

by strptime. This test pairs with PR 9747 which updates the datetime
doc. The corresponding bug report is bpo-34903.
@mikegleen
Copy link
Contributor Author

I've updated datetimetester.py but the pull request failed because (I think) the existing PR (9747) also included the changes to the doc file. So I think I should close 9747 and then again create a PR which will now correctly include changes to both files.

But I thought I should check first. Does this sound right?

@mikegleen
Copy link
Contributor Author

I have made the requested changes; please review again.

@bedevere-bot
Copy link

Thanks for making the requested changes!

@brettcannon: please review the changes made to this pull request.

@brettcannon brettcannon self-requested a review April 11, 2019 21:14
@@ -2425,6 +2425,22 @@ def test_strptime(self):
self.assertEqual(expected, got)

strptime = self.theclass.strptime

Copy link
Member

Choose a reason for hiding this comment

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

There's actually a https://github.com/python/cpython/blob/master/Lib/test/test_strptime.py file for tests relating to strptime.

Copy link
Member

Choose a reason for hiding this comment

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

I am not a huge fan of the test_strptime thing, which tests _strptime directly rather than testing the public API function(s) datetime.strptime and time.strptime.

The reasoning for this is that the tests in datetimetester.py run against the C and datetime implementations per PEP 399, and they are parametrized to be run against subclasses of datetime.

In this case, I think it's unlikely that these tests will be affected by either of these things, but I also don't think the cost is particularly high, so I lean in favor of testing in datetimetester.py rather than test_strptime.py unless there's a compelling reason to do otherwise.


# bpo-34903: Check that single digit dates and times are allowed.
with self.assertRaises(ValueError):
# %y does require two digits
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
# %y does require two digits
# %y does require two digits.

with self.assertRaises(ValueError):
# %y does require two digits
newdate = strptime('1/2/3 4:5:6', '%d/%m/%y %H:%M:%S')
inputs = [
Copy link
Member

Choose a reason for hiding this comment

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

Could you provide comments specifying which format option you're specifically testing? Or better yet, provide explicit tests for each format you're testing? (You can keep these real-world examples as well).

('6/4/2003', '%u/%V/%G', '2003-01-25T00:00:00')
]
for string, format, target in inputs:
newdate = strptime(string, format)
Copy link
Member

Choose a reason for hiding this comment

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

Can you use unittest.subTest()?

@bedevere-bot
Copy link

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 I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@mikegleen
Copy link
Contributor Author

mikegleen commented Jun 17, 2019

Replaced by #14149.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting changes docs Documentation in the Doc dir skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants