More cleanup.
[python_utils.git] / dateparse / dateparse_utils.py
index 7ca3cf3123a2a460c375a2ef811575d656c0d572..6ba647c847e48931017292947bfe36cb423f772f 100755 (executable)
@@ -1,4 +1,5 @@
 #!/usr/bin/env python3
+# type: ignore
 
 """
 Parse dates in a variety of formats.
@@ -65,9 +66,7 @@ class RaisingErrorListener(antlr4.DiagnosticErrorListener):
     def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
         raise ParseException(msg)
 
-    def reportAmbiguity(
-        self, recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs
-    ):
+    def reportAmbiguity(self, recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs):
         pass
 
     def reportAttemptingFullContext(
@@ -75,9 +74,7 @@ class RaisingErrorListener(antlr4.DiagnosticErrorListener):
     ):
         pass
 
-    def reportContextSensitivity(
-        self, recognizer, dfa, startIndex, stopIndex, prediction, configs
-    ):
+    def reportContextSensitivity(self, recognizer, dfa, startIndex, stopIndex, prediction, configs):
         pass
 
 
@@ -435,13 +432,20 @@ class DateParser(dateparse_utilsListener):
         micros = self.time.microsecond
 
         self.datetime = datetime.datetime(
-            year, month, day, hour, minute, second, micros, tzinfo=self.time.tzinfo
+            year,
+            month,
+            day,
+            hour,
+            minute,
+            second,
+            micros,
+            tzinfo=self.time.tzinfo,
         )
 
         # Apply resudual adjustments to times here when we have a
         # datetime.
         self.datetime = self.datetime + self.timedelta
-        assert self.datetime
+        assert self.datetime is not None
         self.time = datetime.time(
             self.datetime.hour,
             self.datetime.minute,
@@ -550,9 +554,7 @@ class DateParser(dateparse_utilsListener):
             else:
                 raise ParseException(f'Invalid Unit: "{unit}"')
 
-    def exitDeltaPlusMinusExpr(
-        self, ctx: dateparse_utilsParser.DeltaPlusMinusExprContext
-    ) -> None:
+    def exitDeltaPlusMinusExpr(self, ctx: dateparse_utilsParser.DeltaPlusMinusExprContext) -> None:
         try:
             n = ctx.nth()
             if n is None:
@@ -574,17 +576,13 @@ class DateParser(dateparse_utilsListener):
         else:
             self.context['delta_unit'] = unit
 
-    def exitDeltaNextLast(
-        self, ctx: dateparse_utilsParser.DeltaNextLastContext
-    ) -> None:
+    def exitDeltaNextLast(self, ctx: dateparse_utilsParser.DeltaNextLastContext) -> None:
         try:
             txt = ctx.getText().lower()
         except Exception:
             raise ParseException(f'Bad next/last: {ctx.getText()}')
         if 'month' in self.context or 'day' in self.context or 'year' in self.context:
-            raise ParseException(
-                'Next/last expression expected to be relative to today.'
-            )
+            raise ParseException('Next/last expression expected to be relative to today.')
         if txt[:4] == 'next':
             self.context['delta_int'] = +1
             self.context['day'] = self.now_datetime.day
@@ -613,9 +611,7 @@ class DateParser(dateparse_utilsListener):
         if 'time_delta_before_after' not in self.context:
             raise ParseException(f'Bad Before/After: {ctx.getText()}')
 
-    def exitDeltaTimeFraction(
-        self, ctx: dateparse_utilsParser.DeltaTimeFractionContext
-    ) -> None:
+    def exitDeltaTimeFraction(self, ctx: dateparse_utilsParser.DeltaTimeFractionContext) -> None:
         try:
             txt = ctx.getText().lower()[:4]
             if txt == 'quar':
@@ -629,9 +625,7 @@ class DateParser(dateparse_utilsListener):
         except Exception:
             raise ParseException(f'Bad time fraction {ctx.getText()}')
 
-    def exitDeltaBeforeAfter(
-        self, ctx: dateparse_utilsParser.DeltaBeforeAfterContext
-    ) -> None:
+    def exitDeltaBeforeAfter(self, ctx: dateparse_utilsParser.DeltaBeforeAfterContext) -> None:
         try:
             txt = ctx.getText().lower()
         except Exception:
@@ -639,9 +633,7 @@ class DateParser(dateparse_utilsListener):
         else:
             self.context['delta_before_after'] = txt
 
-    def exitDeltaTimeBeforeAfter(
-        self, ctx: dateparse_utilsParser.DeltaBeforeAfterContext
-    ) -> None:
+    def exitDeltaTimeBeforeAfter(self, ctx: dateparse_utilsParser.DeltaBeforeAfterContext) -> None:
         try:
             txt = ctx.getText().lower()
         except Exception:
@@ -803,9 +795,7 @@ class DateParser(dateparse_utilsListener):
             special = ctx.specialDate().getText().lower()
             self.context['special'] = special
         except Exception:
-            raise ParseException(
-                f'Bad specialDate expression: {ctx.specialDate().getText()}'
-            )
+            raise ParseException(f'Bad specialDate expression: {ctx.specialDate().getText()}')
         try:
             mod = ctx.thisNextLast()
             if mod is not None:
@@ -889,9 +879,7 @@ class DateParser(dateparse_utilsListener):
         self.context['month'] = d.month
         self.context['day'] = d.day
 
-    def exitSpecialTimeExpr(
-        self, ctx: dateparse_utilsParser.SpecialTimeExprContext
-    ) -> None:
+    def exitSpecialTimeExpr(self, ctx: dateparse_utilsParser.SpecialTimeExprContext) -> None:
         try:
             txt = ctx.specialTime().getText().lower()
         except Exception:
@@ -916,9 +904,7 @@ class DateParser(dateparse_utilsListener):
         except Exception:
             pass
 
-    def exitTwelveHourTimeExpr(
-        self, ctx: dateparse_utilsParser.TwelveHourTimeExprContext
-    ) -> None:
+    def exitTwelveHourTimeExpr(self, ctx: dateparse_utilsParser.TwelveHourTimeExprContext) -> None:
         try:
             hour = ctx.hour().getText()
             while not hour[-1].isdigit():
@@ -1028,7 +1014,7 @@ def main() -> None:
             logger.exception(e)
             print("Unrecognized.")
         else:
-            assert dt
+            assert dt is not None
             print(dt.strftime('%A %Y/%m/%d %H:%M:%S.%f %Z(%z)'))
     sys.exit(0)