
    i|&                        S r SSKJr  SSKrSSKJr  SSKJrJrJrJ	r	  SSK
Jr  SSKJrJrJr  SS	KJr  SS
KJr  SS jrSS jrSS jr\" S\S9r   S           SS jjr      SS jr        SS jrg)z.Strategies for customizing subclass behaviors.    )annotationsN)collect)AnyCallableTypeVarUnion   )BaseConverter)AttributeOverridemake_dict_structure_fnmake_dict_unstructure_fn)already_generating)
subclassesc                    [         R                  " U 5      =(       d    U nU S 0n[        U5       H  n[        U5       H  nS X$'   M	     M     [	        U5      $ N)typing
get_originr   _make_subclasses_treelist)cl
cls_originseensclsscls        \/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/cattrs/strategies/_subclasses.pyr   r      sS    ""2&,"J :D*%)#.DDJ / & :    c                    [         R                  " U 5      =(       d    U n[        [        U5      5      n[        U5      n[	        X4-  5      $ )z?Whether the given class has subclasses from `given_subclasses`.)r   r   setr   bool)r   given_subclassesr   actualgivens        r   _has_subclassesr#      s>    ""2&,"JJ'(F !Er   c                    [        [        U 5      5      n[        [        U5      [        U5      -  5      n[        U5      S:  a	  [        U   $ S $ )Nr	   )tupler   r   lenr   )r   given_subclasses_treeactual_subclass_tree
class_trees       r   _get_union_typer*   #   sI     !6r!:;s/037L3MMNJ #J1 45>$>r   C)boundc                    [        5         Ub  U /UQ7nO[        [        U 5      5      nUc  [        XXT5        g[	        XX45        g)a  
Configure the converter so that the attrs/dataclass `cl` is un/structured as if it
was a union of itself and all its subclasses that are defined at the time when this
strategy is applied.

:param cl: A base `attrs` or `dataclass` class.
:param converter: The `Converter` on which this strategy is applied. Do note that
    the strategy does not work for a :class:`cattrs.BaseConverter`.
:param subclasses: A tuple of sublcasses whose ancestor is `cl`. If left as `None`,
    subclasses are detected using recursively the `__subclasses__` method of `cl`
    and its descendents.
:param union_strategy: A callable of two arguments passed by position
    (`subclass_union`, `converter`) that defines the union strategy to use to
    disambiguate the subclasses union. If `None` (the default), the automatic unique
    field disambiguation is used which means that every single subclass
    participating in the union must have an attribute name that does not exist in
    any other sibling class.
:param overrides: a mapping of `cl` attribute names to overrides (instantiated with
    :func:`cattrs.gen.override`) to customize un/structuring.

.. versionadded:: 23.1.0
.. versionchanged:: 24.1.0
   When overrides are not provided, hooks for individual classes are retrieved from
   the converter instead of generated with no overrides, using converter defaults.
.. versionchanged:: 25.2.0
   Slotted dataclasses work on Python 3.14 via :func:`cattrs.subclasses.subclasses`,
   which filters out duplicate classes caused by slotting.
N)r   r%   r   *_include_subclasses_without_union_strategy'_include_subclasses_with_union_strategy)r   	converterr   union_strategy	overridesparent_subclass_trees         r   include_subclassesr4   ,   sR    H I "0Z0$%:2%>?2/	
 	0^	
r   c                d   U H  n [        X5      nU 4S jnUb  [        X40 UD6n[        X40 UD6nO"UR                  U 5      nUR	                  U 5      nUc	  X4SS jjnOUR                  XCS9n	UU UU	4   SS jjnUU U4   SS jjn
UR                  XX5        UR                  XZ5        M     g )Nc                    XL $ r    cls_cls     r   	cls_is_cl=_include_subclasses_without_union_strategy.<locals>.cls_is_clq   
    :r   c                    U" X5      $ r   r7   )val_r:   
_base_hooks       r   struct_hook?_include_subclasses_without_union_strategy.<locals>.struct_hook}   s    !#++r   )r2   c                L    U" U 5      nXcL a  U" X5      $ UR                  X5      $ )z
If val is disambiguated to the class `cl`, use its base hook.

If val is disambiguated to a subclass, dispatch on its exact runtime
type.
)	structure)r?   r@   _cr:   rA   _dis_fndis_cls          r   rB   rC      s,     !=%c//||C00r   c                b    U R                   UL a  U" U 5      $ UR                  X R                   S9$ )z
If val is an instance of the class `cl`, use the hook.

If val is an instance of a subclass, dispatch on its exact runtime type.
)unstructure_as)	__class__unstructure)r?   rF   r:   rA   s       r   unstruct_hookA_include_subclasses_without_union_strategy.<locals>.unstruct_hook   s/     }}#!#&>>#mm>DDr   )r?   dictreturnr   )r?   zparent_subclass_tree[0]rP   rO   )r*   r   r   get_structure_hookget_unstructure_hook_get_dis_funcregister_structure_hook_funcregister_unstructure_hook_func)r   r0   r3   r2   subclass_unionr;   base_struct_hookbase_unstruct_hookrB   dis_fnrM   s              r   r.   r.   `   s     # )B! 	  5bQyQ!9"!U9!U(;;B?!*!?!?!C!.0 , ,,^,QF
 +11 1* )		E(	E
 	E" 	..yF00JC #r   c                l   U Vs/ s H  n[        XA5      (       d  M  UPM     nnU(       d  g0 n0 nU Hs  n[        U5      U1-
  [        l         Ub  [	        X@40 UD6n[        X@40 UD6n	O U R                  USS9nU R                  USS9n	[        5       [        l        XU'   XU'   Mu     [        U   n
UR                  5        H  u  pKU4S jnU R                  X5        M     UR                  5        H  u  pKU4S jnU R                  X5        M     U" X5        U R                  U
5      nU R                  U
5      n	U H  nU4S jnU R                  X5        [        U Vs/ s HR  n[        [        R                  " U5      =(       d    U[        R                  " U5      =(       d    U5      (       d  MP  UPMT     sn5      n[!        U5      S:  d  M  [        U   nU" X5        U R                  U5      n	X4S	S jjnU R                  UU5        M     gs  snf ! [        5       [        l        f = fs  snf )
aB  
This function is tricky because we're dealing with what is essentially a circular
reference.

We need to generate a structure hook for a class that is both:
* specific for that particular class and its own fields
* but should handle specific functions for all its descendants too

Hence the dance with registering below.
NF)cache_resultc                    XL $ r   r7   r8   s     r   r;   :_include_subclasses_with_union_strategy.<locals>.cls_is_cl   r=   r   c                    XL $ r   r7   r8   s     r   r;   r]      r=   r   c                    XL $ r   r7   r8   s     r   r;   r]      r=   r      c                    U" X5      $ r   r7   )payloadr@   _u_ss       r   sh3_include_subclasses_with_union_strategy.<locals>.sh   s    '&r   )rb   rO   rP   r   )r#   r   r   working_setr   r   rR   rQ   r   itemsrU   rT   r%   
issubclassr   r   r&   )r0   union_classesr1   r2   r   parent_classesoriginal_unstruct_hooksoriginal_struct_hooksrM   rB   final_unionhookr;   cr   ure   s                    r   r/   r/      s.   " $1W=ROB4Vb=NW 
 *-]);rd)B&	3$ 8 T) T4RPiP ) > >rPU > V'::2E:R-0U*&3#$/b!! ( &K+113! 	 	00A 4 *//1! 	 	..y? 2 ;*22;?M..{;K  " 	 	00J '&Af//27a9J9J29N9TRTU &

 z?Qj!A1(#66q9K() ' 229bA- ] X( .1U*Ds#   HH<H"AH1
5H1
H.)r   typerP   z
list[type])r   rr   r    tuple[type, ...]rP   r   )r   rr   r'   ztuple[type]rP   ztype | None)NNN)r   rr   r0   r+   r   ztuple[type, ...] | Noner1   zCallable[[Any, C], Any] | Noner2   #dict[str, AttributeOverride] | NonerP   None)r0   r
   r3   rs   r2   rt   )r0   r+   rj   rs   r1   zCallable[[Any, C], Any]r2   rt   )__doc__
__future__r   r   gcr   r   r   r   r   
convertersr
   genr   r   r   gen._constsr   r   r   r#   r*   r+   r4   r.   r/   r7   r   r   <module>r|      s    4 "   0 0 & U U , #	 ? C}% +/59591
1
1
 (1
 3	1

 31
 
1
hHKHK +HK 3	HKVUBUB#UB ,UB 3	UBr   