ixc / python-edtf

MIT License
52 stars 19 forks source link

Parser raises unhandled AttributeError in parse_edtf #32

Closed mel-mason closed 2 months ago

mel-mason commented 5 years ago

The parser fails with an unhandled AttributeError on the following: '7 Jun-14 Jul 1916'. It looks like the issue is that the lower and upper bounds are both lists. I'm expecting EDTFParseExceptions, but not Attribute errors.

Code to replicate: (edtf=4.0.1, python = 3.6)

import edtf
parsed_date_string = edtf.text_to_edtf('7 Jun-14 Jul 1916') # 'uuuu-06-07/1916-07-14'
parsed_date = edtf.parse_edtf(parsed_date_string) # [PartialUnspecified: 'uuuu-06-07']/[Date: '1916-07-14']
lower = parsed_date.lower_strict()

Exception thrown:

AttributeError                            Traceback (most recent call last)
~/Code/silo_indexer/.venv/lib/python3.6/site-packages/edtf/parser/parser_classes.py in _strict_date(self, lean)
    350             try:
--> 351                 r = self.lower._strict_date(lean)
    352                 if r is None:

AttributeError: 'list' object has no attribute '_strict_date'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
<ipython-input-7-1bed0c797cc0> in <module>
      2 parsed_date_string = edtf.text_to_edtf('7 Jun-14 Jul 1916') # 'uuuu-06-07/1916-07-14'
      3 parsed_date = edtf.parse_edtf(parsed_date_string) # [PartialUnspecified: 'uuuu-06-07']/[Date: '1916-07-14']
----> 4 lower = parsed_date.lower_strict()

~/Code/silo_indexer/.venv/lib/python3.6/site-packages/edtf/parser/parser_classes.py in lower_strict(self)
    126 
    127     def lower_strict(self):
--> 128         return self._strict_date(lean=EARLIEST)
    129 
    130     def upper_strict(self):

~/Code/silo_indexer/.venv/lib/python3.6/site-packages/edtf/parser/parser_classes.py in _strict_date(self, lean)
    354                 return r
    355             except AttributeError: # it's a string, or no date. Result depends on the upper date
--> 356                 upper = self.upper._strict_date(LATEST)
    357                 return apply_delta(sub, upper, appsettings.DELTA_IF_UNKNOWN)
    358         else:

AttributeError: 'list' object has no attribute '_strict_date'
ColeDCrawford commented 5 months ago

This should be fixed in the next v5 release:

>>> parsed_date_string = text_to_edtf('7 Jun-14 Jul 1916')
>>> parsed_date_string
'XXXX-06-07/1916-07-14'
>>> parsed_date = parse_edtf(parsed_date_string)
>>> parsed_date
Level2Interval: 'XXXX-06-07/1916-07-14'
>>> parsed_date.lower_strict()
time.struct_time(tm_year=0, tm_mon=6, tm_mday=7, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=0, tm_isdst=-1)
aweakley commented 2 months ago

This is resolved by #56