
    0Vh9                       d Z ddlmZ ddl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/Z0 ddlm1Z2 ddlm3Z4 ddlm5Z6 ddlm7Z8 ddlm9Z: ddlm;Z< ddlm=Z> ddlm?Z@ ddlmAZB ddlmCZD ddlmEZF dd lmGZH dd!lmIZJ dd"lmKZL dd#lmMZN dd$lmOZP dd%lmQZR dd&lmSZT dd'lmUZV dd(lmWZX dd)lmYZZ dd*l[m\Z\ dd+l[m]Z^ dd,l[m_Z` dd-l[maZb dd.lcmdZdmeZe dd/lfmgZg g d0Zher&dd1limjZjmkZk dd2lmlZlmmZm dd3lnmoZo  eod4      Zp emd5      Zq G d6 d7e      Zrer G d8 d9er      Zsdd:d=d;Ztydd:d>d<Zty)?a  Utilities for working with ``PyTree``\s.

The :mod:`optree.pytree` namespace contains aliases of ``optree.tree_*`` utilities.

>>> import optree.pytree as pytree
>>> tree = {'b': (2, [3, 4]), 'a': 1, 'c': None, 'd': 5}
>>> leaves, treespec = pytree.flatten(tree)
>>> leaves, treespec  # doctest: +IGNORE_WHITESPACE
(
    [1, 2, 3, 4, 5],
    PyTreeSpec({'a': *, 'b': (*, [*, *]), 'c': None, 'd': *})
)
>>> tree == pytree.unflatten(treespec, leaves)
True

.. versionadded:: 0.14.1
    )annotationsN)all)
ModuleType)TYPE_CHECKING)PyTreeEntry)tree_accessors)tree_all)tree_any)tree_broadcast_common)tree_broadcast_map) tree_broadcast_map_with_accessor)tree_broadcast_map_with_path)tree_broadcast_prefix)tree_flatten)tree_flatten_one_level)tree_flatten_with_accessor)tree_flatten_with_path)tree_is_leaf)	tree_iter)tree_leaves)tree_map)	tree_map_)tree_map_with_accessor)tree_map_with_accessor_)tree_map_with_path)tree_map_with_path_)tree_max)tree_min)tree_partition)
tree_paths)tree_reduce)tree_replace_nones)tree_structure)tree_sum)tree_transpose)tree_transpose_map) tree_transpose_map_with_accessor)tree_transpose_map_with_path)tree_unflatten)dict_insertion_ordered)register_pytree_node)register_pytree_node_class)unregister_pytree_node)
PyTreeKind
PyTreeSpec)__version__)*reexportr/   r.   r   flattenflatten_with_pathflatten_with_accessor	unflatteniterleaves	structurepaths	accessorsis_leafmapmap_map_with_pathmap_with_path_map_with_accessormap_with_accessor_replace_nones	partition	transposetranspose_maptranspose_map_with_pathtranspose_map_with_accessorbroadcast_prefixbroadcast_commonbroadcast_mapbroadcast_map_with_pathbroadcast_map_with_accessorreducesummaxminr   anyflatten_one_levelregister_noderegister_node_classunregister_noder*   )CallableIterable)AnyTypeVar)	ParamSpec_P_Tc                       e Zd ZU dZded<   ddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fdZedd       ZddZdd	Z	dd
Z
 xZS )ReexportedModulez2A module that re-exports APIs from another module.str__doc__N)doc__all____dir__extra_membersc               B   |xs d|j                    d| d|d}t        | 	  ||       ||j                  D ch c]
  }|dk7  s	| }}t	        |      }|5|j                         D ch c]  }|j                  d      r|dk7  s| }}t	        |      j                  |      }|r6|j                         D ]  \  }	}
t        | |	|
        |j                  |       || _        || _        || _        t        |      | _        t        |      | _        y c c}w c c}w )NzRe-exports :mod:`z` as :mod:`z` with namespace :const:`z`.r1   _)__name__super__init__rb   setrc   
startswithintersectionitemssetattrupdate_ReexportedModule__namespace_ReexportedModule__original_ReexportedModule__all_setsorted_ReexportedModule__all_ReexportedModule__dir)selfname	namespaceoriginalra   rb   rc   rd   nkeyvalue	__class__s              =/home/dcms/DCMS/lib/python3.12/site-packages/optree/pytree.pyri   zReexportedModule.__init__   s.     
 1 12+dV D&&/]"6 	 	s#?"*"2"2FQa:oqFGFg,?"*"2"2"4bQALL<MRSWaRaqbGbg,++G4+113 *
Uc5)*NN=)$" G_
G_
 G cs   
DD,DD	Dc                    | j                   S z7Return the list of attributes available in this module.)rt   rv   s    r~   rb   zReexportedModule.__all__   s     zz    c                6    | j                   j                         S r   )ru   copyr   s    r~   rc   zReexportedModule.__dir__   s    zz  r   c                   || j                   v rKt        | j                  |      }t        j                  |      r| j                  |      }t        | ||       |S t        d| j                  d|      )z-Get an attribute from the re-exported module.module z has no attribute )	rr   getattrrq   _inspect
isfunction__reexport__rn   AttributeErrorrg   )rv   rw   attrs      r~   __getattr__zReexportedModule.__getattr__   sk    4>>!4??D1D""4(((.D$%Kwt}}&77I$RSSr   c                  t        j                        }d|j                  vrt        j                        dfd       }nt        j                        | j
                  d	 	 	 	 	 	 	 dfd       }j                  r/j                  j                  dd| j
                  d      |_        |j                  |j                  j                         D cg c]/  }|j                  dk7  r|n|j                  | j
                        1 c}	      |_
        t        t        d
d            r | j                  j                        |_        |S c c}w )z0Re-export a function with the default namespace.rx   c                      | i |S N )argskwargsfuncs     r~   wrappedz.ReexportedModule.__reexport__.<locals>.wrapped   s    T,V,,r   )rx   c                     |d| i|S )Nrx   r   )rx   r   r   r   s      r~   r   z.ReexportedModule.__reexport__.<locals>.wrapped   s     TAYA&AAr   z2(default: :const:`''`, i.e., the global namespace)z(default: :const:`z`))default)
parametersgetN)r   _P.argsr   	_P.kwargsreturnr\   )r   r   rx   r_   r   r   r   r\   )r   	signaturer   
_functoolswrapsrp   r`   replacevaluesrw   __signature__callabler   r   r   )rv   r   sigr   ps    `   r~   r   zReexportedModule.__reexport__   sV     &cnn,d#- $- d# "&!1!1BBB $B 	B $B ||"&,,"6"6H()9)9(<B?# %(KK !^^224 ;.AAIIdFVFVI4WW %0 %G! GD%./++DHH5GKs   4E)rw   r_   rx   r_   ry   _ModuleTypera   
str | Nonerb   Iterable[str] | Nonerc   r   rd   zdict[str, Any] | Noner   None)r   z	list[str])rw   r_   r   rX   )r   Callable[_P, _T]r   r   )rg   
__module____qualname__r`   __annotations__ri   propertyrb   rc   r   r   __classcell__)r}   s   @r~   r^   r^      s    <L (,(,/3!%!% 	!%
 !% !% &!% &!% -!% 
!%F  !T!r   r^   c                     e Zd ZU ded<   ded<   ded<   eZded<   eZded	<   eZd
ed<    ee      Z ee	      Z	 ee
      Z
 ee      Z ee      Z ee      Z ee      Z ee      Z ee      Z ee      Z ee      Z ee      Z ee      Z ee      Z ee      Z ee      Z ee      Z ee      Z ee      Z ee      Z ee      Z ee      Z ee      Z ee      Z ee       Z  ee!      Z! ee"      Z" ee#      Z# ee$      Z$ ee%      Z% ee&      Z& ee'      Z' ee(      Z( ee)      Z) ee*      Z* ee+      Z+ ee,      Z, ee-      Z-y)ReexportedPyTreeModuler_   r0   r   	functoolsdataclassesztype[PyTreeSpec]r/   ztype[PyTreeKind]r.   ztype[PyTreeEntry]r   N).rg   r   r   r   r/   r.   r   staticmethodr2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   r   rQ   rR   rS   rT   rU   r*   r   r   r~   r   r      s     '1
$1'1
$1)4&4w'():; ,-B C +	D!f% +	U# +	w'3D!$]3%n5():;)*<=$]3 +	 +	$]3"./F"G&23N&O#'(89'(89$]3"./F"G&23N&O#f%33333():;$]3*+>?&7!-.D!Er   r   )modulec                    t        d      )zFRe-export a pytree utility module with the given namespace as default.z1reexport() is not available in type checking mode)NotImplementedError)rx   r   s     r~   r1   r1     s    !"UVVr   c           	     >   ddl m} | |u rd} nt        | t              st	        d| d      |	 t        j                  d      xs d}| d	}|r!t        d
 |j                  d      D              st        d|      || d| dfD ]#  }|t
        j                  v st        d|d       t        | d| t               }t        | d| t"              }t        || t
        j                  t$           t&        ||d      }|t
        j                  |<   |t
        j                  | d<   |t
        j                  | d<   |S # t        $ rL 	 t        j                  d      j                  j                  dd      }n# t        t        f$ r d}Y nw xY wY Rw xY w)a  Re-export a pytree utility module with the given namespace as default.

        >>> import optree
        >>> pytree = optree.pytree.reexport(namespace='my-pkg', module='my_pkg.pytree')
        >>> pytree.flatten({'a': 1, 'b': 2})
        ([1, 2], PyTreeSpec({'a': *, 'b': *}))

        This function is useful for downstream libraries that want to re-export the pytree utilities
        with their own namespace::

            # foo/__init__.py
            import optree
            pytree = optree.pytree.reexport(namespace='foo')

            # foo/bar.py
            from foo import pytree

            @pytree.dataclasses.dataclass
            class Bar:
                a: int
                b: float

            print(pytree.flatten({'a': 1, 'b': 2, 'c': Bar(3, 4.0)}))
            # Output:
            #   ([1, 2, 3, 4.0], PyTreeSpec({'a': *, 'b': *, 'c': CustomTreeNode(Bar[()], [*, *])}, namespace='foo'))

        Args:
            namespace (str): The namespace to re-export from.
            module (str, optional): The name of the module to re-export.
                If not provided, defaults to ``<caller_module>.pytree``. The caller module is determined
                by inspecting the stack frame.

        Returns:
            The re-exported module.
        r   )__GLOBAL_NAMESPACE z$The namespace must be a string, got .   __main__rg   z.pytreec              3  <   K   | ]  }|j                           y wr   )isidentifier).0parts     r~   	<genexpr>zreexport.<locals>.<genexpr>[  s     !T$$"3"3"5!Ts   zinvalid module name: z.dataclassesz
.functoolsr   z already exists)rx   ry   )r0   r   r   )rx   ry   rd   )optree.registryr   
isinstancer_   	TypeError_sys_getframemodulenamer   	_getframe	f_globalsr   
ValueError_allsplitmodulesr^   r   r   rg   r0   )rx   r   GLOBAL_NAMESPACEcaller_modulemodule_namereexported_dataclassesreexported_functoolsmods           r~   r1   r1   $  s   J 	K((IIs+B9-qQRR>/ $ 8 8 ; Iz &g.FT!T&,,sBS!TT4VJ?@@"vhl$;xz=RS 	KKdll* 7;/!IJJ	K "2hl# "

  0hj! 

 '7\\(+*51		'
  #V0Fx|,-.Bxz*+
K " //$(NN1$5$?$?$C$CJPZ$[M&
3 /$.M/	/s5   E 	F/F FFFFFF)rx   r_   r   r   r   r   )rx   r_   r   r   r   r   )ur`   
__future__r   r   r   inspectr   sysr   builtinsr   r   typesr   r   typingr   _TYPE_CHECKINGoptree.dataclassesr   optree.functoolsoptree.accessorsr   
optree.opsr   r:   r	   r
   rQ   r   rI   r   rJ   r   rL   r   rK   r   rH   r   r2   r   rR   r   r4   r   r3   r   r;   r   r6   r   r7   r   r<   r   r=   r   r@   r   rA   r   r>   r   r?   r   rO   r   rP   r   rC   r    r9   r!   rM   r"   rB   r#   r8   r$   rN   r%   rD   r&   rE   r'   rG   r(   rF   r)   r5   r   r*   r+   rS   r,   rT   r-   rU   optree.typingr.   r/   optree.versionr0   rb   collections.abcrV   rW   rX   rY   typing_extensionsrZ   r[   r\   r^   r   r1   r   r   r~   <module>r      s  $ #      + 2 ( $ ( 2 & & @ : V N @ . B J B . ( , & ( B D : < & & 2 * , : 2 & 2 : V N 2 2 A M E 0 5+\ 2#+	4B	B\{ \~ -F!1 -F^ :> W :> Ur   