scrapinghub / dateparser

python parser for human readable dates
BSD 3-Clause "New" or "Revised" License
2.56k stars 465 forks source link

Parse settings do not accept subclass types #1115

Closed majikman111 closed 3 months ago

majikman111 commented 1 year ago

Using a settings dictionary when parsing a date results in an error if the provided object is a subclass of the type specified in the dateparser settings schema, such as when using a pendulum.DateTime object . So running

settings = {
    "RELATIVE_BASE": pendulum.now()
}

dateparser.parse("-15m", settings=settings)

will generate the error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/lib/python3.8/site-packages/dateparser/conf.py", line 92, in wrapper
    return f(*args, **kwargs)
  File "/lib/python3.8/site-packages/dateparser/__init__.py", line 58, in parse
    parser = DateDataParser(languages=languages, locales=locales,
  File "/lib/python3.8/site-packages/dateparser/conf.py", line 92, in wrapper
    return f(*args, **kwargs)
  File "/lib/python3.8/site-packages/dateparser/date.py", line 387, in __init__
    check_settings(settings)
  File "/lib/python3.8/site-packages/dateparser/conf.py", line 244, in check_settings
    raise SettingValidationError(
dateparser.conf.SettingValidationError: "RELATIVE_BASE" must be "datetime", not "DateTime".

Even though a pendulum.DateTime is a subclass of a datetime object. The check at this line https://github.com/scrapinghub/dateparser/blob/master/dateparser/conf.py#L243 should call isinstance instead of directly checking type equality to ensure subclassed objects are allowed in settings definitions. This is a regression from previous versions where pendulum.DateTime objects were allowed.

majikman111 commented 3 months ago

Resolved by #1223