Skip to content

Split a separate "no-spaces" parser setting off of the current "absolute-time" one #751

Closed

Description

@skyuchukov-bp

Summary

Currently, including the absolute-time entry into the PARSERS array in settings actually results in trying to parse the datestring by two distinct parsers under the hood.

This issue proposes to split off the two parsers into their own separate entries of PARSERS, so the user can tune their usage and precedence to her liking.

Example use-case

EDIT: Seem this use-case is already discussed in a separate issue: #356
One specific use-case that can be brought to light is when parsing dates in strict mode and encountering strings with only a year in them:

>>> import dateparser
>>> dateparser.parse('2016')
datetime.datetime(2016, 7, 23, 0, 0)
>>> dateparser.parse('2016', settings={ 'STRICT_PARSING': True })
datetime.datetime(2020, 1, 6, 0, 0)

Obviously, the chance of someone writing "2016" with the intent to mean "Jan 06, 2020" is pretty low. Disabling the "no spaces" parser is one way to circumvent such behavior. More granular settings for the particular parser might be needed if one needs some of its other functionalities (feel free to file a separate issue for that).

Currently, a workaround for disabling the "no spaces" parser can be monkey-patching dateparser.parser.no_space_parser_eligibile to always return False, but as we know, monkey-patching is generally not the best thing in the world. (Credits to @JohannesBuchner for proposing a similar workaround for a different issue in this comment)

Naming ambiguities

The actual setting name for the "no-spaces" parser needs further consideration, as this parser does not correspond exactly to the cases of strings without any (white)spaces. Formally, looking at the current no_space_parser_eligibile implementation this parser handles:

  • strings with only digits (no other symbols whatsoever - spaces, dots, colons, etc)
  • strings for which the first non-digit symbol is : (this is rather strange, as "11.05:2020" will not be handled, but the nonsense string of "11:05.2020" is parsed to datetime.datetime(2005, 1, 1, 0, 0))

The result is that a proper datestring like "11.05.2020", which does not have spaces, will not be handled by the current "no spaces" parser.

Activity

  1. noviluni commented on Jul 23, 2020

    @noviluni
    Contributor

    Hi @skyuchukov-bp , thank you for the proposal!

    I think that you are right, we should separate those parsers to avoid those situations.

    I didn't have time to check the code carefully to see how feasible is this, but when having a moment I will give it a try and I will also check the weird behavior you described in the "Naming ambiguities" section.

    Of course, if you want to open a PR in the meanwhile don't hesitate to do it 😄

    Thanks again for raising this up!


    related issue: #713
    related PR: #715

  2. added this to the v1.0.0 milestone on Jul 23, 2020
  3. skyuchukov-bp commented on Jul 23, 2020

    @skyuchukov-bp
    Author

    Thanks for the fast reply @noviluni.

    After checking the v1.0.0 milestone you've added, I've actually discovered that the "example use-case" described here is already discussed in another issue: #356. I will update the info above with that.

    Nevertheless, having the ability to separately use the 2 parses should still have other valid uses-cases and is not directly tied to the year parsing issue.

  4. cfculhane commented on Aug 11, 2020

    @cfculhane

    Thanks for this, the STRICT_PARSER=True parsing of "2017" -> datetime.datetime(2017, 8, 11, 0, 0) is driving me crazy :)

  5. noviluni commented on Sep 16, 2020

    @noviluni
    Contributor

    Hi @skyuchukov-bp , I added a PR separating the "no_spaces" parser: #786

    If you have the chance, it would be really nice if you check it.

    From now, you could do:

    from dateparser_data.settings import default_parsers
    
    parsers = [parser for parser in default_parsers if parser != 'no-spaces-time']
    
    dateparser.parse('2017', settings={'STRICT_PARSING': True, 'PARSERS': parsers})

    And it will return None.

    @cfculhane you could also check if this works for you :)

    Thanks in advance!

  6. noviluni commented on Sep 16, 2020

    @noviluni
    Contributor

    Ah @skyuchukov-bp !

    And if you have any idea for the naming ambiguities it will be welcomed!

  7. skyuchukov-bp commented on Sep 17, 2020

    @skyuchukov-bp
    Author

    Thanks @noviluni! I've glanced over the PR and added some questions/comments.
    As far as the naming goes - not sure there's something short enough to describes the parser - probably something like digits-and-colon-only-parser (which is still not entirely true, as the parser allows other non-digit symbols, as long as the first one is a colon, but looking at the example above it probably ignores these parts - need to dig deeper in the patterns to verify that).

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions