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

Breaking parser - june 15, 1987 #770

Closed
FluffyDietEngine opened this issue Nov 17, 2023 · 3 comments
Closed

Breaking parser - june 15, 1987 #770

FluffyDietEngine opened this issue Nov 17, 2023 · 3 comments

Comments

@FluffyDietEngine
Copy link

  • [ x ] I am on the latest Pendulum version.

  • [ x ] I have searched the issues of this repo and believe that this is not a duplicate.

  • OS version and name: Mac Sonoma 14

  • Pendulum version: 2.1.2

Issue

Pendulum parser is breaking with the mentioned date format -> "june 15, 1987"

Traceback

>>> from pendulum import parse as p_parse
>>> p_parse(dt)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/santhoshsolomon/Desktop/venv/lib/python3.11/site-packages/pendulum/parser.py", line 29, in parse
    return _parse(text, **options)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/santhoshsolomon/Desktop/venv/lib/python3.11/site-packages/pendulum/parser.py", line 45, in _parse
    parsed = base_parse(text, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/santhoshsolomon/Desktop/venv/lib/python3.11/site-packages/pendulum/parsing/__init__.py", line 74, in parse
    return _normalize(_parse(text, **_options), **_options)
                      ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/santhoshsolomon/Desktop/venv/lib/python3.11/site-packages/pendulum/parsing/__init__.py", line 128, in _parse
    raise ParserError("Unable to parse string [{}]".format(text))
pendulum.parsing.exceptions.ParserError: Unable to parse string [june 15,1987]

Other dateparsers like python-dateutils, dateparser are able to parse, but still there are wrong data.
Reference - dateutil/dateutil#1321

@mattsta
Copy link

mattsta commented Nov 25, 2023

This is working as-designed (because the input format isn't specific enough) but there's a workaround by just using strict=False:

In [7]: pendulum.parse("june 15,1987")
ParserError: Unable to parse string [june 15, 1987]

In [8]: pendulum.parse("june 15,1987", strict=False)
Out[8]: DateTime(1987, 6, 15, 0, 0, 0, tzinfo=Timezone('UTC'))

@FluffyDietEngine
Copy link
Author

@mattsta Thanks for the workaround. But can you help me understand what side effects strict=False can cause if I include this in a underlying code, which acts as a universal date converter in my whole application?

@mattsta
Copy link

mattsta commented Dec 1, 2023

🌟 https://pendulum.eustace.io/docs/#parsing 🌟

If you pass a non-standard or more complicated string, it will raise an exception, so it is advised to use the from_format() helper instead.

However, if you want the library to fall back on the dateutil parser, you have to pass strict=False.

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

No branches or pull requests

3 participants