Skip to content

Commit 9926ad4

Browse files
committed
fix
1 parent cea7d2d commit 9926ad4

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

mypy-baseline.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
posthog/utils.py:0: error: Library stubs not installed for "six" [import-untyped]
22
posthog/utils.py:0: error: Library stubs not installed for "dateutil.tz" [import-untyped]
33
posthog/utils.py:0: error: Statement is unreachable [unreachable]
4-
posthog/utils.py:0: error: Argument 1 to "join" of "str" has incompatible type "AttributeError"; expected "Iterable[str]" [arg-type]
54
posthog/request.py:0: error: Library stubs not installed for "requests" [import-untyped]
65
posthog/request.py:0: note: Hint: "python3 -m pip install types-requests"
76
posthog/request.py:0: error: Library stubs not installed for "dateutil.tz" [import-untyped]
@@ -38,4 +37,4 @@ posthog/ai/utils.py:0: note: Perhaps you meant "typing.Any" instead of "any"?
3837
posthog/ai/utils.py:0: error: Function "builtins.any" is not valid as a type [valid-type]
3938
posthog/ai/utils.py:0: note: Perhaps you meant "typing.Any" instead of "any"?
4039
sentry_django_example/sentry_django_example/settings.py:0: error: Need type annotation for "ALLOWED_HOSTS" (hint: "ALLOWED_HOSTS: list[<type>] = ...") [var-annotated]
41-
sentry_django_example/sentry_django_example/settings.py:0: error: Incompatible types in assignment (expression has type "str", variable has type "None") [assignment]
40+
sentry_django_example/sentry_django_example/settings.py:0: error: Incompatible types in assignment (expression has type "str", variable has type "None") [assignment]

posthog/utils.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from dataclasses import asdict, is_dataclass
66
from datetime import date, datetime, timezone
77
from decimal import Decimal
8+
from typing import Any, Optional
89
from uuid import UUID
910

1011
import six
@@ -99,11 +100,21 @@ def _clean_dataclass(dataclass_):
99100
return data
100101

101102

102-
def _coerce_unicode(cmplx):
103+
def _coerce_unicode(cmplx: Any) -> Optional[str]:
104+
"""
105+
In theory, this method is only called
106+
after many isinstance checks are carried out in `utils.clean`.
107+
When we supported Python 2 it was safe to call `decode` on a `str`
108+
but in Python 3 that will throw.
109+
So, we check if the input is bytes and only call `decode` in that case
110+
"""
103111
try:
104-
item = cmplx.decode("utf-8", "strict")
105-
except AttributeError as exception:
106-
item = ":".join(exception)
112+
if isinstance(cmplx, bytes):
113+
item = cmplx.decode("utf-8", "strict")
114+
else:
115+
item = str(cmplx)
116+
except Exception as exception:
117+
item = ":".join(map(str, exception.args))
107118
log.warning("Error decoding: %s", item)
108119
return None
109120
return item

0 commit comments

Comments
 (0)