o
    h*                     @   s  d 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
 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 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 ddlmZ ddlmZ ddlmZ ddlmZ edZede
d fddZedddeeeee f  ddfdd Z ed!e	def d"ed#edefd$d Z 	d.d!ee	def  d"ed#ededef fd%d Z e	d/ddd&eee! eee! df f deeeee f  dd'fd(d)Z"ed&eee! eee! df f d!e	def d"ed#edef
d*d)Z"e!fddd&eee! eee! df f d"edeeeee f  d#eded'ef f
d+d)Z"G d,d dej#Z$eG d-d' d'e$Z%dS )0z/Record warnings during test function execution.    Npformat)TracebackType)Any)Callable)	Generator)Iterator)List)Optional)Pattern)Tuple)Type)TypeVar)Union)final)overload)check_ispytest)WARNS_NONE_ARG)fixture)failTreturn)WarningsRecorderNNc                  c   sF    t dd} |  td | V  W d   dS 1 sw   Y  dS )zReturn a :class:`WarningsRecorder` instance that records all warnings emitted by test functions.

    See https://docs.pytest.org/en/latest/how-to/capture-warnings.html for information
    on warning categories.
    T	_ispytestdefaultN)r   warningssimplefilter)wrec r   E/var/www/html/ai/venv/lib/python3.10/site-packages/_pytest/recwarn.pyrecwarn   s   

"r!   .matchr#   r   c                 C      d S Nr   r"   r   r   r    deprecated_call*      r&   funcargskwargsc                 O   r$   r%   r   )r(   r)   r*   r   r   r    r&   1   r'   c                 O   s0   d}| dur| f| }t ttfg|R i |S )a  Assert that code produces a ``DeprecationWarning`` or ``PendingDeprecationWarning``.

    This function can be used as a context manager::

        >>> import warnings
        >>> def api_call_v2():
        ...     warnings.warn('use v3 of this api', DeprecationWarning)
        ...     return 200

        >>> import pytest
        >>> with pytest.deprecated_call():
        ...    assert api_call_v2() == 200

    It can also be used by passing a function and ``*args`` and ``**kwargs``,
    in which case it will ensure calling ``func(*args, **kwargs)`` produces one of
    the warnings types above. The return value is the return value of the function.

    In the context manager form you may use the keyword argument ``match`` to assert
    that the warning matches a text or regex.

    The context manager produces a list of :class:`warnings.WarningMessage` objects,
    one for each warning raised.
    TN)warnsDeprecationWarningPendingDeprecationWarning)r(   r)   r*   __tracebackhide__r   r   r    r&   8   s   
expected_warningWarningsCheckerc                C   r$   r%   r   )r/   r#   r   r   r    r+   X   s   r+   c                 O   r$   r%   r   )r/   r(   r)   r*   r   r   r    r+   a   s   c                O   s   d}|s|rd t|}td| dt| |ddS |d }t|s0t|dt| dt| dd	 ||d
d i |W  d   S 1 sLw   Y  dS )a  Assert that code raises a particular class of warning.

    Specifically, the parameter ``expected_warning`` can be a warning class or sequence
    of warning classes, and the code inside the ``with`` block must issue at least one
    warning of that class or classes.

    This helper produces a list of :class:`warnings.WarningMessage` objects, one for
    each warning raised (regardless of whether it is an ``expected_warning`` or not).

    This function can be used as a context manager, which will capture all the raised
    warnings inside it::

        >>> import pytest
        >>> with pytest.warns(RuntimeWarning):
        ...    warnings.warn("my warning", RuntimeWarning)

    In the context manager form you may use the keyword argument ``match`` to assert
    that the warning matches a text or regex::

        >>> with pytest.warns(UserWarning, match='must be 0 or None'):
        ...     warnings.warn("value must be 0 or None", UserWarning)

        >>> with pytest.warns(UserWarning, match=r'must be \d+$'):
        ...     warnings.warn("value must be 42", UserWarning)

        >>> with pytest.warns(UserWarning, match=r'must be \d+$'):
        ...     warnings.warn("this is not here", UserWarning)
        Traceback (most recent call last):
          ...
        Failed: DID NOT WARN. No warnings of type ...UserWarning... were emitted...

    **Using with** ``pytest.mark.parametrize``

    When using :ref:`pytest.mark.parametrize ref` it is possible to parametrize tests
    such that some runs raise a warning and others do not.

    This could be achieved in the same way as with exceptions, see
    :ref:`parametrizing_conditional_raising` for an example.

    Tz, z5Unexpected keyword arguments passed to pytest.warns: z"
Use context-manager form instead?)
match_exprr   r   z object (type: z) must be callabler      N)joinsorted	TypeErrorr0   callabletype)r/   r#   r)   r*   r.   argnamesr(   r   r   r    r+   k   s   .
$c                       s   e Zd ZdZdddeddf fddZeded	 fd
dZde	dd	fddZ
ded	 fddZde	fddZefdee dd	fddZdddZd  fddZdeee  dee dee ddf fddZ  ZS )!r   aF  A context manager to record raised warnings.

    Each recorded warning is an instance of :class:`warnings.WarningMessage`.

    Adapted from `warnings.catch_warnings`.

    .. note::
        ``DeprecationWarning`` and ``PendingDeprecationWarning`` are treated
        differently; see :ref:`ensuring_function_triggers`.

    Fr   r   r   Nc                   s&   t | t jdd d| _g | _d S )NT)recordF)r   super__init___entered_list)selfr   	__class__r   r    r;      s   
zWarningsRecorder.__init__zwarnings.WarningMessagec                 C   s   | j S )zThe list of recorded warnings.r=   r>   r   r   r    list   s   zWarningsRecorder.listic                 C   s
   | j | S )z Get a recorded warning by index.rA   )r>   rD   r   r   r    __getitem__      
zWarningsRecorder.__getitem__c                 C   
   t | jS )z&Iterate through the recorded warnings.)iterr=   rB   r   r   r    __iter__   rF   zWarningsRecorder.__iter__c                 C   rG   )z The number of recorded warnings.)lenr=   rB   r   r   r    __len__   rF   zWarningsRecorder.__len__clsc                 C   sB   t | jD ]\}}t|j|r| j|  S qd}t|d)z>Pop the first recorded warning, raise exception if not exists.Tz not found in warning list)	enumerater=   
issubclasscategorypopAssertionError)r>   rL   rD   wr.   r   r   r    rP      s   zWarningsRecorder.popc                 C   s   g | j dd< dS )z$Clear the list of recorded warnings.NrA   rB   r   r   r    clear   s   zWarningsRecorder.clearc                    sD   | j rd}td| dt  }|d usJ || _td | S )NTzCannot enter z twicealways)r<   RuntimeErrorr:   	__enter__r=   r   r   )r>   r.   r=   r?   r   r    rV      s   

zWarningsRecorder.__enter__exc_typeexc_valexc_tbc                    s4   | j sd}td| dt ||| d| _ d S )NTzCannot exit z without entering firstF)r<   rU   r:   __exit__)r>   rW   rX   rY   r.   r?   r   r    rZ      s
   
zWarningsRecorder.__exit__)r   N)r   r   )__name__
__module____qualname____doc__boolr;   propertyr	   rC   intrE   r   rI   rK   Warningr   rP   rS   rV   r
   BaseExceptionr   rZ   __classcell__r   r   r?   r    r      s(    

c                
       s   e Zd Zedfdddeeee eee df f  deeee	e f  de
ddf fd	d
Zdeee  dee dee ddf fddZ  ZS )r0   NFr   r/   .r1   r   r   c                   s   t | t jdd d}|d u rtjtdd d }n+t|tr5|D ]}t|t	s1t
|t| q"|}nt|t	r>|f}nt
|t| || _|| _d S )NTr   z/exceptions must be derived from Warning, not %s   )
stacklevel)r   r:   r;   r   warnr   
isinstancetuplerN   rb   r5   r7   r/   r1   )r>   r/   r1   r   msgexpected_warning_tupexcr?   r   r    r;      s"   	



zWarningsChecker.__init__rW   rX   rY   c                    s   t  ||| d} fdd}|d u ro|d u rq|d u rs jd urut fdd D s=d}td j d|  d d S  jd urw D ]}t|j jr\t	 j
t|jr\ d S qDtd j d	 j d
|   d S d S d S d S d S d S )NTc                      s   t dd  D ddS )Nc                 S   s   g | ]}|j qS r   )message).0r9   r   r   r    
<listcomp>#  s    z?WarningsChecker.__exit__.<locals>.found_str.<locals>.<listcomp>   )indentr   r   rB   r   r    	found_str"  s   z+WarningsChecker.__exit__.<locals>.found_strc                 3   s    | ]
}t |j jV  qd S r%   )rN   rO   r/   )rn   rrB   r   r    	<genexpr>(  s    z+WarningsChecker.__exit__.<locals>.<genexpr>z"DID NOT WARN. No warnings of type z0 were emitted.
The list of emitted warnings is: .z* matching the regex were emitted.
 Regex: z
 Emitted warnings: )r:   rZ   r/   anyr   r1   rN   rO   recompilesearchstrrm   )r>   rW   rX   rY   r.   rr   rs   r?   rB   r    rZ     s<   


zWarningsChecker.__exit__)r[   r\   r]   rb   r
   r   r   r   rz   r   r_   r;   rc   r   rZ   rd   r   r   r?   r    r0      s2    
r%   ).)&r^   rw   r   pprintr   typesr   typingr   r   r   r   r	   r
   r   r   r   r   r   _pytest.compatr   r   _pytest.deprecatedr   r   _pytest.fixturesr   _pytest.outcomesr   r   r!   rz   r&   rb   r+   catch_warningsr   r0   r   r   r   r    <module>   s    


 



?O