o
    ]i                  	   @   s   d Z ddl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ejdejfdd	Zdejdeje fd
dZdejdefddZdejdejejeeje eje f  fddZG dd dejZdejeef de	ddfddZdS )zbAttribute docstrings parsing.

.. seealso:: https://peps.python.org/pep-0257/#what-is-a-docstring
    N)
ModuleType   )	DocstringDocstringParamnodereturnc                 C   s
   t | dS )z<Return the constant's value if the given node is a constant.value)getattrr    r   ]/var/www/html/stock_analysis/be/venv/lib/python3.10/site-packages/docstring_parser/attrdoc.pyast_get_constant_value   s   
r   c                 C   sB   t tdr
t| S t| tjrtt| S t| tjr| jS dS )z0Convert the AST node to source code as a string.unparseN)	hasattrastr   
isinstanceConstantstrr   Nameidr
   r   r   r   ast_unparse   s   

r   c                 C   s*   t | tjot | jtjot t| jtS )z2Return True if the given node is a literal string.)r   r   Exprr   r   r   r   r
   r   r   r   ast_is_literal_str    s
   r   c                 C   sx   t | tjtjfr:t | tjr| jd n| j}t |tjr:d}t | tjr*t| j}d}| j	r4t| j	}|j
||fS dS )z@Return name, type and default if the given node is an attribute.r   N)r   r   Assign	AnnAssigntargetstargetr   r   
annotationr   r   )r   r   type_strdefaultr   r   r   ast_get_attribute)   s   

r    c                
   @   sV   e Zd ZdZdZdZdd Zdejdej	e
eje
eje
 eje
 f f fddZdS )	AttributeDocstringsz6An ast.NodeVisitor that collects attribute docstrings.Nc                 C   s^   | j rt|r| j \}}}t|j||f| j|< t|| _ t|tjtj	fr-| 
| d S d S N)	prev_attrr   r   r   	attr_docsr    r   r   ClassDefModulegeneric_visit)selfr   	attr_name	attr_typeattr_defaultr   r   r   visitB   s   

zAttributeDocstrings.visit	componentr   c                 C   s   i | _ d| _z
tt|}W n ty   Y | j S w t|}t	|r.| 
| | j S t|tjrEt|jd tjrE| 
|jd  | j S )zGet attribute docstrings from the given component.

        :param component: component to process (class or module)
        :returns: for each attribute docstring, a tuple with (description,
            type, default)
        Nr   )r$   r#   textwrapdedentinspect	getsourceOSErrorr   parseismoduler,   r   r&   bodyr%   )r(   r-   sourcetreer   r   r   get_attr_docsN   s"   		


z!AttributeDocstrings.get_attr_docs)__name__
__module____qualname____doc__r$   r#   r,   TAnyDictr   TupleOptionalr8   r   r   r   r   r!   <   s    $r!   obj	docstringc              	   C   sh   t dd |jD }t |  D ]\}\}}}||vr1td|g||||du|d}|j| qdS )zAdd attribute docstrings found in the object's source code.

    :param obj: object from which to parse attribute docstrings
    :param docstring: Docstring object where found attributes are added
    :returns: list with names of added attributes
    c                 s   s    | ]}|j V  qd S r"   )arg_name).0pr   r   r   	<genexpr>q   s    z+add_attribute_docstrings.<locals>.<genexpr>	attributeN)argsdescriptionrD   	type_nameis_optionalr   )setparamsr!   r8   itemsr   metaappend)rB   rC   rN   rD   rJ   rK   r   paramr   r   r   add_attribute_docstringsh   s   	rS   )r<   r   r0   r.   typingr=   typesr   commonr   r   ASTr>   r   rA   r   r   boolr   r@   r    NodeVisitorr!   UniontyperS   r   r   r   r   <module>   s.    	 
,