
    wgw_                       d Z ddlmZ ddlZddlZddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddlm!Z! ddlm"Z" ddlm#Z# ejH                  ee   e e!   f   Z%ejL                  e%ee!f      Z'ejP                  ejH                  ejR                  df      Z* ejV                  dd      Z, G d d ejZ                  e   ej\                  e         Z/ G d! d"eee!f         Z0d&d#Z1e0je                           G d$ d%      Z3y)'zProvide :class:`BidictBase`.    )annotationsN)starmap)eq)MappingProxyType   )BidirectionalMapping)DROP_NEW)DROP_OLD)ON_DUP_DEFAULT)RAISE)OnDup)DuplicationError)KeyAndValueDuplicationError)KeyDuplicationError)ValueDuplicationError)inverted)	iteritems)KT)MISSING)OKT)OVT)VT)Maplike)
MapOrItems.BTzBidictBase[t.Any, t.Any])boundc                      e Zd ZdZy)BidictKeysViewzSince the keys of a bidict are the values of its inverse (and vice versa),
    the :class:`~collections.abc.ValuesView` result of calling *bi.values()*
    is also a :class:`~collections.abc.KeysView` of *bi.inverse*.
    N__name__
__module____qualname____doc__     Q/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/bidict/_base.pyr   r   4   s    r%   r   c                      e Zd ZU dZeZded<   ded<   eZded<   eZ	ded<   d	ed
<   d- fdZ
ed-d       Zded<   ed-d       Zed-d       Zed.d       Zed/d       Zd0d1dZed2d       Zd2dZed2d       Zd3dZd4dZd5dZd6 fdZd7dZd8dZd8dZd9dZd:dZ ei       fd d d!	 	 	 	 	 	 	 	 	 d;d"Z d<d#Z!d<d$Z"e#d=d>d%       Z$d?d&Z%d@d'Z&d@d(Z'dAd)Z(dBd*Z)dCd+Z*dDd,Z+ xZ,S )E
BidictBasez6Base class implementing :class:`BidirectionalMapping`.zt.MutableMapping[KT, VT]_fwdmzt.MutableMapping[VT, KT]_invmz0t.ClassVar[type[t.MutableMapping[t.Any, t.Any]]]	_fwdm_cls	_invm_clsz*t.ClassVar[type[BidictBase[t.Any, t.Any]]]_inv_clsc                B    t         |           | j                          y N)super__init_subclass___init_class)cls	__class__s    r&   r1   zBidictBase.__init_subclass__Q   s    !#r%   c                D    | j                          | j                          y r/   )_ensure_inv_cls_set_reversedr3   s    r&   r2   zBidictBase._init_classU   s    r%   zt.ClassVar[t.Any]__reversed__c                    | t         ur!| j                  }|t         j                  u}|ryt        d | j                  | j                  fD              }|rt
        | _        yd| _        y)zSet __reversed__ for subclasses that do not set it explicitly
        according to whether backing mappings are reversible.
        Nc              3  P   K   | ]  }t        |t        j                           y wr/   )
issubclasst
Reversible).0is     r&   	<genexpr>z+BidictBase._set_reversed.<locals>.<genexpr>f   s      eAq||!< es   $&)r(   r9   allr+   r,   _fwdm_reversed)r3   resolved
overriddenbacking_reversibles       r&   r7   zBidictBase._set_reversed\   s_    
 j ''H!)@)@@J  es}}VYVcVcFd ee-?>Tr%   c                h    t        | di       j                  d      ry| j                         | _        y)a  Ensure :attr:`_inv_cls` is set, computing it dynamically if necessary.

        All subclasses provided in :mod:`bidict` are their own inverse classes,
        i.e., their backing forward and inverse mappings are both the same type,
        but users may define subclasses where this is not the case.
        This method ensures that the inverse class is computed correctly regardless.

        See: :ref:`extending:Dynamic Inverse Class Generation`
        (https://bidict.rtfd.io/extending.html#dynamic-inverse-class-generation)
        __dict__r-   N)getattrget_make_inv_clsr-   r8   s    r&   r6   zBidictBase._ensure_inv_clsi   s.    , 3
B'++J7((*r%   c                2     j                         }t         fd|j                         D              }|r S  |d<   t         j                   d t
        f|      } j                  |_        t        j                  t        j                  t           |      S )Nc              3  L   K   | ]  \  }}t        |t              |k(    y wr/   )rI   r   )r?   kvr3   s      r&   rA   z+BidictBase._make_inv_cls.<locals>.<genexpr>   s#     Vv1WS!W5:Vs   !$r-   Inv)_inv_cls_dict_diffrB   itemstyper    GeneratedBidictInverser!   r=   castTyper   )r3   diffcls_is_own_invinv_clss   `   r&   rK   zBidictBase._make_inv_cls   s    %%'VVVJ Z#,,s+c3I-JDQ ^^vvaffRj'**r%   c                4    | j                   | j                  dS )N)r+   r,   )r,   r+   r8   s    r&   rQ   zBidictBase._inv_cls_dict_diff   s     
 	
r%   c                   | j                         | _        | j                         | _        | j	                  ||d       y)a8  Make a new bidirectional mapping.
        The signature behaves like that of :class:`dict`.
        ktems passed via positional arg are processed first,
        followed by any items passed via keyword argument.
        Any duplication encountered along the way
        is handled as per :attr:`on_dup`.
        FrollbackN)r+   r)   r,   r*   _update)selfargkws      r&   __init__zBidictBase.__init__   s2     ^^%
^^%
S"u-r%   c                    t        | dd      }||S t        | dd      }| |       }||S | j                         }|| _        d| _        d|_        t	        j
                  |       |_        |S )z3The inverse of this bidirectional mapping instance._invN_invweak)rI   _make_inverserd   re   weakrefref)r_   invinvweaks      r&   inversezBidictBase.inverse   s}     *1vt)D?J$
D1)C
  "/2	JN {{4( 
r%   c                j    | j                         }| j                  |_        | j                  |_        |S r/   )r-   r*   r)   )r_   ri   s     r&   rf   zBidictBase._make_inverse   s(    "&--/JJ	JJ	
r%   c                    | j                   S )zAlias for :attr:`inverse`.)rk   r_   s    r&   ri   zBidictBase.inv   s     ||r%   c                x    | j                   j                  }| rt        | j                               nd}| d| dS )zSee :func:`repr`. ())r4   r    dictrR   )r_   clsnamerR   s      r&   __repr__zBidictBase.__repr__   s8    ..))&*TZZ\"!E7!$$r%   c                t    t        j                  t        t           | j                  j                               S )a;  A set-like object providing a view on the contained values.

        Since the values of a bidict are equivalent to the keys of its inverse,
        this method returns a set-like object for this bidict's values
        rather than just a collections.abc.ValuesView.
        This object supports set operations like union and difference,
        and constant- rather than linear-time containment checks,
        and is no more expensive to provide than the less capable
        collections.abc.ValuesView would be.

        See :meth:`keys` for more information.
        )r=   rU   r   r   rk   keysrn   s    r&   valueszBidictBase.values   s'     vvnR($,,*;*;*=>>r%   c                x    | j                   | j                  }}|t        u r|j                         S t	        |       S )a  A set-like object providing a view on the contained keys.

        When *b._fwdm* is a :class:`dict`, *b.keys()* returns a
        *dict_keys* object that behaves exactly the same as
        *collections.abc.KeysView(b)*, except for

          - offering better performance

          - being reversible on Python 3.8+

          - having a .mapping attribute in Python 3.10+
            that exposes a mappingproxy to *b._fwdm*.
        )r)   r+   rs   rw   r   )r_   fwdmfwdm_clss      r&   rw   zBidictBase.keys   s2     T^^h&$.tyy{HN44HHr%   c                x    | j                   t        u r| j                  j                         S t        |          S )a  A set-like object providing a view on the contained items.

        When *b._fwdm* is a :class:`dict`, *b.items()* returns a
        *dict_items* object that behaves exactly the same as
        *collections.abc.ItemsView(b)*, except for:

          - offering better performance

          - being reversible on Python 3.8+

          - having a .mapping attribute in Python 3.10+
            that exposes a mappingproxy to *b._fwdm*.
        )r+   rs   r)   rR   r0   )r_   r4   s    r&   rR   zBidictBase.items   s.     &*^^t%;tzz!PPr%   c                    || j                   v S )z;True if the mapping contains the specified key, else False.r)   r_   keys     r&   __contains__zBidictBase.__contains__  s    djj  r%   c                    t        |t        j                        r+| j                  j	                         |j	                         k(  S t
        S )u0  *x.__eq__(other)　⟺　x == other*

        Equivalent to *dict(x.items()) == dict(other.items())*
        but more efficient.

        Note that :meth:`bidict's __eq__() <bidict.BidictBase.__eq__>` implementation
        is inherited by subclasses,
        in particular by the ordered bidict subclasses,
        so even with ordered bidicts,
        :ref:`== comparison is order-insensitive <eq-order-insensitive>`
        (https://bidict.rtfd.io/other-bidict-types.html#eq-is-order-insensitive).

        *See also* :meth:`equals_order_sensitive`
        )
isinstancer=   Mappingr)   rR   NotImplementedr_   others     r&   __eq__zBidictBase.__eq__  s6     eQYY'::##%66r%   c           
         t        |t        j                        rt        |       t        |      k7  ryt	        t        t        t        | j                         |j                                           S )zOrder-sensitive equality check.

        *See also* :ref:`eq-order-insensitive`
        (https://bidict.rtfd.io/other-bidict-types.html#eq-is-order-insensitive)
        F)	r   r=   r   lenrB   r   r   ziprR   r   s     r&   equals_order_sensitivez!BidictBase.equals_order_sensitive&  sI     %+s4yCJ/F72s4::<?@AAr%   c                   | j                   | j                  }}|j                  |t              }|j                  |t              }|t        u|t        u}	}|rX|	rV||k(  r||k(  sJ y|j                  t
        u rt        ||      |j                  t        u ry|j                  t        u sJ ||fS |rH|j                  t
        u rt        |      |j                  t        u ry|j                  t        u sJ ||fS |	rD|j                  t
        u rt        |      |j                  t        u ry|j                  t        u sJ ||fS )a  Check *key* and *val* for any duplication in self.

        Handle any duplication as per the passed in *on_dup*.

        If (key, val) is already present, return None
        since writing (key, val) would be a no-op.

        If duplication is found and the corresponding :class:`~bidict.OnDupAction` is
        :attr:`~bidict.DROP_NEW`, return None.

        If duplication is found and the corresponding :class:`~bidict.OnDupAction` is
        :attr:`~bidict.RAISE`, raise the appropriate exception.

        If duplication is found and the corresponding :class:`~bidict.OnDupAction` is
        :attr:`~bidict.DROP_OLD`, or if no duplication is found,
        return *(oldkey, oldval)*.
        N)r)   r*   rJ   r   valr   r   r	   r
   r   r   r   )
r_   r   r   on_duprz   invmoldvaloldkeyisdupkeyisdupvals
             r&   _dedupzBidictBase._dedup0  s;   $ ZZd((30((30#72F'4I(f}f}$}zzU"1#s;;zzX%::)))" v~ zzU")#..zzX%::))) v~ zzU"+C00zzX%::))) v~r%   c                Z   | j                   | j                  }}|j                  |j                  }	}|j                  |j                  }}
 |||        |	||       |t        u r#|t        u r||j                  |
|f||ff       yy|t        ur=|t        ur5 |
|        ||       |"|j                  |||f|	||f|||f|	||ff       yy|t        ur( ||       ||j                  |||f|	||f||ff       yy|t        usJ  |
|       ||j                  |||f|	||f|
|ff       yy)a  Insert (newkey, newval), extending *unwrites* with associated inverse operations if provided.

        *oldkey* and *oldval* are as returned by :meth:`_dedup`.

        If *unwrites* is not None, it is extended with the inverse operations necessary to undo the write.
        This design allows :meth:`_update` to roll back a partially applied update that fails part-way through
        when necessary.

        This design also allows subclasses that require additional operations to easily extend this implementation.
        For example, :class:`bidict.OrderedBidictBase` calls this inherited implementation, and then extends *unwrites*
        with additional operations needed to keep its internal linked list nodes consistent with its items' order
        as changes are made.
        N)r)   r*   __setitem____delitem__r   extend)r_   newkeynewvalr   r   unwritesrz   r   fwdm_setinvm_setfwdm_delinvm_dels               r&   _writezBidictBase._writec  s    ZZd!--t/?/?(!--t/?/?(  W7!2#v&v&!  $
 7"vW'<VV#vv.vv.vv.vv.	!  $ 7"V#vv.vv.v&!  $ (((V#vv.vv.v&!  $r%   Nr]   r   c                  t        |t        j                  t        f      s#t	        d|j
                  j                   d      |s|sy|| j                  }|t        |v }| s$|s"t        |t              r| j                  |       y|rtt        |t        j                        rZt        |      t        |      z   t        |       kD  r7| j                         }|j                  ||d|       | j                  |       y| j                  }|rg nd}t!        |fi |D ])  \  }}		 | j#                  ||	|      }
|
 |||	g|
d|i + y# t$        $ r |t'        |      D ]	  ^}} ||    w xY w)ziUpdate with the items from *arg* and *kw*, maybe failing and rolling back as per *on_dup* and *rollback*.'z' object is not iterableNFr   r   )r   r=   Iterabler   	TypeErrorr4   r    r   r   r(   
_init_fromSizedr   copyr^   r   r   r   r   reversed)r_   r`   ra   r]   r   tmpwriter   r   r   dedup_resultfnargss                r&   r^   zBidictBase._update  sr    #

G45a 6 677OPQQ2>[[FH B:c:#>OOC  
30SXB5G#d)5S))+CKKR%K?OOC  *2B!#,, 		BHC#{{3V< 'c3AAA		B $ '%-h%7 "	TD	"	s   "E$E,c                "    | j                         S )z7Used for the copy protocol. See the :mod:`copy` module.)r   rn   s    r&   __copy__zBidictBase.__copy__  s    yy{r%   c                :    | j                  | j                  |       S )z%Make a (shallow) copy of this bidict.)_from_otherr4   rn   s    r&   r   zBidictBase.copy  s     55r%   c                     |        }|j                  |       |r$t        j                  t        |j                        S |S )a  Fast, private constructor based on :meth:`_init_from`.

        If *inv* is true, return the inverse of the instance instead of the instance itself.
        (Useful for pickling with dynamically-generated inverse classes -- see :meth:`__reduce__`.)
        )r   r=   rU   r   rk   )btr   ri   insts       r&   r   zBidictBase._from_other  s3     t+.qvvb$,,'8D8r%   c                :   | j                   j                          | j                  j                          | j                   j                  |       t	        |t
              r|j                  nt        | j                         }| j                  j                  |       y)zDFast init from *other*, bypassing item-by-item duplication checking.N)r)   clearr*   updater   r(   rk   r   )r_   r   ri   s      r&   r   zBidictBase._init_from  se    





%   *%<emm(4::BV

#r%   c                    t        |t        j                        st        S | j	                         }|j                  |d       |S )zReturn self|other.Fr\   )r   r=   r   r   r   r^   r_   r   news      r&   __or__zBidictBase.__or__  s6    %+!!iikEE*
r%   c                    t        |t        j                        st        S | j	                  |      }|j                  | d       |S )zReturn other|self.Fr\   )r   r=   r   r   r4   r^   r   s      r&   __ror__zBidictBase.__ror__  s9    %+!!nnU#D5)
r%   c                ,    t        | j                        S )zThe number of contained items.)r   r)   rn   s    r&   __len__zBidictBase.__len__  s    4::r%   c                ,    t        | j                        S )z!Iterator over the contained keys.)iterr)   rn   s    r&   __iter__zBidictBase.__iter__  s    DJJr%   c                     | j                   |   S )u   *x.__getitem__(key) ⟺ x[key]*r~   r   s     r&   __getitem__zBidictBase.__getitem__
  s    zz#r%   c                    | j                   }| }t        | t              x}r| j                  }| j                  }| j
                  |t        |      |ffS )z&Return state information for pickling.)r4   r   rT   r-   rk   r   rs   )r_   r3   r   should_inverts       r&   
__reduce__zBidictBase.__reduce__  sS    nn(, 't-CDD=D--C<<D#tDz=!AAAr%   )returnNone)r3   type[BT]r   r   )r   zdict[str, t.Any])r$   )r`   MapOrItems[KT, VT]ra   r   r   r   )r   zBidictBase[VT, KT])r   str)r   zBidictKeysView[VT])r   zt.KeysView[KT])r   zt.ItemsView[KT, VT])r   zt.Anyr   bool)r   objectr   r   )r   r   r   r   r   r   r   zDedupResult[KT, VT])r   r   r   r   r   zOKT[KT]r   zOVT[VT]r   zUnwrites | Noner   r   )
r`   r   ra   zt.Mapping[str, VT]r]   zbool | Noner   zOnDup | Noner   r   )r_   r   r   r   )F)r   r   r   r   ri   r   r   r   )r   r   r   r   )r_   r   r   zt.Mapping[KT, VT]r   r   )r   int)r   t.Iterator[KT])r   r   r   r   )r   ztuple[t.Any, ...])-r    r!   r"   r#   r   r   __annotations__rs   r+   r,   r1   classmethodr2   r7   r6   rK   rQ   rb   propertyrk   rf   ri   ru   rx   rw   rR   r   r   r   r   r   r   r^   r   r   staticmethodr   r   r   r   r   r   r   r   __classcell__)r4   s   @r&   r(   r(   ;   s   @ F#### CGI?FBFI?F 98   $#
J 
J + +2 
+ 
+ 
 

.  @  %?I"Q&!(B1f8z "2"!50B
 !%#0B0B 0B
 0B 0B 
0Bd6 9 9 
Br%   r(   c                x    t        | j                  t        j                        sJ t	        | j                        S )z2Iterator over the contained keys in reverse order.)r   r)   r=   r>   r   rn   s    r&   rC   rC     s)    djj!,,///DJJr%   c                      e Zd ZdZy)rT   z<Base class for dynamically-generated inverse bidict classes.Nr   r$   r%   r&   rT   rT   %  s    Fr%   rT   )r_   zBidictBase[KT, t.Any]r   r   )4r#   
__future__r   typingr=   rg   	itertoolsr   operatorr   typesr   _abcr   _dupr	   r
   r   r   r   _excr   r   r   r   _iterr   r   _typingr   r   r   r   r   r   r   TupleOldKVOptionalDedupResultListAnyUnwritesTypeVarr   KeysView
ValuesViewr   r(   rC   r2   rT   r$   r%   r&   <module>r      s   # "     " &       " - % '          	
BR !jjr2v'66!''!%%*%&QYYt56QZZ^Q\\"%5 ]B%b"f- ]BB     G Gr%   