§
    .P„j:   ã                  óø   — 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m	Z	 d dl
mZ d dlmZ d dlmZmZmZmZmZmZmZmZ  ej        e¦  «        Ze	d d„¦   «         Ze	d!d„¦   «         Zd"d#d„Z	 d"d$d„Z	 d%d&d„ZdS )'é    )ÚannotationsN)ÚMapping)ÚAnyÚoverload)Úget_field_type_from_annotations)ÚKey)Ú
DataclassTÚPossiblyNestedDictÚVÚcontains_dataclass_type_argÚis_dataclass_instanceÚis_dataclass_typeÚis_optionalÚunflatten_splitÚobjr	   Úchanges_dictúdict[str, Any]Úreturnc                ó   — d S ©N© )r   r   s     ú[/root/disk_3tb/conda_envs/Huy-visual/lib/python3.11/site-packages/simple_parsing/replace.pyÚreplacer      ó   € à€Có    c                ó   — d S r   r   )r   Úchangess     r   r   r       r   r   údict[str, Any] | Nonec                ó@  — |r|rt          d¦  «        ‚|p|}t          |¦  «        }i }t          j        | ¦  «        D ]»}|j        |vrŒ|j        st          d|j        › d�¦  «        ‚t          | |j        ¦  «        }t          |¦  «        rHt          ||j                 t          ¦  «        r(| 
                    |j        ¦  «        }t          |fi |¤Ž}n| 
                    |j        ¦  «        }|||j        <   Œ¼|                     |¦  «         t          j        | fi |¤ŽS )a  Replace some values in a dataclass.

    Compared to `dataclasses.replace`, this has two major differences:
    1. Allows recursively replacing values in nested dataclass fields. When a dictionary is passed
    as the value, and the value of that field on `obj` is a dataclass, then `replace` is called
    recursively.

    2. Allows passing a dictionary of flattened changes, e.g. `{"a.b": 1}` instead of
    `{"a": {"b": 1}}`.

    ## Examples

    >>> import dataclasses
    >>> import simple_parsing
    >>> from typing import Union
    >>> @dataclasses.dataclass
    ... class A:
    ...    a: int = 0
    >>> @dataclasses.dataclass
    ... class B:
    ...     b: str = "b"
    >>> @dataclasses.dataclass
    ... class Config:
    ...     a_or_b: Union[A, B] = dataclasses.field(default_factory=A)

    >>> base_config = Config(a_or_b=A(a=1))


    NOTE: key difference with respect to `dataclasses.replace`:
    >>> dataclasses.replace(base_config, a_or_b={"a": 123})
    Config(a_or_b={'a': 123})
    >>> simple_parsing.replace(base_config, a_or_b={"a": 123})
    Config(a_or_b=A(a=123))

    Replace accepts either a dictionary of changes or keyword arguments:

    >>> simple_parsing.replace(base_config, {"a_or_b": B(b="bob")})
    Config(a_or_b=B(b='bob'))
    >>> simple_parsing.replace(base_config, a_or_b=B(b='bob'))
    Config(a_or_b=B(b='bob'))

    Changes can also be passed in a 'flat' format, which makes it easy to replace nested fields:
    >>> simple_parsing.replace(base_config, {"a_or_b.a": 2})
    Config(a_or_b=A(a=2))
    z-Cannot pass both `changes_dict` and `changes`ú'Cannot replace value of non-init field ú.)Ú
ValueErrorr   ÚdataclassesÚfieldsÚnameÚinitÚgetattrr   Ú
isinstanceÚdictÚpopr   Úupdate)r   r   r   Úreplace_kwargsÚfieldÚfield_valueÚfield_changesÚ	new_values           r   r   r   %   sI  € ð\ ð J˜ð JÝÐHÑIÔIÐIØÐ%˜g€Gõ ˜gÑ&Ô&€Gà€NÝÔ# CÑ(Ô(ð /ð /ˆØŒ:˜WÐ$Ð$ØØŒzð 	VÝÐTÀuÄzÐTÐTÐTÑUÔUÐUå˜c 5¤:Ñ.Ô.ˆå  Ñ-Ô-ð 	0µ*¸WÀUÄZÔ=PÕRVÑ2WÔ2Wð 	0Ø#ŸKšK¨¬
Ñ3Ô3ˆMÝ Ð=Ð=¨}Ð=Ð=ˆIˆIàŸš E¤JÑ/Ô/ˆIØ%.ˆ�u”zÑ"Ð"ð ×Ò˜'Ñ"Ô"Ð"åÔ˜sÐ5Ð5 nÐ5Ð5Ð5r   Ú
selectionsú"dict[str, Key | DataclassT] | Nonec                ód  — d}|s| S t          ||d¬¦  «        }i }t          j        | ¦  «        D �]ï}|j        st	          d|j        › d�¦  «        ‚|j        |vrŒ,t          | |j        ¦  «        }t          | j        |j        ¦  «        }d}t          |¦  «        st	          d|› �¦  «        ‚| 
                    |j        ¦  «        }t          |t          ¦  «        r| 
                    |d¦  «        }	|}
n|}	d}
t          |	¦  «        r |	¦   «         }nìt          |	¦  «        rt          j        |	¦  «        }nÈ|j                             dd¦  «        rXt          |	t&          ¦  «        sJ ‚|j        d         |	         }t          |¦  «        r|}nq |j        d         |	         ¦   «         }nUt)          |¦  «        r|	€d}nAt          |¦  «        r|	€|                     ¦   «         }nt	          d	|	› d
|j        › d�¦  «        ‚|
rt-          ||
¦  «        }n|}|||j        <   �Œñt          j        | fi |¤ŽS )zøThis function replaces the dataclass of subgroups, union, and optional union. The
    `selections` dict can be in flat format or in nested format.

    The values of selections can be `Key` of subgroups, dataclass type, and dataclass instance.
    Ú__key__F©Ú	recursiver    r!   Nz?The replaced subgroups contains no dataclass in its annotation Ú	subgroupszinvalid selection key 'z' for field 'ú')Ú_unflatten_selection_dictr#   r$   r&   r"   r%   r'   r   Ú	__class__r   r*   r(   r)   r   r   ÚcopyÚdeepcopyÚmetadataÚgetÚstrr   Údefault_factoryÚreplace_subgroupsr   )r   r1   Úkeywordr,   r-   r.   Úfield_annotationr0   Ú	selectionÚvalue_of_selectionÚchild_selectionsÚsubgroup_selections               r   rA   rA   q   s¥  € ð €Gàð Øˆ
Ý*¨:°wÈ%ÐPÑPÔP€Jà€NÝÔ# CÑ(Ô(ð 4/ñ 4/ˆØŒzð 	VÝÐTÀuÄzÐTÐTÐTÑUÔUÐUàŒ:˜ZÐ'Ð'Øå˜c 5¤:Ñ.Ô.ˆÝ:¸3¼=È%Ì*ÑUÔUÐàˆ	å*Ð+;Ñ<Ô<ð 	ÝØdÐRbÐdÐdñô ð ð —N’N 5¤:Ñ.Ô.ˆ	Ý�i¥Ñ&Ô&ð 	$Ø!*§¢¨w¸Ñ!=Ô!=ÐØ(ÐÐà!*ÐØ#ÐåÐ/Ñ0Ô0ð 	Ø,Ð,Ñ.Ô.ˆKˆKÝ"Ð#5Ñ6Ô6ð 	Ýœ-Ð(:Ñ;Ô;ˆKˆKØŒ^×Ò ¨TÑ2Ô2ð 	ÝÐ0µ#Ñ6Ô6Ð6Ð6Ð6Ø!&¤°Ô!<Ð=OÔ!PÐÝ$Ð%7Ñ8Ô8ð Pà0��ð N˜eœn¨[Ô9Ð:LÔMÑOÔO��ÝÐ)Ñ*Ô*ð 	Ð/AÐ/IØˆKˆKÝ(Ð)9Ñ:Ô:ð 	Ð?QÐ?YØ×/Ò/Ñ1Ô1ˆKˆKåØXÐ*<ÐXÐXÈ5Ì:ÐXÐXÐXñô ð ð ð 	$Ý)¨+Ð7GÑHÔHˆIˆIà#ˆIà%.ˆ�u”zÑ"Ñ"ÝÔ˜sÐ5Ð5 nÐ5Ð5Ð5r   r4   r!   TÚ	flattenedúMapping[str, V]rB   r?   Úsepr6   ÚboolúPossiblyNestedDict[str, V]c                ór  — i }t          ¦   «         }|                      ¦   «         D ]H\  }}|                     |¦  «        }t          |¦  «        dk    r|                     |d         ¦  «         ŒI|                      ¦   «         D ]‚\  }}|                     |¦  «        }	|	d         }
|	dd…         }|
|v rM|                     |
i ¦  «        }t          |¦  «        dk    r|||<   n||d                     |¦  «        <   |||
<   Œ}|||<   Œƒ|r0|D ]-}|                     |¦  «        }t          ||¬¦  «        }|||<   Œ.|S )aé  This function convert a flattened dict into a nested dict and it inserts the `keyword` as
    the selection into the nested dict.

    >>> _unflatten_selection_dict({'ab_or_cd': 'cd', 'ab_or_cd.c_or_d': 'd'})
    {'ab_or_cd': {'__key__': 'cd', 'c_or_d': 'd'}}

    >>> _unflatten_selection_dict({'lv1': 'a', 'lv1.lv2': 'b', 'lv1.lv2.lv3': 'c'})
    {'lv1': {'__key__': 'a', 'lv2': {'__key__': 'b', 'lv3': 'c'}}}

    >>> _unflatten_selection_dict({'lv1': 'a', 'lv1.lv2': 'b', 'lv1.lv2.lv3': 'c'}, recursive=False)
    {'lv1': {'__key__': 'a', 'lv2': 'b', 'lv2.lv3': 'c'}}

    >>> _unflatten_selection_dict({'ab_or_cd.c_or_d': 'd'})
    {'ab_or_cd': {'c_or_d': 'd'}}

    >>> _unflatten_selection_dict({"a": 1, "b": 2})
    {'a': 1, 'b': 2}
    é   r   é   Nr!   r5   )	ÚsetÚitemsÚsplitÚlenÚaddr>   Újoinr*   r9   )rH   rB   rJ   r6   ÚdcÚunflatten_those_top_level_keysÚkÚvÚsplited_keysÚkeysÚtop_level_keyÚ	rest_keysÚsub_dcÚunflatten_vs                 r   r9   r9   ¸   s`  € ð* 
€Bå%(¡U¤UÐ"Ø—’Ñ!Ô!ð @ð @‰ˆˆ1Ø—w’w˜s‘|”|ˆÝˆ|ÑÔ Ò!Ð!Ø*×.Ò.¨|¸A¬Ñ?Ô?Ð?øà—’Ñ!Ô!ð ð ‰ˆˆ1Ø�wŠw�s‰|Œ|ˆØ˜QœˆØ˜˜˜”Hˆ	ØÐ:Ð:Ð:Ø—V’V˜M¨2Ñ.Ô.ˆFÝ�9‰~Œ~ Ò"Ð"Ø"#��w‘�à./��s—x’x 	Ñ*Ô*Ñ+Ø &ˆBˆ}ÑÐàˆBˆq‰EˆEàð  Ø/ð 	 ð 	 ˆAØ—’�q‘	”	ˆAÝ3°AÀÐKÑKÔKˆKØˆBˆq‰EˆEØ€Ir   )r   r	   r   r   r   r	   )r   r	   r   r	   r   )r   r	   r   r   r   r	   )r   r	   r1   r2   r   r	   )r4   r!   T)
rH   rI   rB   r?   rJ   r?   r6   rK   r   rL   )Ú
__future__r   r;   r#   ÚloggingÚcollections.abcr   Útypingr   r   Ú5simple_parsing.annotation_utils.get_field_annotationsr   Ú simple_parsing.helpers.subgroupsr   Úsimple_parsing.utilsr	   r
   r   r   r   r   r   r   Ú	getLoggerÚ__name__Úloggerr   rA   r9   r   r   r   ú<module>rj      s¸  ðØ "Ð "Ð "Ð "Ð "Ð "à €€€Ø Ð Ð Ð Ø €€€Ø #Ð #Ð #Ð #Ð #Ð #Ø  Ð  Ð  Ð  Ð  Ð  Ð  Ð  ðð ð ð ð ð ð 1Ð 0Ð 0Ð 0Ð 0Ð 0ð	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 
ˆÔ	˜8Ñ	$Ô	$€ð 
ðð ð ñ 
„ðð 
ðð ð ñ 
„ððI6ð I6ð I6ð I6ð I6ðZ GKðD6ð D6ð D6ð D6ð D6ðP ]að0ð 0ð 0ð 0ð 0ð 0ð 0r   