mchaput / whoosh

Pure-Python full-text search library
Other
569 stars 69 forks source link

`DateParserPlugin` doesn't set default for `basedate` #50

Open CodeOptimist opened 9 months ago

CodeOptimist commented 9 months ago

Although DateParserPlugin.__init__ states:

        :param basedate: a datetime object representing the current time
            against which to measure relative dates. If you do not supply this
            argument, the plugin uses ``datetime.utcnow()``.

and is likely referring to both ParserBase.date_from() and DateParse.date_from() that assume a default of datetime.utcnow() when their own basedate parameter is None, other functions called within DateParserPlugin, like whoosh.util.times.timespan.disambiguated() require a non-None value.

As such, submitting a date range query like [oct 1970 to dec 8 19z70] (intentional typo), throws:

  File "...\whoosh\util\times.py", line 334, in disambiguated
    end.year = max(start.year, basedate.year)
                               ^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'year'

Proposed fix

I may have missed something, but as both date_from() are only called within dateparse.py from what I can tell, it might make sense to remove

        if basedate is None:
            basedate = datetime.utcnow()

and =None parameter defaults and simply set within DateParserPlugin.__init__ as follows?

        self.basedate = basedate or datetime.utcnow()

Collecting these issues for now, will fix in a fork eventually.