Skip to content

Commit 79c9d47

Browse files
committed
fix: improve strict public API types
1 parent ee80810 commit 79c9d47

3 files changed

Lines changed: 176 additions & 172 deletions

File tree

.changeset/quiet-horses-type.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'pypi/posthog': patch
3+
---
4+
5+
Improve strict Pyright coverage for public PostHog APIs.

posthog/__init__.py

Lines changed: 86 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from typing_extensions import Unpack
55

6-
from posthog.args import ExceptionArg, OptionalCaptureArgs, OptionalSetArgs
6+
from posthog.args import ID_TYPES, ExceptionArg, OptionalCaptureArgs, OptionalSetArgs
77
from posthog.client import Client
88
from posthog.exception_capture import ExceptionCapture
99
from posthog.contexts import (
@@ -61,7 +61,8 @@
6161
)
6262
from posthog.types import (
6363
BeforeSendCallback as BeforeSendCallback,
64-
FeatureFlag,
64+
FeatureFlag as FeatureFlag,
65+
FlagValue,
6566
FlagsAndPayloads,
6667
)
6768
from posthog.types import (
@@ -198,7 +199,7 @@ def set_capture_exception_code_variables_context(enabled: bool):
198199
return inner_set_capture_exception_code_variables_context(enabled)
199200

200201

201-
def set_code_variables_mask_patterns_context(mask_patterns: list):
202+
def set_code_variables_mask_patterns_context(mask_patterns: list[str]):
202203
"""
203204
Override code-variable mask patterns for exceptions in the current context.
204205
@@ -212,7 +213,7 @@ def set_code_variables_mask_patterns_context(mask_patterns: list):
212213
return inner_set_code_variables_mask_patterns_context(mask_patterns)
213214

214215

215-
def set_code_variables_ignore_patterns_context(ignore_patterns: list):
216+
def set_code_variables_ignore_patterns_context(ignore_patterns: list[str]):
216217
"""
217218
Override code-variable ignore patterns for exceptions in the current context.
218219
@@ -491,15 +492,14 @@ def set_once(**kwargs: Unpack[OptionalSetArgs]) -> Optional[str]:
491492

492493

493494
def group_identify(
494-
group_type, # type: str
495-
group_key, # type: str
496-
properties=None, # type: Optional[Dict]
497-
timestamp=None, # type: Optional[datetime.datetime]
498-
uuid=None, # type: Optional[str]
499-
disable_geoip=None, # type: Optional[bool]
500-
distinct_id=None, # type: Optional[str]
501-
):
502-
# type: (...) -> Optional[str]
495+
group_type: str,
496+
group_key: str,
497+
properties: Optional[Dict[str, Any]] = None,
498+
timestamp: Optional[datetime.datetime] = None,
499+
uuid: Optional[str] = None,
500+
disable_geoip: Optional[bool] = None,
501+
distinct_id: Optional[ID_TYPES] = None,
502+
) -> Optional[str]:
503503
"""
504504
Set properties on a group.
505505
@@ -538,13 +538,12 @@ def group_identify(
538538

539539

540540
def alias(
541-
previous_id, # type: str
542-
distinct_id, # type: str
543-
timestamp=None, # type: Optional[datetime.datetime]
544-
uuid=None, # type: Optional[str]
545-
disable_geoip=None, # type: Optional[bool]
546-
):
547-
# type: (...) -> Optional[str]
541+
previous_id: str,
542+
distinct_id: str,
543+
timestamp: Optional[datetime.datetime] = None,
544+
uuid: Optional[str] = None,
545+
disable_geoip: Optional[bool] = None,
546+
) -> Optional[str]:
548547
"""
549548
Associate user behaviour before and after they e.g. register, login, or perform some other identifying action.
550549
@@ -610,17 +609,16 @@ def capture_exception(
610609

611610

612611
def feature_enabled(
613-
key, # type: str
614-
distinct_id, # type: str
615-
groups=None, # type: Optional[dict]
616-
person_properties=None, # type: Optional[dict]
617-
group_properties=None, # type: Optional[dict]
618-
only_evaluate_locally=False, # type: bool
619-
send_feature_flag_events=True, # type: bool
620-
disable_geoip=None, # type: Optional[bool]
621-
device_id=None, # type: Optional[str]
622-
):
623-
# type: (...) -> bool
612+
key: str,
613+
distinct_id: ID_TYPES,
614+
groups: Optional[Dict[str, str]] = None,
615+
person_properties: Optional[Dict[str, Any]] = None,
616+
group_properties: Optional[Dict[str, Dict[str, Any]]] = None,
617+
only_evaluate_locally: bool = False,
618+
send_feature_flag_events: bool = True,
619+
disable_geoip: Optional[bool] = None,
620+
device_id: Optional[str] = None,
621+
) -> Optional[bool]:
624622
"""
625623
Use feature flags to enable or disable features for users.
626624
@@ -664,16 +662,16 @@ def feature_enabled(
664662

665663

666664
def get_feature_flag(
667-
key, # type: str
668-
distinct_id, # type: str
669-
groups=None, # type: Optional[dict]
670-
person_properties=None, # type: Optional[dict]
671-
group_properties=None, # type: Optional[dict]
672-
only_evaluate_locally=False, # type: bool
673-
send_feature_flag_events=True, # type: bool
674-
disable_geoip=None, # type: Optional[bool]
675-
device_id=None, # type: Optional[str]
676-
) -> Optional[FeatureFlag]:
665+
key: str,
666+
distinct_id: ID_TYPES,
667+
groups: Optional[Dict[str, str]] = None,
668+
person_properties: Optional[Dict[str, Any]] = None,
669+
group_properties: Optional[Dict[str, Dict[str, Any]]] = None,
670+
only_evaluate_locally: bool = False,
671+
send_feature_flag_events: bool = True,
672+
disable_geoip: Optional[bool] = None,
673+
device_id: Optional[str] = None,
674+
) -> Optional[FlagValue]:
677675
"""
678676
Get feature flag variant for users. Used with experiments.
679677
@@ -717,15 +715,15 @@ def get_feature_flag(
717715

718716

719717
def get_all_flags(
720-
distinct_id, # type: str
721-
groups=None, # type: Optional[dict]
722-
person_properties=None, # type: Optional[dict]
723-
group_properties=None, # type: Optional[dict]
724-
only_evaluate_locally=False, # type: bool
725-
disable_geoip=None, # type: Optional[bool]
726-
device_id=None, # type: Optional[str]
727-
flag_keys_to_evaluate=None, # type: Optional[list[str]]
728-
) -> Optional[dict[str, FeatureFlag]]:
718+
distinct_id: ID_TYPES,
719+
groups: Optional[Dict[str, str]] = None,
720+
person_properties: Optional[Dict[str, Any]] = None,
721+
group_properties: Optional[Dict[str, Dict[str, Any]]] = None,
722+
only_evaluate_locally: bool = False,
723+
disable_geoip: Optional[bool] = None,
724+
device_id: Optional[str] = None,
725+
flag_keys_to_evaluate: Optional[list[str]] = None,
726+
) -> Optional[dict[str, FlagValue]]:
729727
"""
730728
Get all flags for a given user.
731729
@@ -765,17 +763,16 @@ def get_all_flags(
765763

766764

767765
def get_feature_flag_result(
768-
key,
769-
distinct_id,
770-
groups=None, # type: Optional[dict]
771-
person_properties=None, # type: Optional[dict]
772-
group_properties=None, # type: Optional[dict]
773-
only_evaluate_locally=False,
774-
send_feature_flag_events=True,
775-
disable_geoip=None, # type: Optional[bool]
776-
device_id=None, # type: Optional[str]
777-
):
778-
# type: (...) -> Optional[FeatureFlagResult]
766+
key: str,
767+
distinct_id: ID_TYPES,
768+
groups: Optional[Dict[str, str]] = None,
769+
person_properties: Optional[Dict[str, Any]] = None,
770+
group_properties: Optional[Dict[str, Dict[str, Any]]] = None,
771+
only_evaluate_locally: bool = False,
772+
send_feature_flag_events: bool = True,
773+
disable_geoip: Optional[bool] = None,
774+
device_id: Optional[str] = None,
775+
) -> Optional[FeatureFlagResult]:
779776
"""
780777
Get a FeatureFlagResult object which contains the flag result and payload.
781778
@@ -821,17 +818,17 @@ def get_feature_flag_result(
821818

822819

823820
def get_feature_flag_payload(
824-
key,
825-
distinct_id,
826-
match_value=None,
827-
groups=None, # type: Optional[dict]
828-
person_properties=None, # type: Optional[dict]
829-
group_properties=None, # type: Optional[dict]
830-
only_evaluate_locally=False,
831-
send_feature_flag_events=True,
832-
disable_geoip=None, # type: Optional[bool]
833-
device_id=None, # type: Optional[str]
834-
) -> Optional[str]:
821+
key: str,
822+
distinct_id: ID_TYPES,
823+
match_value: Optional[FlagValue] = None,
824+
groups: Optional[Dict[str, str]] = None,
825+
person_properties: Optional[Dict[str, Any]] = None,
826+
group_properties: Optional[Dict[str, Dict[str, Any]]] = None,
827+
only_evaluate_locally: bool = False,
828+
send_feature_flag_events: bool = True,
829+
disable_geoip: Optional[bool] = None,
830+
device_id: Optional[str] = None,
831+
) -> Optional[Any]:
835832
"""
836833
Get the payload associated with a feature flag value.
837834
@@ -869,7 +866,7 @@ def get_feature_flag_payload(
869866

870867

871868
def get_remote_config_payload(
872-
key, # type: str
869+
key: str,
873870
):
874871
"""Get the payload for a remote config feature flag.
875872
@@ -889,14 +886,14 @@ def get_remote_config_payload(
889886

890887

891888
def get_all_flags_and_payloads(
892-
distinct_id,
893-
groups=None, # type: Optional[dict]
894-
person_properties=None, # type: Optional[dict]
895-
group_properties=None, # type: Optional[dict]
896-
only_evaluate_locally=False,
897-
disable_geoip=None, # type: Optional[bool]
898-
device_id=None, # type: Optional[str]
899-
flag_keys_to_evaluate=None, # type: Optional[list[str]]
889+
distinct_id: ID_TYPES,
890+
groups: Optional[Dict[str, str]] = None,
891+
person_properties: Optional[Dict[str, Any]] = None,
892+
group_properties: Optional[Dict[str, Dict[str, Any]]] = None,
893+
only_evaluate_locally: bool = False,
894+
disable_geoip: Optional[bool] = None,
895+
device_id: Optional[str] = None,
896+
flag_keys_to_evaluate: Optional[list[str]] = None,
900897
) -> FlagsAndPayloads:
901898
"""
902899
Get all feature flag values and payloads for a user.
@@ -932,14 +929,14 @@ def get_all_flags_and_payloads(
932929

933930

934931
def evaluate_flags(
935-
distinct_id=None, # type: Optional[str]
936-
groups=None, # type: Optional[Dict[str, str]]
937-
person_properties=None, # type: Optional[Dict[str, Any]]
938-
group_properties=None, # type: Optional[Dict[str, Dict[str, Any]]]
939-
only_evaluate_locally=False, # type: bool
940-
disable_geoip=None, # type: Optional[bool]
941-
flag_keys=None, # type: Optional[list]
942-
device_id=None, # type: Optional[str]
932+
distinct_id: Optional[str] = None,
933+
groups: Optional[Dict[str, str]] = None,
934+
person_properties: Optional[Dict[str, Any]] = None,
935+
group_properties: Optional[Dict[str, Dict[str, Any]]] = None,
936+
only_evaluate_locally: bool = False,
937+
disable_geoip: Optional[bool] = None,
938+
flag_keys: Optional[list[str]] = None,
939+
device_id: Optional[str] = None,
943940
) -> FeatureFlagEvaluations:
944941
"""Evaluate all feature flags for a user in a single call and return a
945942
:class:`FeatureFlagEvaluations` snapshot. Branch on ``.is_enabled()`` /

0 commit comments

Comments
 (0)