diff --git a/mypy-baseline.txt b/mypy-baseline.txt index 04ecc768..005f3f6b 100644 --- a/mypy-baseline.txt +++ b/mypy-baseline.txt @@ -4,7 +4,6 @@ posthog/request.py:0: note: (or run "mypy --install-types" to install all missin posthog/request.py:0: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports posthog/request.py:0: error: Incompatible types in assignment (expression has type "bytes", variable has type "str") [assignment] posthog/consumer.py:0: error: Name "Empty" already defined (possibly by an import) [no-redef] -posthog/consumer.py:0: error: Need type annotation for "items" (hint: "items: list[] = ...") [var-annotated] posthog/consumer.py:0: error: Unsupported operand types for <= ("int" and "str") [operator] posthog/consumer.py:0: note: Right operand is of type "int | str" posthog/consumer.py:0: error: Unsupported operand types for < ("str" and "int") [operator] diff --git a/posthog/consumer.py b/posthog/consumer.py index 8f3fa3b5..6a9becbe 100644 --- a/posthog/consumer.py +++ b/posthog/consumer.py @@ -1,3 +1,4 @@ +from typing import Any import json import logging import time @@ -96,7 +97,7 @@ def upload(self): def next(self): """Return the next batch of items to upload.""" queue = self.queue - items = [] + items: list[Any] = [] start_time = time.monotonic() total_size = 0 diff --git a/posthog/utils.py b/posthog/utils.py index c77236d6..8080ab90 100644 --- a/posthog/utils.py +++ b/posthog/utils.py @@ -5,7 +5,7 @@ import time from collections import defaultdict from dataclasses import asdict, is_dataclass -from datetime import date, datetime, timezone +from datetime import date, datetime, timezone, timedelta from decimal import Decimal from typing import Any, Optional from uuid import UUID @@ -16,18 +16,18 @@ log = logging.getLogger("posthog") -def is_naive(dt): +def is_naive(dt: datetime) -> bool: """Determines if a given datetime.datetime is naive.""" return dt.tzinfo is None or dt.tzinfo.utcoffset(dt) is None # pragma: no mutate -def total_seconds(delta): - """Determines total seconds with python < 2.7 compat.""" +def total_seconds(delta: timedelta) -> float: + """Return the total number of seconds contained in the duration.""" # http://stackoverflow.com/questions/3694835/python-2-6-5-divide-timedelta-with-timedelta return (delta.microseconds + (delta.seconds + delta.days * 24 * 3600) * 1e6) / 1e6 -def guess_timezone(dt): +def guess_timezone(dt: datetime) -> datetime: """Attempts to convert a naive datetime to an aware datetime.""" if is_naive(dt): # attempts to guess the datetime.datetime.now() local timezone @@ -44,7 +44,7 @@ def guess_timezone(dt): return dt -def remove_trailing_slash(host): +def remove_trailing_slash(host: str) -> str: if host.endswith("/"): return host[:-1] return host