a
    ij                     @  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m	Z	m
Z
 ddlmZ ddlZddlmZ e	e Ze
dZeZeeef ZeZejG dd	 d	Zdd
ddddZdd
dddZdddddddZdddddddZdd ZdS )zHelper utils.    )annotationsN)AnyCallableDictTypeTypeVar)epy)	Annotated_ClsTc                   @  s2   e Zd ZU ded< ded< ejddddZd	S )
DescriptorInfoHint
annotationz4Callable[[dataclasses.Field[Any], Hint], Descriptor]descriptor_fnobject)returnc                 C  s"   t | jtu sJ | jj\}|S )zReturns the Annotated sentinel.)typing_extensions
get_originr   r	   __metadata__)selfannotated_token r   O/home/ghrups/robot-bench/.venv/lib/python3.9/site-packages/etils/edc/helpers.pyr   )   s    
zDescriptorInfo.annotated_tokenN)__name__
__module____qualname____annotations__	functoolscached_propertyr   r   r   r   r   r   $   s   
r   zlist[DescriptorInfo])clsdescriptor_infosr   c                   s6    s| S d| _ | jt fdd}|| _| S )zH`__new__` decorator to replace the fields by descriptors on first usage.Fc                   sn   t ju r| }n| g|R i |}| jdr:|S |  D ] }| jddrVqBt| d qBd| _|S )N_edc_processedT)r   )r   __new____dict__getmro_replace_field_by_descriptorr    )r   argskwargsr   Zcurr_clsr   Z
old_new_fnr   r   
new_new_fn9   s    

zwrap_new.<locals>.new_new_fn)r    r!   r   wraps)r   r   r)   r   r(   r   wrap_new1   s    r+   _Cls)r   r   c          	        s   t | sdS dd t | D }t| dd}| D ]z\}}|| jvrJq6t|turZq6|j	}|| }|D ]B t
 fdd|jD sql ||}t| || || | qlq6dS )zHIterate over the dataclass fields and replace the fields by descriptors.Nc                 S  s   i | ]}|j |qS r   )name).0fr   r   r   
<dictcomp>]       z0_replace_field_by_descriptor.<locals>.<dictcomp>Tinclude_extrasc                 3  s   | ]}| j u V  qd S )N)r   )r.   aZdescriptor_infor   r   	<genexpr>l   s   z/_replace_field_by_descriptor.<locals>.<genexpr>)dataclassesis_dataclassfields_get_type_hintsitemsr   r   r   r	   
__origin__anyr   r   setattr__set_name__)	r   r   r9   hintsr-   hintZhint_clsfieldZ
descriptorr   r5   r   r%   U   s&    

r%   Fr2   bool_Hints)r3   r   c             
   C  s   zt | |dW S  ty| } zVd| j d| j d}dd | j D }d|}tj||| d d W Y d	}~n
d	}~0 0 d	S )
z-`get_type_hints` with better error reporting.r2   z%Could not infer typing annotation of z defined in z:
c                 S  s    g | ]\}}d | d|qS )z * z: r   )r.   kvr   r   r   
<listcomp>   r1   z#_get_type_hints.<locals>.<listcomp>
)prefixN)	_get_type_hints_fix	Exceptionr   r   r   r;   joinr   reraise)r   r3   emsglinesr   r   r   r:   v   s    

r:   c             	   C  s   dd |   D }zHtj| |dW t|   |D ]\}}|r0||_q0S ]\}}|rF||_qFn$t|   |D ]\}}|rj||_qj0 dS )z `get_type_hints` with bug fixes.c                 S  s   g | ]}t |qS r   )_fix_annotations)r.   subclsr   r   r   rG      r1   z'_get_type_hints_fix.<locals>.<listcomp>r2   N)r$   r   get_type_hintszipr   )r   r3   old_annotationsrR   r   r   r   r   rJ      s    
rJ   c                 C  s6   | t u sdt| di vrdS t| j}| jd |S )z/Remove the `_: dataclasses.KW_ONLY` annotation._r   N)r   getattrdictr   pop)r   rU   r   r   r   rQ      s
    
rQ   )__doc__
__future__r   r7   r   typingr   r   r   r   r   Zetilsr   r   r	   r,   r
   r   strrD   Z
Descriptor	dataclassr   r+   r%   r:   rJ   rQ   r   r   r   r   <module>   s&   $!