
    n9e.                     N   d dl mZmZmZmZmZmZmZmZm	Z	 d dl m
Z d dl mZ d dlZd Zd Zd Zd Zd	 Zd
 Z G d de      ZdZd Zd Z e       Zd ZeeedeefdZd Z G d de      ZedediZi Z d Z!d Z"d Z#eefdZ$ddeefdZ%ddeefdZ&d Z'i Z(d Z)d  Z*defd!Z+y)"    )	CheckedPMapCheckedPSetCheckedPVectorCheckedTypeInvariantException_restore_pickleget_typemaybe_parse_user_typemaybe_parse_many_user_types)optional)wrap_invariantNc                 6   t        t        |D cg c]5  }t        |j                  j	                  |i       j                               7 c}g             | |<   t        | j                               D ]!  \  }}t        |t              s|| |   |<   | |= # y c c}w N)dictsumlist__dict__getitems
isinstance_PField)dctbasesnamebkvs         :/usr/lib/python3/dist-packages/pyrsistent/_field_common.py
set_fieldsr      s    S%PQ$qzz~~dB7==?@PRTUVCISYY[! 1a!CIaLA Qs   :Bc                 Z     t        d  fd|D        D              }|rt        |dd      y )Nc              3   *   K   | ]  \  }}|s|  y wr    ).0is_ok
error_codes      r   	<genexpr>z*check_global_invariants.<locals>.<genexpr>   s!      W'8ujPU # Ws   c              3   .   K   | ]  } |        y wr   r"   )r#   	invariantsubjects     r   r&   z*check_global_invariants.<locals>.<genexpr>   s     H	7+Hs   r"   zGlobal invariant failed)tupler   )r)   
invariantserror_codess   `  r   check_global_invariantsr-      s:     WHZHW WK b2KLL     c                 f    t        |t              r| t        u r|j                  |      S  | ||      S r   )r   r   PFIELD_NO_SERIALIZER	serialize)
serializerformatvalues      r   r1   r1   !   s0    %%*8L*Lv&&fe$$r.   c                     |j                   rjt        fd|j                   D              sKt              }dj                  | j                  ||j                        }t	        | ||j                   ||      y y )Nc              3   H   K   | ]  }t        t        |              y wr   )r   r	   )r#   tr4   s     r   r&   zcheck_type.<locals>.<genexpr>)   s     Qj<Qs   "z'Invalid type for field {0}.{1}, was {2})typeanyr3   __name__
PTypeError)destination_clsfieldr   r4   actual_typemessages      `  r   
check_typer@   (   se    zz#QejjQQ5k;BB?C[C[]acncwcwx$

KQQ Rzr.   c                     t        |      t        u ryt        |      }t        |      dk(  ryt	        t        |d         |       S )NTr   F)r8   setr*   len
issubclassr	   )type_cls
field_typetypess      r   is_type_clsrH   /   s@    J3*E
5zQhuQx((33r.   c                     |syt        | |j                        sydt        j                  |j                        j
                  v S )NFignore_extra)rH   r8   inspect	signaturefactory
parameters)rE   r=   rJ   s      r   is_field_ignore_extra_complaintrO   8   s9    x,W..u}}=HHHHr.   c                   &    e Zd ZdZd Zed        Zy)r   r8   r(   initial	mandatory_factoryr2   c                 X    || _         || _        || _        || _        || _        || _        y r   rQ   )selfr8   r(   rR   rS   rM   r2   s          r   __init__z_PField.__init__G   s,    	""$r.   c                     | j                   t        u rUt        | j                        dk(  r=t	        t        | j                        d         }t        |t              r|j                  S | j                   S )N   r   )	rT   PFIELD_NO_FACTORYrC   r8   r	   r*   rD   r   create)rV   typs     r   rM   z_PField.factoryO   sV     ==--#dii.A2E5+A./C#{+zz!}}r.   N)r:   
__module____qualname__	__slots__rW   propertyrM   r"   r.   r   r   r   D   s     WI%  r.   r   r"   c                      y)N)TNr"   )_s    r   <lambda>rc   Z       r.   c                     | S r   r"   )xs    r   rc   rc   [   s    a r.   c                     |S r   r"   )rb   r4   s     r   rc   rc   ]   s     r.   Fc                    t        | t        t        t        f      rt        t	        |             }nt        t        |             }|t        k7  rt        |      rt        |      n|}t        ||||||      }t        |       |S )a  
    Field specification factory for :py:class:`PRecord`.

    :param type: a type or iterable with types that are allowed for this field
    :param invariant: a function specifying an invariant that must hold for the field
    :param initial: value of field if not specified when instantiating the record
    :param mandatory: boolean specifying if the field is mandatory or not
    :param factory: function called when field is set.
    :param serializer: function that returns a serialized version of the field
    )r8   r(   rR   rS   rM   r2   )r   r   rB   r*   r   r
   PFIELD_NO_INVARIANTcallabler   r   _check_field_parameters)	r8   r(   rR   rS   rM   r2   rG   invariant_functionr=   s	            r   r=   r=   `   s|    , $sE*+/56)$/06?CV6V[cdm[n	2t}*<g'ZQE E"Lr.   c                 l     j                   D ]F  }t        |t               rt        |t              r%t        dj	                  t        |                    j
                  t        urlt         j
                        sW j                   rKt         fd j                   D              s-t        dj	                  t         j
                                    t         j                        st        d      t         j                        st        d      t         j                        st        d      y )Nz Type parameter expected, not {0}c              3   J   K   | ]  }t        j                  |        y wr   )r   rR   )r#   r7   r=   s     r   r&   z*_check_field_parameters.<locals>.<genexpr>   s     "TA:emmQ#?"Ts    #zInitial has invalid type {0}zInvariant must be callablezFactory must be callablezSerializer must be callable)r8   r   str	TypeErrorr3   rR   PFIELD_NO_INITIALrj   r9   r(   rM   r2   )r=   r7   s   ` r   rk   rk      s    ZZ P!T":a+=>EEd1gNOOP }}--'JJs"T"TT6==d5==>QRSSEOO$455EMM"233E$$%566 &r.   c                   "     e Zd ZdZ fdZ xZS )r;   a  
    Raised when trying to assign a value with a type that doesn't match the declared type.

    Attributes:
    source_class -- The class of the record
    field -- Field name
    expected_types  -- Types allowed for the field
    actual_type -- The non matching type
    c                 d    t        t        | 
  |i | || _        || _        || _        || _        y r   )superr;   rW   source_classr=   expected_typesr>   )rV   ru   r=   rv   r>   argskwargs	__class__s          r   rW   zPTypeError.__init__   s6    j$($9&9(
,&r.   )r:   r]   r^   __doc__rW   __classcell__)ry   s   @r   r;   r;      s    ' 'r.   r;   PVectorPSetc                 0    t         | |f   }t        ||      S )z=Unpickling function for auto-generated PVec/PSet field types.)_seq_field_typesr   )checked_class	item_typedatatype_s       r   _restore_seq_field_pickler      s    ]I56E5$''r.   c                 2    dj                  d | D              S )z4Convert a tuple of types to a human-readable string. c              3   b   K   | ]'  }t        |      j                  j                          ) y wr   )r	   r:   
capitalize)r#   r\   s     r   r&   z"_types_to_names.<locals>.<genexpr>   s#     H38C=))446Hs   -/)join)rG   s    r   _types_to_namesr      s    77H%HHHr.   c                      t         j                   f      }||S  G  fdd       }t            }t        |j                        |z   |_        |t          f<   |S )zFCreate a subclass of the given checked class with the given item type.c                   &    e Zd ZW ZW Z fdZy)%_make_seq_field_type.<locals>.TheTypec                 ,    t         t        |       ffS r   )r   r   )rV   r   r   s    r   
__reduce__z0_make_seq_field_type.<locals>.TheType.__reduce__   s    -"ItDz:< <r.   N)r:   r]   r^   __type____invariant__r   )r   item_invariantr   s   r   TheTyper      s    &	<r.   r   )r   r   SEQ_FIELD_TYPE_SUFFIXESr   _checked_typesr:   )r   r   r   r   r   suffixs   ```   r   _make_seq_field_typer      sm      -!;<E< <- < %]3F&w'='=>GG18]I-.Nr.   c           	          t        | ||      |rdfd	}nj                  }t        |rt              n|d| ||            S )a  
    Create checked field for either ``PSet`` or ``PVector``.

    :param checked_class: ``CheckedPSet`` or ``CheckedPVector``.
    :param item_type: The required type for the items in the set.
    :param optional: If true, ``None`` can be used as a value for
        this field.
    :param initial: Initial value to pass to factory.

    :return: A ``field`` containing a checked class.
    c                 2    | y j                  | ||      S )N)_factory_fieldsrJ   r[   )argumentr   rJ   r   s      r   rM   z _sequence_field.<locals>.factory   s"    ~~h^j~kkr.   T)r8   rM   rS   r(   rR   )NF)r   r[   r=   optional_type)r   r   r   rR   r(   r   rM   r   s          @r   _sequence_fieldr      sO     #=)^LG	l ..mG,g D$ )+ +r.   c                 ,    t        t        | ||||      S )al  
    Create checked ``PSet`` field.

    :param item_type: The required type for the items in the set.
    :param optional: If true, ``None`` can be used as a value for
        this field.
    :param initial: Initial value to pass to factory if no value is given
        for the field.

    :return: A ``field`` containing a ``CheckedPSet`` of the given type.
    r(   r   )r   r   r   r   rR   r(   r   s        r   
pset_fieldr      s     ;	8W%.*8: :r.   c                 ,    t        t        | ||||      S )au  
    Create checked ``PVector`` field.

    :param item_type: The required type for the items in the vector.
    :param optional: If true, ``None`` can be used as a value for
        this field.
    :param initial: Initial value to pass to factory if no value is given
        for the field.

    :return: A ``field`` containing a ``CheckedPVector`` of the given type.
    r   )r   r   r   s        r   pvector_fieldr      s     >9h%.*8: :r.   c                      y)N)Tr   r"   )items    r   rc   rc     rd   r.   c                 0    t         | |f   }t        ||      S )z8Unpickling function for auto-generated PMap field types.)_pmap_field_typesr   )key_type
value_typer   r   s       r   _restore_pmap_field_pickler     s    h
23E5$''r.   c                      t         j                   f      }||S  G  fddt              }dj                  t	        |j
                        t	        |j                              |_        |t          f<   |S )zDCreate a subclass of CheckedPMap with the given key and value types.c                        e Zd ZW  ZW Zd Zy)%_make_pmap_field_type.<locals>.TheMapc                 R    t         | j                  | j                  t        |       ffS r   )r   __key_type____value_type__r   )rV   s    r   r   z0_make_pmap_field_type.<locals>.TheMap.__reduce__(  s+    .&&(;(;T$ZHJ Jr.   N)r:   r]   r^   r   r   r   )r   r   s   r   TheMapr   $  s    #	Jr.   r   z{0}To{1}PMap)r   r   r   r3   r   _checked_key_types_checked_value_typesr:   )r   r   r   r   s   ``  r   _make_pmap_field_typer     sx    !!8Z"89EJ J %++1123346FO /5h
*+Mr.   c                     t        | |      |rfd}nj                  }t        d        |rt              n||      S )ad  
    Create a checked ``PMap`` field.

    :param key: The required type for the keys of the map.
    :param value: The required type for the values of the map.
    :param optional: If true, ``None`` can be used as a value for
        this field.
    :param invariant: Pass-through to ``field``.

    :return: A ``field`` containing a ``CheckedPMap``.
    c                 ,    | y j                  |       S r   r   )r   r   s    r   rM   zpmap_field.<locals>.factoryB  s    }}X..r.   T)rS   rR   r8   rM   r(   )r   r[   r=   r   )r   r   r   r(   rM   r   s        @r   
pmap_fieldr   3  sH     #8Z8F	/ --4/7mF+V I7 7r.   ),pyrsistent._checked_typesr   r   r   r   r   r   r	   r
   r   r   r   r   rK   r   r-   r1   r@   rH   rO   objectr   PFIELD_NO_TYPEri   rZ   rq   r0   r=   rk   rp   r;   r   r   r   r   r   r   r   r   _validr   r   r   r   r"   r.   r   <module>r      s  
 
 
 @ 4 M%R4If * ,  H -  )<FW#4AU!H7(' '& I   (
I( 2#6+> $)",1:& ',R/!4:& 
!  (
* /4?R 7r.   