o
    v&iZC                     @  s   d Z ddlmZ ddlZddlZddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZmZ er>dd	lmZ dd
lmZ d/ddZejG dd dZee ZG dd dZejG dd dZd0ddZd1d$d%Z	d2d3d(d)Zd4d-d.ZdS )5z Results of coverage measurement.    )annotationsN)Iterable)TYPE_CHECKING)ConfigError	nice_pair)TArcTLineNo)CoverageData)FileReporterdatar
   	precisionintfile_reporterr   filenamestrreturnAnalysisc                 C  s  |   }| }| }|| |pg }|ru| }| |p"g }	||	}	tt}
|D ]\}}|
| 	| q/dd |

 D }t }|	D ]\}}||krY|	||f qI||v rf|	||| f qI||}| }| }nt }t }i }t }t||||||||||d
S )z'Create an Analysis from a FileReporter.c                 S  s*   i | ]\}}t |d kr|t|d qS )   r   )lenlist).0fromnotonos r   V/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/coverage/results.py
<dictcomp>+   s    $z/analysis_from_file_reporter.<locals>.<dictcomp>
r   r   has_arcs
statementsexcludedexecutedarc_possibilities_setarcs_executed_setexit_counts	no_branch)r   linesexcluded_linestranslate_linesarcstranslate_arcscollectionsdefaultdictsetadditemsr$   no_branch_linesr   )r   r   r   r   r   r   r    r!   r"   r)   destsr   tonosingle_destsnew_arcsr#   r$   r%   r   r   r   analysis_from_file_reporter   sP   



r5   c                   @  s   e Zd ZU dZded< ded< ded< ded	< ded
< ded< ded< ded< ded< ded< d+ddZd,d-ddZd.ddZd/ddZd0d d!Z	d1d#d$Z
d1d%d&Zd2d(d)Zd*S )3r   z(The results of analyzing a FileReporter.r   r   r   r   boolr   set[TLineNo]r   r    r!   	set[TArc]r"   r#   zdict[TLineNo, int]r$   r%   r   Nonec              
     s   t  j _t  j _ j j  _ jr7 	 } 
 }t fdd| D }tdd | D }nd } }}t jdt jt jt j|||d _d S )Nc                 3  s&    | ]\}}| j vrt|V  qd S N)missingr   r   kvselfr   r   	<genexpr>d   s   $ z)Analysis.__post_init__.<locals>.<genexpr>c                 s  s    | ]	\}}t |V  qd S r:   )r   r<   r   r   r   rA   e       r   r   )r   n_filesn_statements
n_excluded	n_missing
n_branchesn_partial_branchesn_missing_branches)sortedr"   arc_possibilitiesr#   arcs_executedr   r!   r;   r   _total_branchesmissing_branch_arcssumr/   Numbersr   r   r    numbers)r@   rG   mbarH   rI   r   r?   r   __post_init__\   s&   zAnalysis.__post_init__Fbranchesc                 C  s.   |r| j r|   }nd}t| j| j|dS )zThe missing line numbers, formatted nicely.

        Returns a string like "1-2, 5-11, 13-14".

        If `branches` is true, includes the missing branch arcs also.

        N)r)   )r   rN   r/   format_linesr   r;   )r@   rT   r)   r   r   r   missing_formattedt   s   
zAnalysis.missing_formatted
list[TArc]c                   s    fdd j D }t|S )z:Returns a sorted list of the un-executed arcs in the code.c                 3  s:    | ]}| j vr|d   jvr|d  jvr|V  qdS )r   r   N)r#   r%   r    )r   pr?   r   r   rA      s    
z(Analysis.arcs_missing.<locals>.<genexpr>)rK   rJ   )r@   r;   r   r?   r   arcs_missing   s   
zAnalysis.arcs_missinglist[TLineNo]c                 C  s   dd | j  D S )z<Returns a list of line numbers that have more than one exit.c                 S  s   g | ]
\}}|d kr|qS )r   r   )r   l1countr   r   r   
<listcomp>       z*Analysis._branch_lines.<locals>.<listcomp>)r$   r/   r?   r   r   r   _branch_lines   s   zAnalysis._branch_linesc                 C  s   t dd | j D S )z"How many total branches are there?c                 s  s    | ]	}|d kr|V  qdS )r   Nr   )r   r\   r   r   r   rA      rB   z+Analysis._total_branches.<locals>.<genexpr>)rO   r$   valuesr?   r   r   r   rM      s   zAnalysis._total_branchesdict[TLineNo, list[TLineNo]]c                 C  sj   |   }t|  }tt}|D ]!\}}||ks'J d| j d| d| ||v r2|| | q|S )zgReturn arcs that weren't executed from branch lines.

        Returns {l1:[l2a,l2b,...], ...}

        zIn z, didn't expect z == )rY   r-   r_   r+   r,   r   r   append)r@   r;   branch_linesrR   r[   l2r   r   r   rN      s   
$zAnalysis.missing_branch_arcsc                 C  sl   t |  }tt}| jD ]%\}}||ks J d|d|||f| jvr(q||v r3|| | q|S )zReturn arcs that were executed from branch lines.

        Only include ones that we considered possible.

        Returns {l1:[l2a,l2b,...], ...}

        z+Oops: Didn't think this could happen: l1 = z, l2 = )r-   r_   r+   r,   r   rL   r"   rb   )r@   rc   ebar[   rd   r   r   r   executed_branch_arcs   s   
zAnalysis.executed_branch_arcsdict[TLineNo, tuple[int, int]]c                 C  sD   |   }i }|  D ]}| j| }t|| }||| f||< q
|S )zGet stats about branches.

        Returns a dict mapping line numbers to a tuple:
        (total_exits, taken_exits).

        )rN   r_   r$   r   )r@   missing_arcsstatslnumexitsr;   r   r   r   branch_stats   s   
zAnalysis.branch_statsN)r   r9   )F)rT   r6   r   r   )r   rW   )r   rZ   r   r   )r   ra   )r   rg   )__name__
__module____qualname____doc____annotations__rS   rV   rY   r_   rM   rN   rf   rl   r   r   r   r   r   M   s(   
 





c                   @  s.   e Zd ZdZdddZdd
dZdddZdS )AnalysisNarrowera  
    For reducing an `Analysis` to a subset of its lines.

    Originally this was a simpler method on Analysis, but that led to quadratic
    behavior.  This class does the bulk of the work up-front to provide the
    same results in linear time.

    Create an AnalysisNarrower from an Analysis, bulk-add region lines to it
    with `add_regions`, then individually request new narrowed Analysis objects
    for each region with `narrow`.  Doing most of the work in limited calls to
    `add_regions` lets us avoid poor performance.
    analysisr   r   r9   c                 C  s.   || _ tt| _tt| _tt| _d S r:   )rt   r+   r,   r-   region2arc_possibilitiesregion2arc_executeddictregion2exit_counts)r@   rt   r   r   r   __init__   s   
zAnalysisNarrower.__init__linessIterable[set[TLineNo]]c           	        s   | j jrHi  |D ]}t|}|D ]}| |< qqd
 fdd}|| j j| j || j j| j | j j D ]\}} 	| }rG|| j
| |< q5d	S d	S )z
        Pre-process a number of sets of line numbers.  Later calls to `narrow`
        with one of these sets will provide a narrowed Analysis.
        arc_setr8   region2arcsdict[TRegionLines, set[TArc]]r   r9   c                   sR   | D ]$\}}  | }r|| ||f   | }r&|| ||f qd S r:   )getr.   )r|   r}   abrline2regionr   r   collect_arcs   s   z2AnalysisNarrower.add_regions.<locals>.collect_arcsN)r|   r8   r}   r~   r   r9   )rt   r   	frozensetr"   ru   r#   rv   r$   r/   r   rx   )	r@   rz   r&   fzlinesliner   lnonumr   r   r   r   add_regions   s    

zAnalysisNarrower.add_regionsr&   r7   c           
      C  s   | j j|@ }| j j|@ }| j j|@ }| j jr0t|}| j| }| j| }| j| }| j j	|@ }	nt
 }t
 }i }t
 }	t| j j| j j| j j|||||||	d
S )zCreate a narrowed Analysis.

        The current analysis is copied to make a new one that only considers
        the lines in `lines`.
        r   )rt   r   r    r!   r   r   ru   rv   rx   r%   r-   r   r   r   )
r@   r&   r   r    r!   r   r"   r#   r$   r%   r   r   r   narrow  s2   


zAnalysisNarrower.narrowN)rt   r   r   r9   )rz   r{   r   r9   )r&   r7   r   r   )rn   ro   rp   rq   ry   r   r   r   r   r   r   rs      s
    

rs   c                   @  s*  e Zd ZU dZdZded< dZded< dZded< dZded< dZ	ded< dZ
ded	< dZded
< dZded< ed0ddZed0ddZed1ddZed1ddZd2ddZed3ddZed3ddZed3dd Zed4d"d#Zed4d$d%Zed4d&d'Zed1d(d)Zd5d+d,Zd6d-d.Zd/S )7rP   zThe numerical results of measuring coverage.

    This holds the basic statistics from `Analysis`, and is used to roll
    up statistics across files.

    r   r   r   rC   rD   rE   rF   rG   rH   rI   r   c                 C     | j | j S )z*Returns the number of executed statements.)rD   rF   r?   r   r   r   
n_executed<     zNumbers.n_executedc                 C  r   )z(Returns the number of executed branches.)rG   rI   r?   r   r   r   n_executed_branchesA  r   zNumbers.n_executed_branchestuple[int, int]c                 C     | j | jfS )z4Return numerator/denominator for statement coverage.)r   rD   r?   r   r   r   ratio_statementsF  r   zNumbers.ratio_statementsc                 C  r   )z1Return numerator/denominator for branch coverage.)r   rG   r?   r   r   r   ratio_branchesK  r   zNumbers.ratio_branches	numeratordenominatorfloatc                 C  s   |dkr
d| | S dS )zHelper for pc_* properties.r         Y@r   r@   r   r   r   r   r   _percentP  s   zNumbers._percentc                 C     | j | j S )z/Returns a single percentage value for coverage.)r   ratio_coveredr?   r   r   r   
pc_coveredV  r   zNumbers.pc_coveredc                 C  r   )z.Returns the percentage covered for statements.)r   r   r?   r   r   r   pc_statements[  r   zNumbers.pc_statementsc                 C  r   )z,Returns the percentage covered for branches.)r   r   r?   r   r   r   pc_branches`  r   zNumbers.pc_branchesr   c                 C     t | j| jS )a  Returns the percent covered, as a string, without a percent sign.

        Note that "0" is only returned when the value is truly zero, and "100"
        is only returned when the value is truly 100.  Rounding can never
        result in either "0" or "100".

        )display_coveredr   r   r?   r   r   r   pc_covered_stre  s   	zNumbers.pc_covered_strc                 C  r   )z=Returns the statement percent covered without a percent sign.)r   r   r   r?   r   r   r   pc_statements_strp     zNumbers.pc_statements_strc                 C  r   )z:Returns the branch percent covered without a percent sign.)r   r   r   r?   r   r   r   pc_branches_stru  r   zNumbers.pc_branches_strc                 C  s    | j | j }| j| j }||fS )z:Return a numerator and denominator for the coverage ratio.)r   r   rD   rG   r   r   r   r   r   z  s   zNumbers.ratio_coveredotherc              
   C  sP   t | j| j|j | j|j | j|j | j|j | j|j | j|j | j|j S r:   )	rP   r   rC   rD   rE   rF   rG   rH   rI   r@   r   r   r   r   __add__  s   






zNumbers.__add__c                 C  s   |dksJ | S )Nr   r   r   r   r   r   __radd__  s   zNumbers.__radd__Nrm   )r   r   )r   r   r   r   r   r   )r   r   )r   r   )r   rP   r   rP   )r   r   r   rP   )rn   ro   rp   rq   r   rr   rC   rD   rE   rF   rG   rH   rI   propertyr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rP   *  sF   
 


rP   pcr   c                 C  sj   dd|  }d|   k r|k rn n|} nd| |   k r!dk r(n nd| } nt | |} | d| dS )zReturn a displayable total percentage, as a string.

    Note that "0" is only returned when the value is truly zero, and "100"
    is only returned when the value is truly 100.  Rounding can never
    result in either "0" or "100".

    g      ?
   r   r   d   .f)round)r   r   near0r   r   r   r     s   

r   r   Iterable[TLineNo]r&   list[tuple[TLineNo, TLineNo]]c                 C  s   t | } t |}g }d}d}| D ]&}|t|kr n||| kr+|d7 }|s(|}|}q|r6|||f d}q|r@|||f |S )z,Produce a list of ranges for `format_lines`.Nr   r   )rJ   r   rb   )r   r&   pairsstartlidxstmtendr   r   r   _line_ranges  s(   r   r)   .Iterable[tuple[TLineNo, list[TLineNo]]] | Nonec           
      C  s   dd t | |D }|dur<t|}|D ]'\}}t|D ]}||vr:||vr:|dkr,|nd}||| d| f qqddd	 t|D }	|	S )
a  Nicely format a list of line numbers.

    Format a list of line numbers for printing by coalescing groups of lines as
    long as the lines represent consecutive statements.  This will coalesce
    even if there are gaps between statements.

    For example, if `statements` is [1,2,3,4,5,10,11,12,13,14] and
    `lines` is [1,2,5,10,11,13,14] then the result will be "1-2, 5-11, 13-14".

    Both `lines` and `statements` can be any iterable. All of the elements of
    `lines` must be in `statements`, and all of the values must be positive
    integers.

    If `arcs` is provided, they are (start,[end,end,end]) pairs that will be
    included in the output as long as start isn't in `lines`.

    c                 S  s   g | ]
}|d  t |fqS )r   r   )r   pairr   r   r   r]     r^   z format_lines.<locals>.<listcomp>Nr   exitz->z, c                 s  s    | ]}|d  V  qdS )Nr   )r   tr   r   r   rA     s    zformat_lines.<locals>.<genexpr>)r   rJ   rb   join)
r   r&   r)   
line_items
line_exitsr   rk   exdestretr   r   r   rU     s   rU   total
fail_underr6   c                 C  sL   d|  kr
dksn d| d}t ||dkr| dkrdS t| ||k S )a'  Determine if a total should fail due to fail-under.

    `total` is a float, the coverage measurement total. `fail_under` is the
    fail_under setting to compare with. `precision` is the number of digits
    to consider after the decimal point.

    Returns True if the total should fail.

    r   r   zfail_under=z' is invalid. Must be between 0 and 100.T)r   r   )r   r   r   msgr   r   r   should_fail_under  s   r   )
r   r
   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r&   r   r   r   r:   )r   r   r&   r   r)   r   r   r   )r   r   r   r   r   r   r   r6   )rq   
__future__r   r+   dataclassescollections.abcr   typingr   coverage.exceptionsr   coverage.miscr   coverage.typesr   r	   coverage.datar
   coverage.pluginr   r5   	dataclassr   r   TRegionLinesrs   rP   r   r   rU   r   r   r   r   r   <module>   s0   
7z_
h
#