Skip to content

Commit 2ccba0a

Browse files
committed
Fixed None Error when using invalid constant in absolute prepositions absolute part
1 parent c376a9d commit 2ccba0a

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

datetimeparser/parsermethods.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ def _concatenate_relative_data(self, relative_data_tokens: List[Union[int, Const
790790

791791
return returned_data
792792

793-
def _parse_absolute_statement(self, data: Union[str, Tuple]):
793+
def _parse_absolute_statement(self, data: Union[str, Tuple]) -> Optional:
794794
"""
795795
Parses constant values like "christmas", "previous st. patricks day", ..
796796
If the input is a Tuple containing the absolute preposition tokens, we return the result of`_convert_tokens`
@@ -815,13 +815,15 @@ def _parse_absolute_statement(self, data: Union[str, Tuple]):
815815
# If the result is None there may be just a normal year (e.g. "2018")
816816
if data.isnumeric():
817817
return (AbsoluteDateTime(year=int(data)),)
818+
else:
819+
return None
818820
else:
819821
# The first element is the Method signature (Method.CONSTANTS)
820822
return result[1]
821823
else:
822824
return self._convert_tokens(data)
823825

824-
def _convert_tokens(self, tokens: Tuple[dict, dict, dict]) -> List[Union[int, Constant, RelativeDateTime]]:
826+
def _convert_tokens(self, tokens: Tuple[dict, dict, dict]) -> Optional[List[Union[int, Constant, RelativeDateTime]]]:
825827
"""
826828
Converts and parses the splitted tokens from _split_data into Tokens suited for the Evaluator
827829
@@ -854,7 +856,12 @@ def _convert_tokens(self, tokens: Tuple[dict, dict, dict]) -> List[Union[int, Co
854856
else:
855857
raise ValueError("No preposition given")
856858
elif data_part["type"] == "absolute":
857-
for absolute_part_data in self._parse_absolute_statement(data_part["data"]):
859+
absolute_data = self._parse_absolute_statement(data_part["data"])
860+
861+
if absolute_data is None:
862+
return None
863+
864+
for absolute_part_data in absolute_data:
858865
returned_data.append(absolute_part_data)
859866

860867
return returned_data
@@ -875,4 +882,7 @@ def parse(self, string: str) -> Optional[Tuple[MethodEnum, List[Union[int, Const
875882

876883
data = self._convert_tokens(prepared_data)
877884

885+
if data is None:
886+
return None
887+
878888
return Method.ABSOLUTE_PREPOSITIONS, data

0 commit comments

Comments
 (0)