Skip to content

Conversation

@oliverb123
Copy link
Contributor

Also bump version for release

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

Major refactoring of the PostHog Python SDK to integrate session and identity management with the context system, improving state management and API consistency.

  • Added new context-aware functions (identify_context, set_context_session) in posthog/scopes.py for better session and identity handling
  • Introduced ContextScope class with proper parent-child relationships, replacing simple stack-based dictionary approach
  • Modified core client methods (identify, capture, set) to automatically handle session IDs and use context-based distinct_id fallback
  • Version bumped from 5.0.0 to 5.1.0 for these non-breaking feature additions
  • Added comprehensive test coverage for new context functionality in test_scopes.py

6 files reviewed, 3 comments
Edit PR Review Bot Settings | Greptile

@oliverb123
Copy link
Contributor Author

Plan is to release this, and then bump the dependency in posthog and add the middleware... this should mean we'll start associating all caught exceptions sessions and distinct ID's ✨ automatically ✨, once the frontend bumps it's SDK version.

@oliverb123 oliverb123 requested review from a team, daibhin and hpouillot June 17, 2025 16:29
Comment on lines +682 to +683
if distinct_id is None:
distinct_id = get_context_distinct_id()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the session data not being added here like it is in some of the earlier methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capture exception doesn't add the session id because it calls capture under the hood, which does. It only bothers grabbing the distinct id because it's is unique in that it auto-generates a distinct id if one isn't set, rather than throwing. To prevent that auto-generation, we have to try and grab a context distinct id up front.

return {}


# NOTE: We should probably remove this function - the way to clear scope context
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just remove this and the above? Feels like the tagging stuff is still in flux and I'm not sure anyone is fully relying on it yet. Maybe fine to wait given we're doing a major version bump soon

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was gonna wait for the major version bump, yeah

Comment on lines 157 to 159
Identify the current context with a distinct ID, associating all events captured in this or
child contexts with the given distinct ID (unless identify_context is called again). This is overridden by
distinct id's passed directly to posthog.capture and related methods (identify, set etc).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I start a new context with fresh=True will the identity still be set? Don't know if it should be but I'm guessing it isn't. Might be worth clarifying that somewhere for the user.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the comments around what fresh means in relation to sessions and distinct id's

@oliverb123 oliverb123 merged commit b775339 into master Jun 18, 2025
7 checks passed
@oliverb123 oliverb123 deleted the err/context-refactor branch June 18, 2025 16:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants