o
    qoi1                     @   s   d Z ddlmZmZmZmZ ddlmZ ddlm	Z	 ddlm
Z
 dee
j deded	d
fddZdeeef ded	d
fddZddd
ddedededeee  d	d
f
ddZd
S )zProgrammatic checks for the style of a baseline configuration.

"Baseline configurations" are configurations for baseline or flagship models.
They will be read by a large number of people, so keeping them simple and
readable is important.
    )AnyDictListOptional)config)daglish)historyattr_historymax_writes_per_attributepath_strreturnNc              	   C   sx   dd | D }t  }| D ]}|j}||j qddd |D }t||kr:tt| d| d| d| d	d
S )a  Style check for the max number of writes to an attribute.

  Checks the max number of writes to an attribute doesn't exceed a certain
  threshold.

  Args:
    attr_history: The list of history entries for a given config attribute.
    max_writes_per_attribute: Threshold for maximum number of writes to a given
      config attribute.
    path_str: A string that would be returned by `daglish.path_str(p)` for some
      path p.
  c                 S   s   g | ]}|j tjjkr|qS  )kindr   
ChangeKind	NEW_VALUE).0entryr   r   Y/home/ubuntu/.local/lib/python3.10/site-packages/fiddle/_src/validation/baseline_style.py
<listcomp>/   s
    z3_check_max_writes_per_attribute.<locals>.<listcomp>, c                 s   s    | ]}| V  qd S )Nr   )r   filenamer   r   r   	<genexpr>:   s    z2_check_max_writes_per_attribute.<locals>.<genexpr>z value(s) written to <config>z in files: zC. Please limit the number of overrides of this config attribute to z0 to maintain baseline configuration readability.N)setlocationaddr   joinlen
ValueError)r	   r
   r   value_history	filenameshistory_entryr   	debug_strr   r   r   _check_max_writes_per_attribute   s"   r"   sample_paths_by_filemax_config_source_filesc                 C   s>   t | |krddd |  D }td| d| ddS )a  Style check for number of files where an attribute is written.

  Checks the max number of files where an attribute is written, doesn't exceed a
  certain threshold.

  Args:
    sample_paths_by_file: A dictionary of filenames mapped to string that would
      be returned by `daglish.path_str(p)` for some path p for a config's
      attribute.
    max_config_source_files: Threshold for maximum number of files producing
      `config`.
  r   c                 s   s$    | ]\}}| d | dV  qdS )z (example: wrote to <config>)Nr   )r   r   pathr   r   r   r   V   s
    
z1_check_max_config_source_files.<locals>.<genexpr>z
More than z file(s) produced this config. For baseline configurations, please ensure that you don't have a big hierarchy of files each overriding values in a config, because this is harder to read for new users. The files which have written to this config are: .N)r   r   itemsr   )r#   r$   r!   r   r   r   _check_max_config_source_filesE   s   
r)         )r$   r
   ignorer   r,   c                   sV   i |pg }dd |D  dt dtjdt f fdd}tj||  t| dS )	a  Checks that a configuration is style-conformant.

  Args:
    config: fdl.Buildable object, or collection which may include Buildable
      objects.
    max_config_source_files: Threshold for maximum number of files producing
      `config`.
    max_writes_per_attribute: Threshold for maximum number of writes to
      `config`'s attributes.
    ignore: An optional list of config sub-objects that are ignored for these
      readability validations/style checks.
  c                 S   s   h | ]}t |qS r   )id)r   objr   r   r   	<setcomp>{   s    z'check_baseline_style.<locals>.<setcomp>valuestater   c           	         s   t |  v rd S t| tjrAt|  D ]+\}}t | v r q| j| }t|j	}t
|| |D ]}|j}|j| q3q|j| ddD ]}qHd S )NT)ignore_leaves)r-   
isinstance
config_lib	Buildableordered_argumentsr(   __argument_history__r   r   current_pathr"   r   
setdefaultr   yield_map_child_values)	r0   r1   	attr_name
attr_valuer	   r   r    r   _
ignore_idsr
   r#   r   r   traverse}   s$   
z&check_baseline_style.<locals>.traverseN)r   r   StateMemoizedTraversalrunr)   )r   r$   r
   r,   r@   r   r>   r   check_baseline_stylec   s    rD   )__doc__typingr   r   r   r   fiddle._srcr   r4   r   r   HistoryEntryintstrr"   r)   rD   r   r   r   r   <module>   sF   
'

!
