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

Empty string parses to today's date #78

Closed
ksonj opened this issue Apr 15, 2015 · 1 comment
Closed

Empty string parses to today's date #78

ksonj opened this issue Apr 15, 2015 · 1 comment
Assignees
Milestone

Comments

@ksonj
Copy link

ksonj commented Apr 15, 2015

Maybe this behavior is intended but it certainly violates the principle of least surprise.

dateutil.parser.parse('')
>>> datetime.datetime(2015, 4, 15, 0, 0)

Is this documented somewhere? I'd expect this to raise a ValueError.

@pganssle
Copy link
Member

@ksonj I'll double check the documentation, but this is basically the current expected behavior. The default parser behavior is that if an element is missing, it is replaced with the corresponding element from the default parameter. The default parameter is optional, and defaults to a timezone-naive datetime object representing midnight today's date.

Since an empty string contains no date elements, all elements are replaced with the values from the default element. The only time ValueError is raised is if the date string is malformed in some way.

I think that it might be a good idea to change this in 2.5.0, though. A date string containing no date likely qualifies as exceptional. I would think it's also particularly important when doing fuzzy date parsing, since there's no general and obvious way to tell whether the date returned came from the string or from the default, e.g.:

    >>> from dateutil.parser import parse
    >>> from datetime import datetime

    >>> dflt_date = datetime(2015, 03, 03)

    >>> parse('What a lovely day it was.', fuzzy=True, default=dflt_date)
    datetime.datetime(2015, 3, 3, 0, 0)

    >>> parse('What a lovely day it was on March 3rd.', fuzzy=True, default=dflt_date)
    datetime.datetime(2015, 3, 3, 0, 0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants