o
    qoi+                     @   s   d Z ddlZddlmZmZmZ ddlZddlmZ ddlm	Z	 dee	j
 defdd	Zd
ejdeee	j
  fddZdedee fddZdeddfddZdS )a+  Checks that custom objects/instances are not present in a configuration.

In general, we recommend building configurations from

(a) primitives like int/float/str/etc.
(b) basic python collections like lists/tuples/dicts
(c) Fiddle configs like fdl.Config, fdl.Partial

Usually, when a custom user object is present in a config, it can be rewritten
to be a fdl.Config that constructs that object. For example, instead of

config.foo.preprocessor = MyPreprocessor(dtype="int32")

write,

config.foo.preprocessor = fdl.Config(MyPreprocessor, dtype="int32")
    N)AnyListOptional)daglish)historyentriesreturnc                 C   s6   | sdS | d j tjurdnd}|| d jjdd S )z-Returns a concise string for a history entry.z(no history)zSet in zDeleted in    )max_filename_parts)	new_valuer   DELETEDlocationformat)r   set_or_deleted r   \/home/ubuntu/.local/lib/python3.10/site-packages/fiddle/_src/validation/no_custom_objects.py_concise_history*   s   r   statec                 C   sl   | j r4| jdur4| j d }| j} t| jtjr,t|tjsJ | jj|j	 }|r*|S dS | j r4| jdusdS )z1Returns the history from a Buildable for a state.Nr	   )
current_pathparent
isinstanceoriginal_valuefdl	Buildabler   Attr__argument_history__name)r   attrr   r   r   r   _get_history_from_state6   s   
r   configc                    s4   g  dd dt jf fdd}t j||   S )zReturns a list of errors found in the given config.

  Args:
    config: Fiddle config object, or nested structure of configs.
  c                 S   s   dt t|  S )Nz, )r   r   )r   r   r   r   history_strL   s   z&get_config_errors.<locals>.history_strr   c                    s   t |j}t| trtt| dr d| |  n-t	| r1 d| |  n|
| sLt | sL dt|  d| |  || S )N_fieldszFound namedtuple at zFound dataclass at zFound z at )r   path_strr   r   tuplehasattrtypeappenddataclassesis_dataclassis_traversableis_unshareablemap_children)valuer   r#   errorsr!   r   r   traverseO   s   
"
z#get_config_errors.<locals>.traverse)r   StateMemoizedTraversalrun)r    r0   r   r.   r   get_config_errorsD   s
   r4   c                 C   s"   t | }|rtdd| dS )zChecks that no custom objects are present in the given config.

  Args:
    config: Fiddle config object, or nested structure of configs.

  Raises:
    ValueError: If the configuration contains custom objects.
  z|Custom objects were found in the config. In general, you should be able to convert these to fdl.Config's. Custom objects:
  z
  N)r4   
ValueErrorjoin)r    r/   r   r   r   check_no_custom_objects_   s   	r7   )__doc__r(   typingr   r   r   fiddler   r   r   HistoryEntrystrr   r1   r   r4   r7   r   r   r   r   <module>   s   
