o
    rh3                     @  s  d Z ddlmZ ddlm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 erVddlm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 edZed3ddZeddd4ddZed5d%dZ	d6d7d(dZe	d8ddd9d,d-Zed:d.d-Ze fddd;d0d-ZG d1d dej!Z"eG d2d+ d+e"Z#dS )<z/Record warnings during test function execution.    )annotationspformatN)TracebackType)Any)Callable)final)	Generator)Iterator)overload)Pattern)TYPE_CHECKING)TypeVar)Self)check_ispytest)fixture)Exit)failTreturnGenerator[WarningsRecorder]c                  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)WarningsRecorderwarningssimplefilter)wrec r   M/var/www/html/alpaca_bot/venv/lib/python3.10/site-packages/_pytest/recwarn.pyrecwarn"   s   

"r    .matchr"   str | Pattern[str] | Noner   c                 C     d S Nr   r!   r   r   r   deprecated_call/      r&   funcCallable[..., T]argsr   kwargsc                 O  r$   r%   r   )r(   r*   r+   r   r   r   r&   3   r'   Callable[..., Any] | NoneWarningsRecorder | Anyc                 O  s4   d}| dur| g|R }t tttfg|R i |S )a  Assert that code produces a ``DeprecationWarning`` or ``PendingDeprecationWarning`` or ``FutureWarning``.

    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FutureWarning)r(   r*   r+   __tracebackhide__r   r   r   r&   7   s   expected_warning)type[Warning] | tuple[type[Warning], ...]WarningsCheckerc                C  r$   r%   r   )r3   r"   r   r   r   r.   Y   s   r.   c                 O  r$   r%   r   )r3   r(   r*   r+   r   r   r   r.   a   s   WarningsChecker | Anyc                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 tuple
    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 emitted (regardless of whether it is an ``expected_warning`` or not).
    Since pytest 8.0, unmatched warnings are also re-emitted when the context closes.

    This function can be used as a context manager::

        >>> 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):  # catch re-emitted warning
        ...     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	TypeErrorr5   callabletype)r3   r"   r*   r+   r2   argnamesr(   r   r   r   r.   j   s   /
$c                      s   e Zd ZdZddd( fdd	Zed)ddZd*ddZd+ddZd,ddZ	e
fd-ddZd.ddZd/ fddZd0 fd&d'Z  ZS )1r   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   boolr   Nonec                  s&   t | t jdd d| _g | _d S )NT)recordF)r   super__init___entered_list)selfr   	__class__r   r   rC      s   
zWarningsRecorder.__init__list[warnings.WarningMessage]c                 C  s   | j S )zThe list of recorded warnings.rE   rF   r   r   r   list   s   zWarningsRecorder.listiintwarnings.WarningMessagec                 C  s
   | j | S )z Get a recorded warning by index.rJ   )rF   rM   r   r   r   __getitem__      
zWarningsRecorder.__getitem__!Iterator[warnings.WarningMessage]c                 C  
   t | jS )z&Iterate through the recorded warnings.)iterrE   rK   r   r   r   __iter__   rQ   zWarningsRecorder.__iter__c                 C  rS   )z The number of recorded warnings.)lenrE   rK   r   r   r   __len__   rQ   zWarningsRecorder.__len__clstype[Warning]c                 C  s   d}t | jD ]'\}}|j|kr| j|  S t|j|r.|du s,t|j| j| js.|}q|dur9| j|S d}t|d)zPop the first recorded warning which is an instance of ``cls``,
        but not an instance of a child class of any other match.
        Raises ``AssertionError`` if there is no match.
        NTz not found in warning list)	enumeraterE   categorypop
issubclassAssertionError)rF   rX   best_idxrM   wr2   r   r   r   r\      s   
zWarningsRecorder.popc                 C  s   g | j dd< dS )z$Clear the list of recorded warnings.NrJ   rK   r   r   r   clear   s   zWarningsRecorder.clearr   c                   sD   | j rd}td| dt  }|d usJ || _td | S )NTzCannot enter z twicealways)rD   RuntimeErrorrB   	__enter__rE   r   r   )rF   r2   rE   rG   r   r   rd      s   

zWarningsRecorder.__enter__exc_typetype[BaseException] | Noneexc_valBaseException | Noneexc_tbTracebackType | Nonec                   s4   | j sd}td| dt ||| d| _ d S )NTzCannot exit z without entering firstF)rD   rc   rB   __exit__)rF   re   rg   ri   r2   rG   r   r   rk      s
   
zWarningsRecorder.__exit__)r   r?   r   r@   )r   rI   )rM   rN   r   rO   )r   rR   )r   rN   )rX   rY   r   rO   )r   r@   )r   r   re   rf   rg   rh   ri   rj   r   r@   )__name__
__module____qualname____doc__rC   propertyrL   rP   rU   rW   Warningr\   ra   rd   rk   __classcell__r   r   rG   r   r      s    



c                      sB   e Zd Zedfddd fddZdddZd fddZ  ZS )r5   NFr   r3   r4   r7   r#   r   r?   r   r@   c                  s   t | t jdd d}t|tr'|D ]}t|ts#t|t| q|}nt|tr5t|tr5|f}nt|t| || _	|| _
d S )NTr   z/exceptions must be derived from Warning, not %s)r   rB   rC   
isinstancetupler]   rr   r;   r=   r3   r7   )rF   r3   r7   r   msgexcexpected_warning_tuprG   r   r   rC     s    


zWarningsChecker.__init__warningrO   c                 C  s>   | j d usJ t|j| j ot| jd u pt| jt|jS r%   )	r3   r]   r[   r?   r7   researchstrmessage)rF   ry   r   r   r   matches  s   zWarningsChecker.matchesre   rf   rg   rh   ri   rj   c                   s  t  ||| d}|d urt|trt|trd S d fdd}z~t fdd D s;td j d	|  d
 nt fdd D sWtd j d j d|  d
 W  D ]} 	|srt
j|j|j|j|j|j|jd qZ D ](}t|jturqu|jjsqu|jjd }t|trqutd|dt|j dd S  D ]} 	|st
j|j|j|j|j|j|jd q D ](}t|jturq|jjsq|jjd }t|trqtd|dt|j dw )NTr   r|   c                     s   t dd  D ddS )Nc                 S  s   g | ]}|j qS r   )r}   ).0rA   r   r   r   
<listcomp>9  s    z?WarningsChecker.__exit__.<locals>.found_str.<locals>.<listcomp>   )indentr   r   rK   r   r   	found_str8  s   z+WarningsChecker.__exit__.<locals>.found_strc                 3  s    | ]
}t |j jV  qd S r%   )r]   r[   r3   r   r`   rK   r   r   	<genexpr><  s    z+WarningsChecker.__exit__.<locals>.<genexpr>z"DID NOT WARN. No warnings of type z" were emitted.
 Emitted warnings: .c                 3  s    | ]}  |V  qd S r%   )r~   r   rK   r   r   r   A  s    z* matching the regex were emitted.
 Regex: z
 Emitted warnings: )r}   r[   filenamelinenomodulesourcer   z$Warning must be str or Warning, got z (type ))r   r|   )rB   rk   rt   	Exceptionr   anyr   r3   r7   r~   r   warn_explicitr}   r[   r   r   rn   r   r=   UserWarningr*   r|   r;   rm   )rF   re   rg   ri   r2   r   r`   rv   rG   rK   r   rk   #  s   





zWarningsChecker.__exit__)r3   r4   r7   r#   r   r?   r   r@   )ry   rO   r   r?   rl   )rm   rn   ro   rr   rC   r~   rk   rs   r   r   rG   r   r5     s    
)r   r   )r"   r#   r   r   )r(   r)   r*   r   r+   r   r   r   r%   )r(   r,   r*   r   r+   r   r   r-   ).)r3   r4   r"   r#   r   r5   )
r3   r4   r(   r)   r*   r   r+   r   r   r   )
r3   r4   r*   r   r"   r#   r+   r   r   r6   )$rp   
__future__r   pprintr   rz   typesr   typingr   r   r   r	   r
   r   r   r   r   typing_extensionsr   r   _pytest.deprecatedr   _pytest.fixturesr   _pytest.outcomesr   r   r   r    r&   r.   rr   catch_warningsr   r5   r   r   r   r   <module>   sX   "	@W