o
    a.                     @   sd  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d Zdd Zd	d
 Zdd Zdd Zdd ZG dd deZdZdd Zdd Ze Zdd ZeeedeefddZdd ZG dd deZeded iZi Z d!d" Z!d#d$ Z"d%d& Z#eefd'd(Z$ddeefd)d*Z%ddeefd+d,Z&d-d Z'i Z(d.d/ Z)d0d1 Z*defd2d3Z+dS )4    )	CheckedPMapCheckedPSetCheckedPVectorCheckedTypeInvariantException_restore_pickleget_typemaybe_parse_user_typemaybe_parse_many_user_types)optional)wrap_invariantNc                    sV   t t fdd|D g |  < t|  D ]\}}t|tr(||   |< | |= qd S )Nc                    s"   g | ]}t |j i  qS  )list__dict__getitems).0bnamer   :/usr/lib/python3/dist-packages/pyrsistent/_field_common.py
<listcomp>   s   " zset_fields.<locals>.<listcomp>)dictsumr   r   
isinstance_PField)dctbasesr   kvr   r   r   
set_fields   s    
r    c                    s4   t dd  fdd|D D }|rt|ddd S )Nc                 s   s    | ]	\}}|s|V  qd S Nr   )r   is_ok
error_coder   r   r   	<genexpr>   s   
 z*check_global_invariants.<locals>.<genexpr>c                 3   s    | ]}| V  qd S r!   r   )r   	invariantsubjectr   r   r$      s    r   zGlobal invariant failed)tupler   )r'   
invariantserror_codesr   r&   r   check_global_invariants   s   r+   c                 C   s&   t |tr| tu r||S | ||S r!   )r   r   PFIELD_NO_SERIALIZER	serialize)
serializerformatvaluer   r   r   r-   !   s   

r-   c                    sR   |j r%t fdd|j D s't  }d| j||j}t| ||j ||d S d S )Nc                 3   s    | ]
}t  t|V  qd S r!   )r   r   r   tr0   r   r   r$   )       zcheck_type.<locals>.<genexpr>z'Invalid type for field {0}.{1}, was {2})typeanyr/   __name__
PTypeError)destination_clsfieldr   r0   actual_typemessager   r3   r   
check_type(   s
   r=   c                 C   s:   t |tu rdS t|}t|dkrdS tt|d | S )NTr   F)r5   setr(   len
issubclassr   )type_cls
field_typetypesr   r   r   is_type_cls/   s   rD   c                 C   s*   |sdS t | |jsdS dt|jjv S )NFignore_extra)rD   r5   inspect	signaturefactory
parameters)rA   r:   rE   r   r   r   is_field_ignore_extra_complaint8   s
   rJ   c                   @   s$   e Zd ZdZdd Zedd ZdS )r   r5   r%   initial	mandatory_factoryr.   c                 C   s(   || _ || _|| _|| _|| _|| _d S r!   rK   )selfr5   r%   rL   rM   rH   r.   r   r   r   __init__G   s   
z_PField.__init__c                 C   s@   | j tu rt| jdkrtt| jd }t|tr|jS | j S )N   r   )	rN   PFIELD_NO_FACTORYr?   r5   r   r(   r@   r   create)rO   typr   r   r   rH   O   s
   
z_PField.factoryN)r7   
__module____qualname__	__slots__rP   propertyrH   r   r   r   r   r   D   s
    r   r   c                 C      dS )N)TNr   )_r   r   r   <lambda>Z       r[   c                 C   s   | S r!   r   )xr   r   r   r[   [   r\   c                 C   s   |S r!   r   )rZ   r0   r   r   r   r[   ]   r\   Fc           	      C   sf   t | tttfrtt| }ntt| }|tkr!t|r!t|n|}t	||||||d}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
    )r5   r%   rL   rM   rH   r.   )r   r   r>   r(   r
   r	   PFIELD_NO_INVARIANTcallabler   r   _check_field_parameters)	r5   r%   rL   rM   rH   r.   rC   invariant_functionr:   r   r   r   r:   `   s   r:   c                    s    j D ]}t|t st|tstdt |q jtur<t js< j r<t fdd j D s<tdt  jt j	sEtdt j
sNtdt jsWtdd S )Nz Type parameter expected, not {0}c                 3   s    | ]	}t  j|V  qd S r!   )r   rL   r1   r:   r   r   r$      s    z*_check_field_parameters.<locals>.<genexpr>zInitial has invalid type {0}zInvariant must be callablezFactory must be callablezSerializer must be callable)r5   r   str	TypeErrorr/   rL   PFIELD_NO_INITIALr_   r6   r%   rH   r.   )r:   r2   r   rb   r   r`      s&   




r`   c                       s    e Zd ZdZ fddZ  ZS )r8   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                    s2   t t| j|i | || _|| _|| _|| _d S r!   )superr8   rP   source_classr:   expected_typesr;   )rO   rg   r:   rh   r;   argskwargs	__class__r   r   rP      s
   
zPTypeError.__init__)r7   rU   rV   __doc__rP   __classcell__r   r   rk   r   r8      s    	r8   PVectorPSetc                 C      t | |f }t||S )z=Unpickling function for auto-generated PVec/PSet field types.)_seq_field_typesr   )checked_class	item_typedatatype_r   r   r   _restore_seq_field_pickle      
rw   c                 C   s   d dd | D S )z4Convert a tuple of types to a human-readable string. c                 s   s    | ]
}t |j V  qd S r!   )r   r7   
capitalize)r   rT   r   r   r   r$      r4   z"_types_to_names.<locals>.<genexpr>)join)rC   r   r   r   _types_to_names   s   r|   c                    sZ   t  f}|dur|S G  fddd }t  }t|j| |_|t  f< |S )zFCreate a subclass of the given checked class with the given item type.Nc                       s"   e Zd ZZZ fddZdS )z%_make_seq_field_type.<locals>.TheTypec                    s   t  t| ffS r!   )rw   r   rO   )rs   rt   r   r   
__reduce__   s   z0_make_seq_field_type.<locals>.TheType.__reduce__N)r7   rU   rV   __type____invariant__r~   r   rs   item_invariantrt   r   r   TheType   s    r   )rr   r   SEQ_FIELD_TYPE_SUFFIXESr|   _checked_typesr7   )rs   rt   r   rv   r   suffixr   r   r   _make_seq_field_type   s   r   c                    sH   t | || |rd fdd	}n j}t|rt n |d|||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.
    NFc                    s   | d u rd S  j | ||dS )N)_factory_fieldsrE   rS   )argumentr   rE   r   r   r   rH      s   z _sequence_field.<locals>.factoryT)r5   rH   rM   r%   rL   )NF)r   rS   r:   optional_type)rs   rt   r   rL   r%   r   rH   r   r   r   _sequence_field   s   r   c                 C      t t| ||||d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   rt   r   rL   r%   r   r   r   r   
pset_field      
r   c                 C   r   )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   r   r   r   pvector_field   r   r   c                 C   rY   )N)Try   r   )itemr   r   r   r[     r\   c                 C   rq   )z8Unpickling function for auto-generated PMap field types.)_pmap_field_typesr   )key_type
value_typeru   rv   r   r   r   _restore_pmap_field_pickle  rx   r   c                    sZ   t  f}|dur|S G  fdddt}dt|jt|j|_|t  f< |S )zDCreate a subclass of CheckedPMap with the given key and value types.Nc                       s   e Zd Z ZZdd ZdS )z%_make_pmap_field_type.<locals>.TheMapc                 S   s   t | j| jt| ffS r!   )r   __key_type____value_type__r   r}   r   r   r   r~   (  s   z0_make_pmap_field_type.<locals>.TheMap.__reduce__N)r7   rU   rV   r   r   r~   r   r   r   r   r   TheMap$  s    r   z{0}To{1}PMap)r   r   r   r/   r|   _checked_key_types_checked_value_typesr7   )r   r   rv   r   r   r   r   _make_pmap_field_type  s   r   c                    sB   t | | |r fdd}n j}td  |rt n ||d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                    s   | d u rd S   | S r!   r   )r   r   r   r   rH   B  s   
zpmap_field.<locals>.factoryT)rM   rL   r5   rH   r%   )r   rS   r:   r   )r   r   r   r%   rH   r   r   r   
pmap_field3  s   
r   ),pyrsistent._checked_typesr   r   r   r   r   r   r   r	   r
   r   r   r   rF   r    r+   r-   r=   rD   rJ   objectr   PFIELD_NO_TYPEr^   rR   re   r,   r:   r`   rd   r8   r   rr   rw   r|   r   r   r   r   _validr   r   r   r   r   r   r   r   <module>   sX   , 		
$


