Skip to content

Commit b6c89bc

Browse files
committed
Merge branch 'master' into dn-feat/setup-client
2 parents e0a7567 + 5d58a53 commit b6c89bc

Some content is hidden

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

61 files changed

+5209
-1340
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ jobs:
99
runs-on: ubuntu-latest
1010

1111
steps:
12-
- uses: actions/checkout@v2
12+
- uses: actions/checkout@85e6279cec87321a52edac9c87bce653a07cf6c2
1313
with:
1414
fetch-depth: 1
1515

1616
- name: Set up Python 3.11
17-
uses: actions/setup-python@v2
17+
uses: actions/setup-python@8d9ed9ac5c53483de85588cdf95a591a75ab9f55
1818
with:
1919
python-version: 3.11.11
2020

21-
- uses: actions/cache@v3
21+
- uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684
2222
with:
2323
path: ~/.cache/pip
24-
key: ${{ runner.os }}-pip-${{ hashFiles('setup.py') }}
24+
key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml') }}
2525
restore-keys: |
2626
${{ runner.os }}-pip-
2727
@@ -30,17 +30,9 @@ jobs:
3030
python -m pip install -e .[dev]
3131
if: steps.cache.outputs.cache-hit != 'true'
3232

33-
- name: Check formatting with black
33+
- name: Check formatting with ruff
3434
run: |
35-
black --check .
36-
37-
- name: Lint with flake8
38-
run: |
39-
flake8 posthog --ignore E501,W503
40-
41-
- name: Check import order with isort
42-
run: |
43-
isort --check-only .
35+
ruff format --check .
4436
4537
- name: Check types with mypy
4638
run: |
@@ -54,12 +46,12 @@ jobs:
5446
python-version: ['3.9', '3.10', '3.11', '3.12', '3.13']
5547

5648
steps:
57-
- uses: actions/checkout@v2
49+
- uses: actions/checkout@85e6279cec87321a52edac9c87bce653a07cf6c2
5850
with:
5951
fetch-depth: 1
6052

6153
- name: Set up Python ${{ matrix.python-version }}
62-
uses: actions/setup-python@v2
54+
uses: actions/setup-python@8d9ed9ac5c53483de85588cdf95a591a75ab9f55
6355
with:
6456
python-version: ${{ matrix.python-version }}
6557

.github/workflows/release.yaml

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,43 @@
1-
name: 'Release'
1+
name: "Release"
22

33
on:
4-
- workflow_dispatch
4+
push:
5+
branches:
6+
- master
7+
paths:
8+
- "posthog/version.py"
9+
workflow_dispatch:
510

611
jobs:
7-
release:
8-
name: Publish release
9-
runs-on: ubuntu-20.04
10-
env:
11-
TWINE_USERNAME: __token__
12-
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
13-
steps:
14-
- name: Checkout the repository
15-
uses: actions/checkout@v2
16-
with:
17-
fetch-depth: 0
18-
token: ${{ secrets.POSTHOG_BOT_GITHUB_TOKEN }}
12+
release:
13+
name: Publish release
14+
runs-on: ubuntu-latest
15+
env:
16+
TWINE_USERNAME: __token__
17+
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
18+
steps:
19+
- name: Checkout the repository
20+
uses: actions/checkout@85e6279cec87321a52edac9c87bce653a07cf6c2
21+
with:
22+
fetch-depth: 0
23+
token: ${{ secrets.POSTHOG_BOT_GITHUB_TOKEN }}
1924

20-
- name: Set up Python
21-
uses: actions/setup-python@v2
25+
- name: Set up Python
26+
uses: actions/setup-python@8d9ed9ac5c53483de85588cdf95a591a75ab9f55
2227

23-
- name: Detect version
24-
run: echo "REPO_VERSION=$(python3 posthog/version.py)" >> $GITHUB_ENV
28+
- name: Detect version
29+
run: echo "REPO_VERSION=$(python3 posthog/version.py)" >> $GITHUB_ENV
2530

26-
- name: Prepare for building release
27-
run: pip install -U pip setuptools wheel twine
31+
- name: Prepare for building release
32+
run: pip install -U pip setuptools packaging wheel twine
2833

29-
- name: Push release to PyPI
30-
run: make release && make release_analytics
34+
- name: Push release to PyPI
35+
run: make release && make release_analytics
3136

32-
- name: Create GitHub release
33-
uses: actions/create-release@v1
34-
env:
35-
GITHUB_TOKEN: ${{ secrets.POSTHOG_BOT_GITHUB_TOKEN }}
36-
with:
37-
tag_name: v${{ env.REPO_VERSION }}
38-
release_name: ${{ env.REPO_VERSION }}
37+
- name: Create GitHub release
38+
uses: actions/create-release@0cb9c9b65d5d1901c1f53e5e66eaf4afd303e70e # v1
39+
env:
40+
GITHUB_TOKEN: ${{ secrets.POSTHOG_BOT_GITHUB_TOKEN }}
41+
with:
42+
tag_name: v${{ env.REPO_VERSION }}
43+
release_name: ${{ env.REPO_VERSION }}

.pre-commit-config.yaml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
repos:
2-
- repo: https://github.com/psf/black
3-
rev: stable
4-
hooks:
5-
- id: black
6-
- repo: https://github.com/pycqa/isort
7-
rev: 5.7.0
8-
hooks:
9-
- id: isort
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

CHANGELOG.md

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,75 @@
1+
## 4.3.2 - 2025-06-06
2+
3+
Add context management:
4+
- New context manager with `posthog.new_context()`
5+
- Tag functions: `posthog.tag()`, `posthog.get_tags()`, `posthog.clear_tags()`
6+
- Function decorator:
7+
- `@posthog.scoped` - Creates context and captures exceptions thrown within the function
8+
- Automatic deduplication of exceptions to ensure each exception is only captured once
9+
10+
## 4.2.1 - 2025-6-05
11+
12+
1. fix: feature flag request use geoip_disable (#235)
13+
2. chore: pin actions versions (#210)
14+
3. fix: opinionated setup and clean fn fix (#240)
15+
4. fix: release action failed (#241)
16+
17+
## 4.2.0 - 2025-05-22
18+
19+
Add support for google gemini
20+
21+
## 4.1.0 - 2025-05-22
22+
23+
Moved ai openai package to a composition approach over inheritance.
24+
25+
## 4.0.1 – 2025-04-29
26+
27+
1. Remove deprecated `monotonic` library. Use Python's core `time.monotonic` function instead
28+
2. Clarify Python 3.9+ is required
29+
30+
## 4.0.0 - 2025-04-24
31+
32+
1. Added new method `get_feature_flag_result` which returns a `FeatureFlagResult` object. This object breaks down the result of a feature flag into its enabled state, variant, and payload. The benefit of this method is it allows you to retrieve the result of a feature flag and its payload in a single API call. You can call `get_value` on the result to get the value of the feature flag, which is the same value returned by `get_feature_flag` (aka the string `variant` if the flag is a multivariate flag or the `boolean` value if the flag is a boolean flag).
33+
34+
Example:
35+
36+
```python
37+
result = posthog.get_feature_flag_result("my-flag", "distinct_id")
38+
print(result.enabled) # True or False
39+
print(result.variant) # 'the-variant-value' or None
40+
print(result.payload) # {'foo': 'bar'}
41+
print(result.get_value()) # 'the-variant-value' or True or False
42+
print(result.reason) # 'matched condition set 2' (Not available for local evaluation)
43+
```
44+
45+
Breaking change:
46+
47+
1. `get_feature_flag_payload` now deserializes payloads from JSON strings to `Any`. Previously, it returned the payload as a JSON encoded string.
48+
49+
Before:
50+
51+
```python
52+
payload = get_feature_flag_payload('key', 'distinct_id') # "{\"some\": \"payload\"}"
53+
```
54+
55+
After:
56+
57+
```python
58+
payload = get_feature_flag_payload('key', 'distinct_id') # {"some": "payload"}
59+
```
60+
61+
## 3.25.0 – 2025-04-15
62+
63+
1. Roll out new `/flags` endpoint to 100% of `/decide` traffic, excluding the top 10 customers.
64+
65+
## 3.24.3 – 2025-04-15
66+
67+
1. Fix hash inclusion/exclusion for flag rollout
68+
69+
## 3.24.2 – 2025-04-15
70+
71+
1. Roll out new /flags endpoint to 10% of /decide traffic
72+
173
## 3.24.1 – 2025-04-11
274

375
1. Add `log_captured_exceptions` option to proxy setup
@@ -13,7 +85,7 @@
1385
## 3.22.0 – 2025-03-26
1486

1587
1. Add more information to `$feature_flag_called` events.
16-
2. Support for the `/decide?v=3` endpoint which contains more information about feature flags.
88+
2. Support for the `/decide?v=4` endpoint which contains more information about feature flags.
1789

1890
## 3.21.0 – 2025-03-17
1991

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

README.md

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
# PostHog Python
22

3-
[![PyPI](https://img.shields.io/pypi/v/posthog)](https://pypi.org/project/posthog/)
4-
3+
<p align="center">
4+
<img alt="posthoglogo" src="https://user-images.githubusercontent.com/65415371/205059737-c8a4f836-4889-4654-902e-f302b187b6a0.png">
5+
</p>
6+
<p align="center">
7+
<a href="https://pypi.org/project/posthog/"><img alt="pypi installs" src="https://img.shields.io/pypi/v/posthog"/></a>
8+
<img alt="GitHub contributors" src="https://img.shields.io/github/contributors/posthog/posthog-python">
9+
<img alt="GitHub commit activity" src="https://img.shields.io/github/commit-activity/m/posthog/posthog-python"/>
10+
<img alt="GitHub closed issues" src="https://img.shields.io/github/issues-closed/posthog/posthog-python"/>
11+
</p>
512

613
Please see the [Python integration docs](https://posthog.com/docs/integrations/python-integration) for details.
714

@@ -14,9 +21,22 @@ Please see the [Python integration docs](https://posthog.com/docs/integrations/p
1421
2. Run `source env/bin/activate` (activates the virtual environment)
1522
3. Run `python3 -m pip install -e ".[test]"` (installs the package in develop mode, along with test dependencies)
1623
* or `uv pip install -e ".[test]"`
17-
4. Run `make test`
24+
4. you have to run `pre-commit install` to have auto linting pre commit
25+
5. Run `make test`
1826
1. To run a specific test do `pytest -k test_no_api_key`
1927

28+
## PostHog recommends `uv` so...
29+
30+
```bash
31+
uv python install 3.9.19
32+
uv python pin 3.9.19
33+
uv venv env
34+
source env/bin/activate
35+
uv pip install --editable ".[dev,test]"
36+
pre-commit install
37+
make test
38+
```
39+
2040
### Running Locally
2141

2242
Assuming you have a [local version of PostHog](https://posthog.com/docs/developing-locally) running, you can run `python3 example.py` to see the library in action.
@@ -41,8 +61,4 @@ Then navigate to `http://127.0.0.1:8080/sentry-debug/` and you should get an eve
4161

4262
### Releasing Versions
4363

44-
Updated are released using GitHub Actions: after bumping `version.py` in `master` and adding to `CHANGELOG.md`, go to [our release workflow's page](https://github.com/PostHog/posthog-python/actions/workflows/release.yaml) and dispatch it manually, using workflow from `master`.
45-
46-
## Questions?
47-
48-
### [Join our Slack community.](https://join.slack.com/t/posthogusers/shared_invite/enQtOTY0MzU5NjAwMDY3LTc2MWQ0OTZlNjhkODk3ZDI3NDVjMDE1YjgxY2I4ZjI4MzJhZmVmNjJkN2NmMGJmMzc2N2U3Yjc3ZjI5NGFlZDQ)
64+
Updated are released using GitHub Actions: after bumping `version.py` in `master` and adding to `CHANGELOG.md`, go to [our release workflow's page](https://github.com/PostHog/posthog-python/actions/workflows/release.yaml) and dispatch it manually, using workflow from `master`.

bin/build

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
#/ Usage: bin/build
3+
#/ Description: Runs linter and mypy
4+
source bin/helpers/_utils.sh
5+
set_source_and_root_dir
6+
7+
flake8 posthog --ignore E501,W503
8+
mypy --no-site-packages --config-file mypy.ini . | mypy-baseline filter

bin/fmt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
#/ Usage: bin/fmt
3+
#/ Description: Formats and lints the code
4+
source bin/helpers/_utils.sh
5+
set_source_and_root_dir
6+
ensure_virtual_env
7+
8+
if [[ "$1" == "--check" ]]; then
9+
black --check .
10+
isort --check-only .
11+
else
12+
black .
13+
isort .
14+
fi

bin/helpers/_utils.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error() {
2+
echo "$@" >&2
3+
}
4+
5+
fatal() {
6+
error "$@"
7+
exit 1
8+
}
9+
10+
set_source_and_root_dir() {
11+
{ set +x; } 2>/dev/null
12+
source_dir="$( cd -P "$( dirname "$0" )" >/dev/null 2>&1 && pwd )"
13+
root_dir=$(cd "$source_dir" && cd ../ && pwd)
14+
cd "$root_dir"
15+
}
16+
17+
ensure_virtual_env() {
18+
if [ -z "$VIRTUAL_ENV" ]; then
19+
echo "Virtual environment not activated. Activating now..."
20+
if [ ! -f env/bin/activate ]; then
21+
echo "Virtual environment not found. Please run 'python -m venv env' first."
22+
exit 1
23+
fi
24+
source env/bin/activate
25+
fi
26+
}

bin/setup

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
#/ Usage: bin/setup
3+
#/ Description: Sets up the dependencies needed to develop this project
4+
source bin/helpers/_utils.sh
5+
set_source_and_root_dir
6+
7+
if [ ! -d "env" ]; then
8+
python3 -m venv env
9+
fi
10+
11+
source env/bin/activate
12+
pip install -e ".[dev,test]"

0 commit comments

Comments
 (0)