
    jKj(                         d Z ddlmZ ddlmZmZmZmZmZm	Z	m
Z
mZ ddlmZ  ed       G d d             Ze G d	 d
             Z G d d      Zy)uC  FeatureFlagEvaluations — a snapshot of feature flag values for a single distinct_id.

Returned by Client.evaluate_flags(). Branch on .is_enabled() / .get_flag(), then pass
the same snapshot to capture() via the `flags` option so events carry the exact flag
values the code branched on, with no additional /flags request.
    )	dataclass)AnyCallableDictListMappingOptionalSetUnion)	FlagValueT)frozenc                       e Zd ZU dZeed<   eed<   ee   ed<   ee   ed<   ee	   ed<   ee	   ed<   ee   ed<   eed	<   y
)_EvaluatedFlagRecordzEInternal per-flag record stored by a FeatureFlagEvaluations instance.keyenabledvariantpayloadidversionreasonlocally_evaluatedN)
__name__
__module____qualname____doc__str__annotations__boolr	   r   int     q/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/posthog/feature_flag_evaluations.pyr   r      sG    O	HMc]c]c]SMr!   r   c                   8    e Zd ZU dZed   ed<   eegdf   ed<   y)_FeatureFlagEvaluationsHostu   Callbacks the evaluations object uses to talk back to the client.

    Kept as a plain dataclass of callables so the class stays decoupled from the
    full Client surface — this also makes it trivial to construct a fake host in tests.
    ).N#capture_flag_called_event_if_neededNlog_warning)r   r   r   r   r   r   r   r    r!   r"   r$   r$      s%     *2))<<3%+&&r!   r$   c                      e Zd ZdZ	 	 	 	 	 	 	 ddededeeef   dee	ee
eef   f      dee   dee   d	ee   d
ededeee      ddfdZdedefdZdedee   fdZdedee   fdZddZdee   dd fdZedee   fd       Zdeeef   fdZedefd       Zedeee
eef   f   fd       Zdeeef   dd fdZdeddfdZy)FeatureFlagEvaluationsus  A point-in-time snapshot of feature flag evaluations for a single distinct_id.

    Returned by :meth:`Client.evaluate_flags` — branch on :meth:`is_enabled` /
    :meth:`get_flag` and pass the same object to :meth:`Client.capture` via the
    ``flags`` option so the captured event carries the exact flag values the code
    branched on.

    Example::

        flags = posthog.evaluate_flags(distinct_id, person_properties={"plan": "enterprise"})
        if flags.is_enabled("new-dashboard"):
            render_new_dashboard()
        posthog.capture("page_viewed", distinct_id=distinct_id, flags=flags)

    To narrow the set of flags that get attached to a captured event, use the in-memory
    helpers :meth:`only` and :meth:`only_accessed`. To narrow the set of flags requested
    from the server in the first place, pass ``flag_keys`` to :meth:`Client.evaluate_flags`.
    Nhostdistinct_idflagsgroupsdisable_geoip
request_idevaluated_aterrors_while_computingquota_limitedaccessedreturnc                     || _         || _        || _        t        |xs i       | _        || _        || _        || _        || _        |	| _	        |
t        |
      | _        yt               | _        y)uN   Internal — instances are created by the SDK via ``Client.evaluate_flags()``.N)_host_distinct_id_flagsdict_groups_disable_geoip_request_id_evaluated_at_errors_while_computing_quota_limitedset	_accessed)selfr)   r*   r+   r,   r-   r.   r/   r0   r1   r2   s              r"   __init__zFeatureFlagEvaluations.__init__<   sj     
'37"3E+%)'=$+4<4H3x=cer!   r   c                     | j                   j                  |      }| j                  |       |rt        |j                        S dS )a$  Return whether the flag is enabled. Fires ``$feature_flag_called`` on the
        first access per (distinct_id, flag, value) tuple, deduped via the SDK's cache.

        Flags that were not returned from the underlying evaluation are treated as
        disabled (returns ``False``).
        F)r7   get_record_accessr   r   rA   r   flags      r"   
is_enabledz!FeatureFlagEvaluations.is_enabledU   s:     {{s#C %)tDLL!4u4r!   c                     | j                   j                  |      }| j                  |       |sy|j                  sy|j                  |j                  S dS )a(  Return the flag value. Fires ``$feature_flag_called`` on first access.

        Returns the variant string for multivariate flags, ``True`` for enabled flags
        without a variant, ``False`` for disabled flags, and ``None`` for flags that
        were not returned by the evaluation.
        NFT)r7   rD   rE   r   r   rF   s      r"   get_flagzFeatureFlagEvaluations.get_flag`   sL     {{s#C ||#||7t||ATAr!   c                 X    | j                   j                  |      }|r|j                  S dS )zReturn the payload associated with a flag.

        Does not count as an access for :meth:`only_accessed` and does not fire any event.
        N)r7   rD   r   rF   s      r"   get_flag_payloadz'FeatureFlagEvaluations.get_flag_payloado   s'    
 {{s##t||--r!   c                     | j                   D ci c]!  }|| j                  v s|| j                  |   # }}| j                  |      S c c}w )u1  Return a filtered copy containing only flags accessed via :meth:`is_enabled`
        or :meth:`get_flag` before this call.

        Order-dependent: if nothing has been accessed yet, the returned snapshot is
        empty. The method honors its name — pre-access if you want a populated result.
        )r@   r7   _clone_with)rA   kfiltereds      r"   only_accessedz$FeatureFlagEvaluations.only_accessedw   sK     04~~R~!dkkAQAt{{1~%~R)) Ss
   AAkeysc                     i }g }|D ]6  }| j                   j                  |      }||||<   &|j                  |       8 |r-| j                  j	                  ddj                  |              | j                  |      S )zReturn a filtered copy containing only flags with the given keys. Keys that
        are not present in the evaluation are dropped and logged as a warning.
        zpFeatureFlagEvaluations.only() was called with flag keys that are not in the evaluation set and will be dropped: z, )r7   rD   appendr5   r&   joinrN   )rA   rR   rP   missingr   rG   s         r"   onlyzFeatureFlagEvaluations.only   s     57C;;??3'D $s#  JJ""77;yy7I6JL ))r!   c                 H    t        | j                  j                               S )z6Return the flag keys that are part of this evaluation.)listr7   rR   rA   s    r"   rR   zFeatureFlagEvaluations.keys   s     DKK$$&''r!   c                    i }g }| j                   j                         D ]S  \  }}|j                  sdn|j                  |j                  nd}||d| <   |j                  sC|j	                  |       U |rt        |      |d<   |S )u   Build the ``$feature/*`` and ``$active_feature_flags`` properties for an event.

        Internal — called by capture() when an event is captured with ``flags=...``.
        FT	$feature/z$active_feature_flags)r7   itemsr   r   rT   sorted)rA   
propertiesactive_flagsr   rG   values         r"   _get_event_propertiesz,FeatureFlagEvaluations._get_event_properties   s    
 &(
"$**,IC || &*ll&>dllD 
 -2J3%()||##C( - 282FJ./r!   c                     | j                   S N)r6   rZ   s    r"   _internal_distinct_idz,FeatureFlagEvaluations._internal_distinct_id   s       r!   c                     | j                   S rd   )r9   rZ   s    r"   _internal_groupsz'FeatureFlagEvaluations._internal_groups   s    ||r!   c                     t        | j                  | j                  || j                  | j                  | j
                  | j                  | j                  | j                  t        | j                        
      S )N)
r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   )r(   r5   r6   r9   r:   r;   r<   r=   r>   r?   r@   )rA   r+   s     r"   rN   z"FeatureFlagEvaluations._clone_with   sf     &))<<--''++#'#?#?-- (
 	
r!   c                    | j                   j                  |       | j                  sy | j                  j	                  |      }|d }n)|j
                  sd}n|j                  |j                  nd}d|d|d|r|j                  ndd| |i}|l|j                  |j                  |d<   |j                  r|j                  |d<   |j                  r|j                  |d	<   |j                  r|j                  |d
<   | j                  r| j                  |d<   | j                  r|r|j                  s| j                  |d<   g }| j                  r|j                  d       | j                   r|j                  d       ||j                  d       |rdj#                  |      |d<   | j$                  j'                  | j                  ||| j(                  | j*                  |       y )NFTz$feature_flagz$feature_flag_responser   r\   z$feature_flag_payloadz$feature_flag_idz$feature_flag_versionz$feature_flag_reasonz$feature_flag_request_idz$feature_flag_evaluated_aterrors_while_computing_flagsr1   flag_missing,z$feature_flag_error)r*   r   responser,   r-   r_   )r@   addr6   r7   rD   r   r   r   r   r   r   r   r;   r<   r=   rT   r>   rU   r5   r%   r9   r:   )rA   r   rG   rm   r_   errorss         r"   rE   z%FeatureFlagEvaluations._record_access   s   3
   {{s#<,0HH'+||'?t||TH S$h4!7!7Uux	&

 ||'6:ll
23ww15
-.||6:ll
23{{59[[
12595E5EJ12t0F0F7;7I7IJ34
 ''MM89MM/*<MM.)030@J,-

66))<<--! 	7 	
r!   )NNNNFFN)r3   r(   )r   r   r   r   r$   r   r   r   r	   r   r   r   r   r
   rB   rH   r   rJ   r   rL   rQ   r   rW   propertyrR   rb   re   rg   rN   rE   r    r!   r"   r(   r(   (   s   0 ;?(,$(&*',#'+T)T T C--.	T
 eCHo!567T  ~T SMT smT !%T T 3s8$T 
T2	5c 	5d 	5BC BHY$7 B.C .HSM .**c *'? *& (d3i ( (tCH~ ( !s ! ! $sE#s(O';"<  
#334
	!
&;
# ;
$ ;
r!   r(   N)r   dataclassesr   typingr   r   r   r   r   r	   r
   r   posthog.typesr   r   r$   r(   r    r!   r"   <module>rt      s^    " K K K # $
 
 
 ' ' ']
 ]
r!   