Skip to content

Commit 0579ca6

Browse files
pauldambraoliverb123
authored andcommitted
fix
1 parent b731128 commit 0579ca6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2380
-678
lines changed

.pre-commit-config.yaml

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
repos:
2-
- repo: https://github.com/psf/black
3-
rev: 25.1.0
4-
hooks:
5-
- id: black
6-
- repo: https://github.com/pycqa/isort
7-
rev: 6.0.1
8-
hooks:
9-
- id: isort
10-
- repo: https://github.com/pycqa/flake8
11-
rev: 7.2.0
12-
hooks:
13-
- id: flake8
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
# Ruff version.
4+
rev: v0.11.12
5+
hooks:
6+
# Run the linter.
7+
- id: ruff-check
8+
args: [ --fix ]
9+
# Run the formatter.
10+
- id: ruff-format

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
lint:
2-
pylint --rcfile=.pylintrc --reports=y --exit-zero analytics | tee pylint.out
3-
flake8 --max-complexity=10 --statistics analytics > flake8.out || true
2+
uvx ruff format
43

54
test:
65
coverage run -m pytest

example.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
# Add argument parsing
77
parser = argparse.ArgumentParser(description="PostHog Python library example")
88
parser.add_argument(
9-
"--flag", default="person-on-events-enabled", help="Feature flag key to check (default: person-on-events-enabled)"
9+
"--flag",
10+
default="person-on-events-enabled",
11+
help="Feature flag key to check (default: person-on-events-enabled)",
1012
)
1113
args = parser.parse_args()
1214

@@ -38,10 +40,19 @@
3840

3941

4042
# Capture an event
41-
posthog.capture("distinct_id", "event", {"property1": "value", "property2": "value"}, send_feature_flags=True)
43+
posthog.capture(
44+
"distinct_id",
45+
"event",
46+
{"property1": "value", "property2": "value"},
47+
send_feature_flags=True,
48+
)
4249

4350
print(posthog.feature_enabled("beta-feature", "distinct_id"))
44-
print(posthog.feature_enabled("beta-feature-groups", "distinct_id", groups={"company": "id:5"}))
51+
print(
52+
posthog.feature_enabled(
53+
"beta-feature-groups", "distinct_id", groups={"company": "id:5"}
54+
)
55+
)
4556

4657
print(posthog.feature_enabled("beta-feature", "distinct_id"))
4758

@@ -53,9 +64,14 @@
5364

5465
posthog.alias("distinct_id", "new_distinct_id")
5566

56-
posthog.capture("new_distinct_id", "event2", {"property1": "value", "property2": "value"})
5767
posthog.capture(
58-
"new_distinct_id", "event-with-groups", {"property1": "value", "property2": "value"}, groups={"company": "id:5"}
68+
"new_distinct_id", "event2", {"property1": "value", "property2": "value"}
69+
)
70+
posthog.capture(
71+
"new_distinct_id",
72+
"event-with-groups",
73+
{"property1": "value", "property2": "value"},
74+
groups={"company": "id:5"},
5975
)
6076

6177
# # Add properties to the person
@@ -82,7 +98,13 @@
8298
# Local Evaluation
8399

84100
# If flag has City=Sydney, this call doesn't go to `/decide`
85-
print(posthog.feature_enabled("test-flag", "distinct_id_random_22", person_properties={"$geoip_city_name": "Sydney"}))
101+
print(
102+
posthog.feature_enabled(
103+
"test-flag",
104+
"distinct_id_random_22",
105+
person_properties={"$geoip_city_name": "Sydney"},
106+
)
107+
)
86108

87109
print(
88110
posthog.feature_enabled(
@@ -98,7 +120,9 @@
98120
print(posthog.get_all_flags("distinct_id_random_22", only_evaluate_locally=True))
99121
print(
100122
posthog.get_all_flags(
101-
"distinct_id_random_22", person_properties={"$geoip_city_name": "Sydney"}, only_evaluate_locally=True
123+
"distinct_id_random_22",
124+
person_properties={"$geoip_city_name": "Sydney"},
125+
only_evaluate_locally=True,
102126
)
103127
)
104128
print(posthog.get_remote_config_payload("encrypted_payload_flag_key"))

posthog/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def capture_exception(
323323
timestamp=None, # type: Optional[datetime.datetime]
324324
uuid=None, # type: Optional[str]
325325
groups=None, # type: Optional[Dict]
326-
**kwargs
326+
**kwargs,
327327
):
328328
# type: (...) -> Tuple[bool, dict]
329329
"""
@@ -366,7 +366,7 @@ def capture_exception(
366366
timestamp=timestamp,
367367
uuid=uuid,
368368
groups=groups,
369-
**kwargs
369+
**kwargs,
370370
)
371371

372372

posthog/ai/anthropic/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
from .anthropic import Anthropic
22
from .anthropic_async import AsyncAnthropic
3-
from .anthropic_providers import AnthropicBedrock, AnthropicVertex, AsyncAnthropicBedrock, AsyncAnthropicVertex
3+
from .anthropic_providers import (
4+
AnthropicBedrock,
5+
AnthropicVertex,
6+
AsyncAnthropicBedrock,
7+
AsyncAnthropicVertex,
8+
)
49

510
__all__ = [
611
"Anthropic",

posthog/ai/anthropic/anthropic.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@
22
import anthropic
33
from anthropic.resources import Messages
44
except ImportError:
5-
raise ModuleNotFoundError("Please install the Anthropic SDK to use this feature: 'pip install anthropic'")
5+
raise ModuleNotFoundError(
6+
"Please install the Anthropic SDK to use this feature: 'pip install anthropic'"
7+
)
68

79
import time
810
import uuid
911
from typing import Any, Dict, Optional
1012

11-
from posthog.ai.utils import call_llm_and_track_usage, get_model_params, merge_system_prompt, with_privacy_mode
13+
from posthog.ai.utils import (
14+
call_llm_and_track_usage,
15+
get_model_params,
16+
merge_system_prompt,
17+
with_privacy_mode,
18+
)
1219
from posthog.client import Client as PostHogClient
1320

1421

@@ -186,8 +193,12 @@ def _capture_streaming_event(
186193
"$ai_http_status": 200,
187194
"$ai_input_tokens": usage_stats.get("input_tokens", 0),
188195
"$ai_output_tokens": usage_stats.get("output_tokens", 0),
189-
"$ai_cache_read_input_tokens": usage_stats.get("cache_read_input_tokens", 0),
190-
"$ai_cache_creation_input_tokens": usage_stats.get("cache_creation_input_tokens", 0),
196+
"$ai_cache_read_input_tokens": usage_stats.get(
197+
"cache_read_input_tokens", 0
198+
),
199+
"$ai_cache_creation_input_tokens": usage_stats.get(
200+
"cache_creation_input_tokens", 0
201+
),
191202
"$ai_latency": latency,
192203
"$ai_trace_id": posthog_trace_id,
193204
"$ai_base_url": str(self._client.base_url),

posthog/ai/anthropic/anthropic_async.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@
22
import anthropic
33
from anthropic.resources import AsyncMessages
44
except ImportError:
5-
raise ModuleNotFoundError("Please install the Anthropic SDK to use this feature: 'pip install anthropic'")
5+
raise ModuleNotFoundError(
6+
"Please install the Anthropic SDK to use this feature: 'pip install anthropic'"
7+
)
68

79
import time
810
import uuid
911
from typing import Any, Dict, Optional
1012

11-
from posthog.ai.utils import call_llm_and_track_usage_async, get_model_params, merge_system_prompt, with_privacy_mode
13+
from posthog.ai.utils import (
14+
call_llm_and_track_usage_async,
15+
get_model_params,
16+
merge_system_prompt,
17+
with_privacy_mode,
18+
)
1219
from posthog.client import Client as PostHogClient
1320

1421

@@ -186,8 +193,12 @@ async def _capture_streaming_event(
186193
"$ai_http_status": 200,
187194
"$ai_input_tokens": usage_stats.get("input_tokens", 0),
188195
"$ai_output_tokens": usage_stats.get("output_tokens", 0),
189-
"$ai_cache_read_input_tokens": usage_stats.get("cache_read_input_tokens", 0),
190-
"$ai_cache_creation_input_tokens": usage_stats.get("cache_creation_input_tokens", 0),
196+
"$ai_cache_read_input_tokens": usage_stats.get(
197+
"cache_read_input_tokens", 0
198+
),
199+
"$ai_cache_creation_input_tokens": usage_stats.get(
200+
"cache_creation_input_tokens", 0
201+
),
191202
"$ai_latency": latency,
192203
"$ai_trace_id": posthog_trace_id,
193204
"$ai_base_url": str(self._client.base_url),

posthog/ai/anthropic/anthropic_providers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
try:
22
import anthropic
33
except ImportError:
4-
raise ModuleNotFoundError("Please install the Anthropic SDK to use this feature: 'pip install anthropic'")
4+
raise ModuleNotFoundError(
5+
"Please install the Anthropic SDK to use this feature: 'pip install anthropic'"
6+
)
57

68
from posthog.ai.anthropic.anthropic import WrappedMessages
79
from posthog.ai.anthropic.anthropic_async import AsyncWrappedMessages

posthog/ai/gemini/gemini.py

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
try:
77
from google import genai
88
except ImportError:
9-
raise ModuleNotFoundError("Please install the Google Gemini SDK to use this feature: 'pip install google-genai'")
10-
11-
from posthog.ai.utils import call_llm_and_track_usage, get_model_params, with_privacy_mode
9+
raise ModuleNotFoundError(
10+
"Please install the Google Gemini SDK to use this feature: 'pip install google-genai'"
11+
)
12+
13+
from posthog.ai.utils import (
14+
call_llm_and_track_usage,
15+
get_model_params,
16+
with_privacy_mode,
17+
)
1218
from posthog.client import Client as PostHogClient
1319

1420

@@ -124,8 +130,16 @@ def _merge_posthog_params(
124130
):
125131
"""Merge call-level PostHog parameters with client defaults."""
126132
# Use call-level values if provided, otherwise fall back to defaults
127-
distinct_id = call_distinct_id if call_distinct_id is not None else self._default_distinct_id
128-
privacy_mode = call_privacy_mode if call_privacy_mode is not None else self._default_privacy_mode
133+
distinct_id = (
134+
call_distinct_id
135+
if call_distinct_id is not None
136+
else self._default_distinct_id
137+
)
138+
privacy_mode = (
139+
call_privacy_mode
140+
if call_privacy_mode is not None
141+
else self._default_privacy_mode
142+
)
129143
groups = call_groups if call_groups is not None else self._default_groups
130144

131145
# Merge properties: default properties + call properties (call properties override)
@@ -166,8 +180,14 @@ def generate_content(
166180
**kwargs: Arguments passed to Gemini's generate_content
167181
"""
168182
# Merge PostHog parameters
169-
distinct_id, trace_id, properties, privacy_mode, groups = self._merge_posthog_params(
170-
posthog_distinct_id, posthog_trace_id, posthog_properties, posthog_privacy_mode, posthog_groups
183+
distinct_id, trace_id, properties, privacy_mode, groups = (
184+
self._merge_posthog_params(
185+
posthog_distinct_id,
186+
posthog_trace_id,
187+
posthog_properties,
188+
posthog_privacy_mode,
189+
posthog_groups,
190+
)
171191
)
172192

173193
kwargs_with_contents = {"model": model, "contents": contents, **kwargs}
@@ -210,8 +230,12 @@ def generator():
210230
for chunk in response:
211231
if hasattr(chunk, "usage_metadata") and chunk.usage_metadata:
212232
usage_stats = {
213-
"input_tokens": getattr(chunk.usage_metadata, "prompt_token_count", 0),
214-
"output_tokens": getattr(chunk.usage_metadata, "candidates_token_count", 0),
233+
"input_tokens": getattr(
234+
chunk.usage_metadata, "prompt_token_count", 0
235+
),
236+
"output_tokens": getattr(
237+
chunk.usage_metadata, "candidates_token_count", 0
238+
),
215239
}
216240

217241
if hasattr(chunk, "text") and chunk.text:
@@ -320,8 +344,14 @@ def generate_content_stream(
320344
**kwargs: Any,
321345
):
322346
# Merge PostHog parameters
323-
distinct_id, trace_id, properties, privacy_mode, groups = self._merge_posthog_params(
324-
posthog_distinct_id, posthog_trace_id, posthog_properties, posthog_privacy_mode, posthog_groups
347+
distinct_id, trace_id, properties, privacy_mode, groups = (
348+
self._merge_posthog_params(
349+
posthog_distinct_id,
350+
posthog_trace_id,
351+
posthog_properties,
352+
posthog_privacy_mode,
353+
posthog_groups,
354+
)
325355
)
326356

327357
return self._generate_content_streaming(

0 commit comments

Comments
 (0)