
    ij@[                       U d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl ddl	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 erdd	lmZ d
dgej2                  ZdZdZded<    ed      Z ed      Z ede      Z edddddejB                  ddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d)d       Z"edddddejB                  ddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d*d       Z"edddddejB                  ddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d+d       Z"ejB                  ejB                  dddddejB                  ddd
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d,dZ"eddddddddddd
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d-d       Z#eddddddddddd
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d.d       Z# ee"f      	 d/ddddddddddd
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d0d       Z# G d  d!ee          Z$d"ddddddddddddejF                  d#	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d1d$Z%e	 d/dd%	 	 	 	 	 d2d&       Z&e	 	 	 	 	 	 d3d'       Z&	 d/dd%	 	 	 	 	 d4d(Z&y)5a  PyTree integration with :mod:`dataclasses`.

This module implements PyTree integration with :mod:`dataclasses` by redefining the :func:`field`,
:func:`dataclass`, and :func:`make_dataclass` functions. The :func:`register_node` function allows
registering existing :func:`dataclasses.dataclass`-decorated classes as pytree nodes. Other APIs
are re-exported from the original :mod:`dataclasses` module.

The PyTree integration allows dataclasses to be flattened and unflattened recursively. The fields
are stored in a special attribute named ``__optree_dataclass_fields__`` in the dataclass.

>>> import math
... import optree
...
>>> @optree.dataclasses.dataclass(namespace='my_module')
... class Point:
...     x: float
...     y: float
...     z: float = 0.0
...     norm: float = optree.dataclasses.field(init=False, pytree_node=False)
...
...     def __post_init__(self) -> None:
...         self.norm = math.hypot(self.x, self.y, self.z)
...
>>> point = Point(2.0, 6.0, 3.0)
>>> point
Point(x=2.0, y=6.0, z=3.0, norm=7.0)
>>> # Flatten without specifying the namespace
>>> optree.tree_flatten(point)  # `Point`s are leaf nodes
([Point(x=2.0, y=6.0, z=3.0, norm=7.0)], PyTreeSpec(*))
>>> # Flatten with the namespace
>>> accessors, leaves, treespec = optree.tree_flatten_with_accessor(point, namespace='my_module')
>>> accessors, leaves, treespec  # doctest: +IGNORE_WHITESPACE,ELLIPSIS
(
    [
        PyTreeAccessor(*.x, (DataclassEntry(field='x', type=<class '...Point'>),)),
        PyTreeAccessor(*.y, (DataclassEntry(field='y', type=<class '...Point'>),)),
        PyTreeAccessor(*.z, (DataclassEntry(field='z', type=<class '...Point'>),))
    ],
    [2.0, 6.0, 3.0],
    PyTreeSpec(CustomTreeNode(Point[()], [*, *, *]), namespace='my_module')
)
>>> point == optree.tree_unflatten(treespec, leaves)
True
    )annotationsN)*)MappingProxyType)TYPE_CHECKINGAnyCallableLiteralProtocolTypeVaroverload)dataclass_transform)DataclassEntry)Iterabler   register_node__optree_dataclass_fields__Tbool_PYTREE_NODE_DEFAULT_T_U_TypeT)boundinitreprhashcomparemetadatakw_onlydocpytree_nodec        	             y N )	defaultr   r   r   r   r   r   r   r    s	            g/var/www/html/emotional.easysim.app/public_html/venv/lib/python3.12/site-packages/optree/dataclasses.pyfieldr&   d        
    c        	             y r"   r#   )	default_factoryr   r   r   r   r   r   r   r    s	            r%   r&   r&   s   r'   r(   c                     y r"   r#   r   s           r%   r&   r&      s     r(   )
r$   r*   r   r   r   r   r   r   r   r    c        
           |xs i j                         }|	|j                  dt              }	|	|d<   | ||||||d}
t        j                  dk\  r||
d<   n|t
        j                  urt        d      t        j                  dk\  r||
d<   n|t        d      |s|	rt        d	t         d
      t        j                  di |
S )a*  Field factory for :func:`dataclass`.

    This factory function is used to define the fields in a dataclass. It is similar to the field
    factory :func:`dataclasses.field`, but with an additional ``pytree_node`` parameter. If
    ``pytree_node`` is :data:`True` (default), the field will be considered a child node in the
    PyTree structure which can be recursively flattened and unflattened. Otherwise, the field will
    be considered as PyTree metadata.

    Setting ``pytree_node`` in the field factory is equivalent to setting a key ``'pytree_node'`` in
    ``metadata`` in the original field factory. The ``pytree_node`` value can be accessed using
    ``field.metadata['pytree_node']``. If ``pytree_node`` is :data:`None`, the value
    ``metadata.get('pytree_node', True)`` will be used.

    .. note::
        If a field is considered a child node, it must be included in the argument list of the
        :meth:`__init__` method, i.e., passes ``init=True`` in the field factory.

    Args:
        pytree_node (bool or None, optional): Whether the field is a PyTree node.
        **kwargs (optional): Optional keyword arguments passed to :func:`dataclasses.field`.

    Returns:
        dataclasses.Field: The field defined using the provided arguments with
        ``field.metadata['pytree_node']`` set.
    r    )r$   r*   r   r   r   r   r      
   r   z4field() got an unexpected keyword argument 'kw_only'r.      r   z0field() got an unexpected keyword argument 'doc'zN`pytree_node=True` is not allowed for non-init fields. Please explicitly set `'.field(init=False, pytree_node=False)`.r#   )
copygetr   sysversion_infodataclassesMISSING	TypeError__name__r&   )r$   r*   r   r   r   r   r   r   r   r    kwargss              r%   r&   r&      s    L B$$&Hll=2FG)H] *F 7"#y	++	+NOO
7"u	JKKK&&.Z/VX
 	

 &v&&r(   F
r   r   eqorderunsafe_hashfrozen
match_argsr   slotsweakref_slotc                     y r"   r#   )r   r   r=   r>   r?   r@   rA   r   rB   rC   	namespaces              r%   	dataclassrF      s     "%r(   c                   y r"   r#   )clsr   r   r=   r>   r?   r@   rA   r   rB   rC   rE   s               r%   rF   rF      s      r(   )field_specifiersc              ~   ddl m} ||||||dt        j                  dk\  r|d<   |d<   |	d<   n-|durt	        d	      |d
urt	        d      |	d
urt	        d      t        j                  dk\  r|
d<   n|
d
urt	        d      | 	dfd}|S t        j                  |       st	        dt         d| d      t        | j                  v r t	        dt         d| j                   d      |urt        t              st	        dd      dk(  r|t        j                  | fi } t        |       S )a  Dataclass decorator with PyTree integration.

    Args:
        cls (type or None, optional): The class to decorate. If :data:`None`, return a decorator.
        namespace (str): The registry namespace used for the PyTree registration.
        **kwargs (optional): Optional keyword arguments passed to :func:`dataclasses.dataclass`.

    Returns:
        type or callable: The decorated class with PyTree integration or decorator function.
    r   __GLOBAL_NAMESPACEr   r   r=   r>   r?   r@   r-   rA   r   rB   Tz;dataclass() got an unexpected keyword argument 'match_args'Fz8dataclass() got an unexpected keyword argument 'kw_only'z6dataclass() got an unexpected keyword argument 'slots'r.      rC   z=dataclass() got an unexpected keyword argument 'weakref_slot'c                     t        | fdiS )NrE   )rF   )rH   r;   rE   s    r%   	decoratorzdataclass.<locals>.decorator5  s    S@I@@@r(   @z0.dataclass() can only be used with classes, not .z".dataclass() cannot be applied to z more than once.$The namespace must be a string, got  rE   rH   r   returnr   )optree.registryrL   r5   r6   r9   inspectisclassr:   _FIELDS__dict__
isinstancestrr7   rF   r   )rH   r   r   r=   r>   r?   r@   rA   r   rB   rC   rE   GLOBAL_NAMESPACErQ   r;   s              `  @r%   rF   rF      s~   8 G "F 7")|#yw	4	UVV		RSS	e	PQQ
7"!-~	U	"WXX
{	A ??3!H:%UVYU\\]^__#,,z;CLL>IYZ
 	
 ((Is1K>ym1MNNB$	



.v
.C	22r(   c                  Z    e Zd Zddddddddddd
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZy)_DataclassDecoratorTFr<   c      
            t         r"   )NotImplementedError)selfrH   r   r   r=   r>   r?   r@   rA   r   rB   rC   s               r%   __call__z_DataclassDecorator.__call__J  s
      "!r(   N)rH   r   r   r   r   r   r=   r   r>   r   r?   r   r@   r   rA   r   r   r   rB   r   rC   r   rX   r   )r:   
__module____qualname__rf   r#   r(   r%   rb   rb   I  s     !"""
 " " " " " " " " " " 
"r(   rb   r#   )basesnsr   r   r=   r>   r?   r@   rA   r   rB   rC   modulerQ   c               x   ddl m} t        |t              s|&||u st        |t              r||}}n|t        d      ||urt        |t              st        d|d      |dk(  r|}||||||	d}||d	}t        j                  d
k\  r|
|d<   ||d<   ||d<   n-|
durt        d      |durt        d      |durt        d      t        j                  dk\  r||d<   n|durt        d      t        j                  dk\  r"|	 t        j                  d      xs d}||d<   n|t        d      d}t        j                  dk\  r;|t         j"                  t"        fv rt%        j&                  t"        |      }d}||d<   n|t         j"                  urt        d      t!        j(                  | fd |i||}|st+        ||      }|S # t        $ rg t        j                  t        t              5  t        j                  d      j                  j                  dd      }ddd       n# 1 sw Y   nxY wY w xY w)!a  Make a new dynamically created dataclass with PyTree integration.

    The dataclass name will be ``cls_name``. ``fields`` is an iterable of either (name), (name, type),
    or (name, type, Field) objects. If type is omitted, use the string :data:`typing.Any`. Field
    objects are created by the equivalent of calling :func:`field` (name, type [, Field-info]).

    The ``namespace`` parameter is the PyTree registration namespace which should be a string. The
    ``namespace`` in the original :func:`dataclasses.make_dataclass` function is renamed to ``ns``
    to avoid conflicts.

    The remaining parameters are passed to :func:`dataclasses.make_dataclass`.
    See :func:`dataclasses.make_dataclass` for more information.

    Args:
        cls_name: The name of the dataclass.
        fields (Iterable[str | tuple[str, Any] | tuple[str, Any, Any]]): An iterable of either
            (name), (name, type), or (name, type, Field) objects.
        namespace (str): The registry namespace used for the PyTree registration.
        ns (dict or None, optional): The namespace used in dynamic type creation.
            See :func:`dataclasses.make_dataclass` and the builtin :func:`type` function for more
            information.
        **kwargs (optional): Optional keyword arguments passed to :func:`dataclasses.make_dataclass`.

    Returns:
        type: The dynamically created dataclass with PyTree integration.
    r   rK   Nz?make_dataclass() missing 1 required keyword-only argument: 'ns'rT   rS   rU   rM   )ri   rE   r-   rA   r   rB   Tz@make_dataclass() got an unexpected keyword argument 'match_args'Fz=make_dataclass() got an unexpected keyword argument 'kw_only'z;make_dataclass() got an unexpected keyword argument 'slots'rN   rC   zBmake_dataclass() got an unexpected keyword argument 'weakref_slot')r.         __main__r:   rk   z<make_dataclass() got an unexpected keyword argument 'module'r0   rV   rQ   z?make_dataclass() got an unexpected keyword argument 'decorator'fields)rY   rL   r^   dictr_   r9   r5   r6   _getframemodulenameAttributeError
contextlibsuppress
ValueError	_getframe	f_globalsr4   r7   rF   	functoolspartialmake_dataclassr   )cls_namerp   ri   rj   r   r   r=   r>   r?   r@   rA   r   rB   rC   rk   rQ   rE   r`   dataclass_kwargsmake_dataclass_kwargsregistered_by_decoratorrH   s                         r%   r{   r{   ^  s   ` G)T"i&7!!ZC%8%r	BZ]^^((Is1K>ym1MNNB$	 " 
 7")3&&-#$)!	4	Z[[		WXX	e	UVV
7"+7(	U	"\]]
7">T003Az
 +1h'		VWW#
7"..	::!)))yII&*#-6k*	+//	/YZZ,,   	C #C95J3 " T((D T ]]1-77;;J
SFT T TTs*   G	 	'H900H) 	H9)H2	.H98H9rV   c                   y r"   r#   rH   rE   s     r%   r   r     s     "%r(   c                   y r"   r#   r   s     r%   r   r     s     r(   c              `    ddl m}  |u st         t              r"t	        d       dk(  rt	        d      d c t	        d       dfd}|S t        j                         st        d	 d
      t        j                         st         d      t         j                  v rt        d j                   d      |urt        t              st        dd
      dk(  r| j                  j                  s1t        j                   d j                  dt         dt"        d       i }i t        j$                         D ]z  }|j&                  j)                  dt*              r<|j                  s t        d|j,                  dt         d      |||j,                  <   _|j                  sl||j,                  <   | t/        |      t1        |      }t1              }t3         t        ||f       	 	 	 	 dfd}d fd}	ddl m}
  |
 ||	t6                S )a+  Register an existing dataclass as a pytree node.

    This function takes an existing :func:`dataclasses.dataclass`-decorated class and registers it
    as a pytree node. It can be used as a direct function call or as a decorator.

    Fields with ``metadata['pytree_node']`` set to :data:`True` (or not set, defaulting to
    :data:`True`) are treated as children, while init fields with ``metadata['pytree_node']`` set
    to :data:`False` are treated as metadata.

    Usage::

        # Direct function call
        register_node(Point, namespace='my-namespace')

        # As a decorator
        @register_node(namespace='my-namespace')
        @dataclasses.dataclass
        class Point:
            x: float
            y: float

    Args:
        cls (type, optional): An existing dataclass. If :data:`None`, return a decorator.
        namespace (str): The registry namespace used for the PyTree registration.

    Returns:
        type or callable: The same class, now registered as a pytree node, or a decorator function.

    .. versionadded:: 0.20.0
    r   rK   Nz?Cannot specify `namespace` when the first argument is a string.rU   z(The namespace cannot be an empty string.z<Must specify `namespace` when the first argument is a class.c                   t        |       S )NrV   )r   r   s    r%   rQ   z register_node.<locals>.decorator  s     	::r(   zExpected a class, got rS   z is not a dataclass.zCannot register z! as a pytree node more than once.rT   z
Dataclass zE was defined with `init=False`. `tree_unflatten()` may fail because `z>.register_node()` reconstructs instances with `cls(**kwargs)`.   )
stacklevelr    zPyTree node field z> must be included in `__init__()`. Or you can explicitly set `r2   c               `     t         fdD              }t         fdD              }||fS )Nc              3  6   K   | ]  }t        |        y wr"   getattr.0nameobjs     r%   	<genexpr>z6register_node.<locals>.flatten_func.<locals>.<genexpr>Q  s     Md+Ms   c              3  :   K   | ]  }|t        |      f  y wr"   r   r   s     r%   r   z6register_node.<locals>.flatten_func.<locals>.<genexpr>R  s     P$T 23Ps   )tuple)r   childrenr   children_field_namesmetadata_fieldss   `  r%   flatten_funcz#register_node.<locals>.flatten_funcI  s4     M8LMMPPP#777r(   c               `    t        t        |            }|j                  |         di |S )Nr#   )rq   zipupdate)r   r   r;   r   rH   s      r%   unflatten_funcz%register_node.<locals>.unflatten_funcV  s-    c.9:h}V}r(   )register_pytree_node)path_entry_typerE   rW   )r   r   rX   zCtuple[tuple[_U, ...], tuple[tuple[str, Any], ...], tuple[str, ...]])r   ztuple[tuple[str, Any], ...]r   ztuple[_U, ...]rX   r   )rY   rL   r^   r_   rv   rZ   r[   r9   r7   is_dataclassr\   r]   r:   __dataclass_params__r   warningswarnUserWarningrp   r   r4   r   r   r   r   setattrr   r   )rH   rE   r`   rQ   children_fieldsfchildren_fields_proxymetadata_fields_proxyr   r   r   r   r   s   ``         @@r%   r   r     sX   J G
*S#"6 ^__"9GHHsYWXX
{	; ??30q9::##C(3'!5677#,,s||n,MN
 	
 ((Is1K>ym1MNNB$	##((( )zWY 	
 OO$ 	(::>>-)=>66(
 322:;bd  '(OAFF#VV&'OAFF#	( !1,_=,_=C02GHI
8
8

8
 5& Jr(   )r$   r   r   r   r   r   r   bool | Noner   r   r   dict[Any, Any] | Noner   #bool | Literal[dataclasses.MISSING]r   
str | Noner    r   rX   r   )r*   zCallable[[], _T]r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r   rX   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r   rX   r   )r$   r   r*   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r   rX   r   )r   r   r   r   r=   r   r>   r   r?   r   r@   r   rA   r   r   r   rB   r   rC   r   rE   r_   rX   Callable[[_TypeT], _TypeT])rH   r   r   r   r   r   r=   r   r>   r   r?   r   r@   r   rA   r   r   r   rB   r   rC   r   rE   r_   rX   r   r"   )rH   z_TypeT | Noner   r   r   r   r=   r   r>   r   r?   r   r@   r   rA   r   r   r   rB   r   rC   r   rE   r_   rX   #_TypeT | Callable[[_TypeT], _TypeT])$r|   r_   rp   z6Iterable[str | tuple[str, Any] | tuple[str, Any, Any]]ri   ztuple[type, ...]rj   zdict[str, Any] | Noner   r   r   r   r=   r   r>   r   r?   r   r@   r   rA   r   r   r   rB   r   rC   r   rk   r   rQ   z_DataclassDecorator[_TypeT]rE   r_   rX   r   )rH   r   rE   r   rX   r   )rH   r   rE   r_   rX   r   )rH   z_TypeT | str | NonerE   r   rX   r   )'__doc__
__future__r   rt   r7   ry   rZ   r5   r   typesr   typingr   r   r   r	   r
   r   r   typing_extensionsr   optree.accessorsr   collections.abcr   __all__r\   r   __annotations__r   r   typer   r8   r&   rF   rb   r{   r   r#   r(   r%   <module>r      s  +^ #     
   " U U U 1 + ( 
  (! d ! T]T]		& 
 &*3>3F3F#  	
   $ 1 
   
 
 &*3>3F3F#%  	
   $ 1 
   
 
 &*3>3F3F#


 
 	

 
 $
 1
 

 
 	
 

 &&&..&*3>3F3F#E'E' E' 	E'
 E' E' E' $E' 1E' 
E' E' 	E'P 
 %
% % 		%
 % % % % % % % %  % 
%  

 	 	
  	          
$ uh/I3 I3	I3 	I3
 I3 	I3 I3 I3 I3 I3 I3 I3 I3 I3 )I3 0I3X"(6* "4 ! $-8-B-B%ww Cw
 w 	w w w 	w w w w w w w  !w" #w$ +%w& 'w( )wt 
% !	%	% 	%
  % 
% 
	 	
  
  $z !	z	z 	z
 )zr(   