o
    2ht8                     @  s   d Z ddlmZ ddlZddlZddlmZmZmZ ddl	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mZ e
rLddlZddlmZmZ g d	Zejd
krYddini Zejd/dddddeG dd dZ[edZedddZ edddZ!edddZ"G dd deZ#G dd deZ$G dd deZ%G dd deZ&G dd de$ee  Z'G d d! d!e$ee!e"f Z(G d"d# d#e'e Z)G d$d% d%e'e Z*G d&d' d'e%Z+G d(d) d)e,ed*f Z-d+e.Z/Z0eD ]Z/e1 e/ Z0e2e0e3se4d,e0d-d.e0_5e6ee/e0 q[/[0dS )0zAccess support for pytrees.    )annotationsN)IterableMappingSequence)TYPE_CHECKINGAnyClassVarGenericLiteralTypeVaroverload)Self)
PyTreeKind)
NamedTupleStructSequence)PyTreeEntryGetItemEntryGetAttrEntryFlattenedEntry	AutoEntrySequenceEntryMappingEntryNamedTupleEntryStructSequenceEntryDataclassEntryPyTreeAccessor)   
   slotsTF)initrepreqfrozenc                   @  sr   e Zd ZU dZded< ded< ded< d"d
dZd#ddZd$ddZd%ddZd&ddZ	d'ddZ
d(d)dd Zd!S )*r   zBase class for path entries.r   entrybuiltins.typetyper   kindreturnNonec                C  s,   | j tjkr
td| j tjkrtddS )zPost-initialize the path entry.z Cannot create a leaf path entry.z$Cannot create a path entry for None.N)r&   r   LEAF
ValueErrorNONEself r.   K/var/www/html/chatgem/venv/lib/python3.10/site-packages/optree/accessors.py__post_init__=   s
   zPyTreeEntry.__post_init__objc             
   C  sD   z|| j  W S  ty! } zt| jd|d| j |d}~ww )Get the child object.z cannot access through z via entry N)r#   	TypeError	__class__)r-   r1   exr.   r.   r/   __call__D   s   zPyTreeEntry.__call__otherobjectr   c                C  s4   t |trt| |fS t |trt| g|R S tS )z8Join the path entry with another path entry or accessor.)
isinstancer   r   NotImplementedr-   r7   r.   r.   r/   __add__M   s
   

zPyTreeEntry.__add__boolc                C  sR   t |to(| j| j| j| jjjj| jj	jjf|j|j|j|jjjj|jj	jjfkS )z$Check if the path entries are equal.)
r9   r   r#   r%   r&   r4   r6   __code__co_codecodifyr;   r.   r.   r/   __eq__U   s   




zPyTreeEntry.__eq__intc                C  s(   t | j| j| j| jjjj| jjjjfS )zGet the hash of the path entry.)	hashr#   r%   r&   r4   r6   r>   r?   r@   r,   r.   r.   r/   __hash__h   s   

zPyTreeEntry.__hash__strc                C     | j j d| jd| jdS ))Get the representation of the path entry.z(entry=, type=))r4   __name__r#   r%   r,   r.   r.   r/   __repr__t      zPyTreeEntry.__repr__ nodec                C     | d| j dS )+Generate code for accessing the path entry.z[<flat index z>]r#   r-   rN   r.   r.   r/   r@   x      zPyTreeEntry.codifyN)r'   r(   r1   r   r'   r   )r7   r8   r'   r   r7   r8   r'   r=   r'   rB   r'   rE   rM   rN   rE   r'   rE   )rJ   
__module____qualname____doc____annotations__r0   r6   r<   rA   rD   rK   r@   r.   r.   r.   r/   r   5   s   
 


	


r   _T_T_co)	covariant_KT_co_VT_coc                      s0   e Zd ZU dZdZded< d fddZ  ZS )r   zTA generic path entry class that determines the entry type on creation automatically.r.   ClassVar[tuple[()]]	__slots__r#   r   r%   r$   r&   r   r'   r   c                  s   ddl m}m} | turt | S |tjkrtd|d||r&t	}n!||r-t
}nt|r5t}nt|tr=t}n
t|trEt}nt}t|tsR||||S td)zCreate a new path entry.r   )is_namedtuple_classis_structseq_classz5Cannot create an automatic path entry for PyTreeKind .zUnreachable code.)optree.typingre   rf   r   super__new__r   CUSTOMr*   r   r   dataclassesis_dataclassr   
issubclassr   r   r   r   r   NotImplementedError)clsr#   r%   r&   re   rf   path_entry_typer4   r.   r/   rj      s&   	




zAutoEntry.__new__)r#   r   r%   r$   r&   r   r'   r   )rJ   rZ   r[   r\   rd   r]   rj   __classcell__r.   r.   rr   r/   r      s   
 r   c                   @  s4   e Zd ZU dZdZded< ddd	ZddddZdS )r   zWA generic path entry class for nodes that access their children by :meth:`__getitem__`.r.   rc   rd   r1   r   r'   c                C  
   || j  S r2   rQ   r-   r1   r.   r.   r/   r6         
zGetItemEntry.__call__rM   rN   rE   c                C  rO   )rP   []rQ   rR   r.   r.   r/   r@      rS   zGetItemEntry.codifyNrT   rX   rY   )rJ   rZ   r[   r\   rd   r]   r6   r@   r.   r.   r.   r/   r      s
   
 
r   c                   @  sJ   e Zd ZU dZdZded< ded< eddd	ZdddZddddZ	dS )r   zWA generic path entry class for nodes that access their children by :meth:`__getattr__`.r.   rc   rd   rE   r#   r'   c                C     | j S zGet the attribute name.rQ   r,   r.   r.   r/   name      zGetAttrEntry.namer1   r   c                C  s   t || jS ru   )getattrr|   rv   r.   r.   r/   r6      s   zGetAttrEntry.__call__rM   rN   c                C     | d| j  S rP   rg   r|   rR   r.   r.   r/   r@         zGetAttrEntry.codifyNrW   rT   rX   rY   )
rJ   rZ   r[   r\   rd   r]   propertyr|   r6   r@   r.   r.   r.   r/   r      s   
 
r   c                   @  s   e Zd ZU dZdZded< dS )r   z2A fallback path entry class for flattened objects.r.   rc   rd   N)rJ   rZ   r[   r\   rd   r]   r.   r.   r.   r/   r      s   
 r   c                   @  P   e Zd ZU dZdZded< ded< ded< edd
dZdddZdddZ	dS )r   z!A path entry class for sequences.r.   rc   rd   rB   r#   zbuiltins.type[Sequence[_T_co]]r%   r'   c                C  rz   )zGet the index.rQ   r,   r.   r.   r/   index   r}   zSequenceEntry.indexr1   Sequence[_T_co]r_   c                C  rt   ru   )r   rv   r.   r.   r/   r6      rw   zSequenceEntry.__call__rE   c                C  rF   )rG   z(index=rH   rI   )r4   rJ   r   r%   r,   r.   r.   r/   rK      rL   zSequenceEntry.__repr__NrV   )r1   r   r'   r_   rW   )
rJ   rZ   r[   r\   rd   r]   r   r   r6   rK   r.   r.   r.   r/   r         
 
r   c                   @  r   )r   z A path entry class for mappings.r.   rc   rd   ra   r#   z&builtins.type[Mapping[_KT_co, _VT_co]]r%   r'   c                C  rz   )zGet the key.rQ   r,   r.   r.   r/   key   r}   zMappingEntry.keyr1   Mapping[_KT_co, _VT_co]rb   c                C  rt   ru   )r   rv   r.   r.   r/   r6     rw   zMappingEntry.__call__rE   c                C  rF   )rG   z(key=rH   rI   )r4   rJ   r   r%   r,   r.   r.   r/   rK     rL   zMappingEntry.__repr__N)r'   ra   )r1   r   r'   rb   rW   )
rJ   rZ   r[   r\   rd   r]   r   r   r6   rK   r.   r.   r.   r/   r      r   r   c                   @  h   e Zd ZU dZdZded< ded< ded< d	ed
< edddZedddZdddZ	ddddZ
dS )r   z*A path entry class for namedtuple objects.r.   rc   rd   rB   r#   zbuiltins.type[NamedTuple[_T]]r%   zLiteral[PyTreeKind.NAMEDTUPLE]r&   r'   tuple[str, ...]c                C     ddl m} || jS )Get the field names.r   )namedtuple_fields)rh   r   r%   )r-   r   r.   r.   r/   fields     
zNamedTupleEntry.fieldsrE   c                C     | j | j S zGet the field name.r   r#   r,   r.   r.   r/   field     zNamedTupleEntry.fieldc                C  rF   rG   z(field=rH   rI   r4   rJ   r   r%   r,   r.   r.   r/   rK   "  rL   zNamedTupleEntry.__repr__rM   rN   c                C  r   r   r   rR   r.   r.   r/   r@   &  r   zNamedTupleEntry.codifyNr'   r   rW   rX   rY   rJ   rZ   r[   r\   rd   r]   r   r   r   rK   r@   r.   r.   r.   r/   r        
 
r   c                   @  r   )r   z0A path entry class for PyStructSequence objects.r.   rc   rd   rB   r#   z!builtins.type[StructSequence[_T]]r%   z"Literal[PyTreeKind.STRUCTSEQUENCE]r&   r'   r   c                C  r   )r   r   )structseq_fields)rh   r   r%   )r-   r   r.   r.   r/   r   4  r   zStructSequenceEntry.fieldsrE   c                C  r   r   r   r,   r.   r.   r/   r   ;  r   zStructSequenceEntry.fieldc                C  rF   r   r   r,   r.   r.   r/   rK   @  rL   zStructSequenceEntry.__repr__rM   rN   c                C  r   r   r   rR   r.   r.   r/   r@   D  r   zStructSequenceEntry.codifyNr   rW   rX   rY   r   r.   r.   r.   r/   r   +  r   r   c                   @  sh   e Zd ZU dZdZded< ded< edd	d
ZedddZedddZ	edddZ
dddZdS )r   z#A path entry class for dataclasses.r.   rc   rd   z	str | intr#   r'   r   c                C     t dd t| jD S )zGet all field names.c                 s      | ]}|j V  qd S Nr   .0fr.   r.   r/   	<genexpr>S      z(DataclassEntry.fields.<locals>.<genexpr>tuplerl   r   r%   r,   r.   r.   r/   r   P     zDataclassEntry.fieldsc                C  r   )zGet the init field names.c                 s  s    | ]	}|j r|jV  qd S r   )r   r|   r   r.   r.   r/   r   X  s    z-DataclassEntry.init_fields.<locals>.<genexpr>r   r,   r.   r.   r/   init_fieldsU  r   zDataclassEntry.init_fieldsrE   c                C  s   t | jtr| j| j S | jS r   )r9   r#   rB   r   r,   r.   r.   r/   r   Z  s   zDataclassEntry.fieldc                C  rz   r{   r   r,   r.   r.   r/   r|   a  r}   zDataclassEntry.namec                C  rF   r   r   r,   r.   r.   r/   rK   f  rL   zDataclassEntry.__repr__Nr   rW   )rJ   rZ   r[   r\   rd   r]   r   r   r   r   r|   rK   r.   r.   r.   r/   r   I  s   
 r   c                      s   e Zd ZU dZdZded< ed1ddZd2d3 fddZd4ddZ	e
d5ddZe
d6ddZd7 fddZd8ddZd9 fd!d"Zd9 fd#d$Zd: fd&d'Zd; fd(d)Zd< fd+d,Zd=d>d/d0Z  ZS )?r   zA path class for PyTrees.r.   rc   rd   r'   tuple[Any, ...]c                C  s   t dd | D S )zGet the path of the accessor.c                 s  r   r   rQ   )r   er.   r.   r/   r   s  r   z&PyTreeAccessor.path.<locals>.<genexpr>)r   r,   r.   r.   r/   pathp  s   zPyTreeAccessor.pathr   Iterable[PyTreeEntry]r   c                  sF   t |ttfst|}tdd |D std|dt | |S )zCreate a new accessor instance.c                 s  s    | ]}t |tV  qd S r   )r9   r   )r   pr.   r.   r/   r   y  s    z)PyTreeAccessor.__new__.<locals>.<genexpr>z$Expected a path of PyTreeEntry, got rg   )r9   listr   allr3   ri   rj   )rp   r   rr   r.   r/   rj   u  s
   zPyTreeAccessor.__new__r1   r   c                C  s   | D ]}||}q|S ru   r.   )r-   r1   r#   r.   r.   r/   r6   }  s   
zPyTreeAccessor.__call__r   rB   r   c                C     d S r   r.   r-   r   r.   r.   r/   __getitem__     zPyTreeAccessor.__getitem__slicec                C  r   r   r.   r   r.   r.   r/   r     r   int | slicePyTreeEntry | Selfc                  s(   t |tr| t |S t |S )z6Get the child path entry or an accessor for a subpath.)r9   r   r4   ri   r   r   rr   r.   r/   r     s   
r7   r8   c                C  s@   t |tr| g | |R S t |tr| g | |R S tS )z6Join the accessor with another path entry or accessor.)r9   r   r4   r   r:   r;   r.   r.   r/   r<     s
   

zPyTreeAccessor.__add__valuec                     |  t |S zRepeat the accessor.)r4   ri   __mul__r-   r   rr   r.   r/   r     rS   zPyTreeAccessor.__mul__c                  r   r   )r4   ri   __rmul__r   rr   r.   r/   r     rS   zPyTreeAccessor.__rmul__r=   c                  s   t |to
t |S )z!Check if the accessors are equal.)r9   r   ri   rA   r;   rr   r.   r/   rA     s   zPyTreeAccessor.__eq__c                  s
   t   S )zGet the hash of the accessor.)ri   rD   r,   rr   r.   r/   rD     rw   zPyTreeAccessor.__hash__rE   c                  s$   | j j d|   dt   dS )z'Get the representation of the accessor.(z, rI   )r4   rJ   r@   ri   rK   r,   rr   r.   r/   rK     s   $zPyTreeAccessor.__repr__*rootc                C  s   |}| D ]}| |}q|S )z%Generate code for accessing the path.)r@   )r-   r   stringr#   r.   r.   r/   r@     s   zPyTreeAccessor.codify)r'   r   )r.   )r   r   r'   r   rT   )r   rB   r'   r   )r   r   r'   r   )r   r   r'   r   )r7   r8   r'   r   )r   rB   r'   r   rU   rV   rW   )r   )r   rE   r'   rE   )rJ   rZ   r[   r\   rd   r]   r   r   rj   r6   r   r   r<   r   r   rA   rD   rK   r@   rs   r.   r.   rr   r/   r   k  s&   
 

r   .rM   zExpected a class, got rg   optreer.   )7r\   
__future__r   rl   syscollections.abcr   r   r   typingr   r   r   r	   r
   r   r   typing_extensionsr   	optree._C_Cr   builtinsrh   r   r   __all__version_infoSLOTS	dataclassr   r^   r_   ra   rb   r   r   r   r   r   r   r   r   r   r   r   r8   _name_clsglobalsr9   r%   r3   rZ   setattrr.   r.   r.   r/   <module>   sN   $G2"
I
