dateparser icon indicating copy to clipboard operation
dateparser copied to clipboard

Parse settings do not accept subclass types

Open majikman111 opened this issue 1 year ago • 0 comments

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 avatar Jan 05 '23 17:01 majikman111