
    ?g3                    ,   S r SSKJr  SSKJr  SSKrSSKJr  SSKJ	r	  SSKJ
r
  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  \(       a  SSKJr  SSKrSSKJr  SSKJr  SSKJr  SSKJr  \" S5      r\S"S j5       r\SS.S#S jj5       r\S$S j5       r S%       S&S jjr\ S'SS.     S(S jjj5       r\          S)S j5       r\ 4SS.         S*S jjjr " S  S\RB                  5      r"\ " S! S\"5      5       r#g)+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Tc               #     #    [        SS9n U    [        R                  " S5        U v   SSS5        g! , (       d  f       g= f7f)zReturn a :class:`WarningsRecorder` instance that records all warnings emitted by test functions.

See :ref:`warnings` for information on warning categories.
T	_ispytestdefaultN)WarningsRecorderwarningssimplefilter)wrecs    1/usr/lib/python3/dist-packages/_pytest/recwarn.pyrecwarnr   "   s0      d+D	i(
 
s   A2	A
A A.matchr   c                    g N r   s    r   deprecated_callr$   .   s    TW    c                    g r"   r#   )funcargskwargss      r   r$   r$   2   s    MPr%   c                T    SnU b  U /UQ7n[        [        [        [        4/UQ70 UD6$ )ae  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.
T)warnsDeprecationWarningPendingDeprecationWarningFutureWarning)r'   r(   r)   __tracebackhide__s       r   r$   r$   6   sC    4 }t}	6FIMQW r%   WarningsCheckerc                   g r"   r#   )expected_warningr    s     r   r+   r+   X   s    
 r%   c                    g r"   r#   )r2   r'   r(   r)   s       r   r+   r+   `   s     	r%   c               H   SnU(       d:  U(       a)  SR                  [        U5      5      n[        SU S35      e[        XSS9$ US   n[	        U5      (       d  [        U< S[        U5       S35      e[        U SS	9   U" US
S 0 UD6sSSS5        $ ! , (       d  f       g= f)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	TypeErrorr0   callabletype)r2   r    r(   r)   r/   argnamesr'   s          r   r+   r+   i   s    ^ yy0HGz56  /TRRAw~~thod4j\ASTUU->ab,V, ?>>s   >B
B!c                     ^  \ rS rSrSrSS.SU 4S jjjr\SS j5       rSS jrSS jr	SS	 jr
\4SS
 jjrSS jrSU 4S jjr        SU 4S jjrSrU =r$ )r      a.  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   c               R   > [        U5        [        TU ]	  SS9  SU l        / U l        g )NT)recordF)r   super__init___entered_list)selfr   	__class__s     r   rB   WarningsRecorder.__init__   s)    y!%46
r%   c                    U R                   $ )zThe list of recorded warnings.rD   rE   s    r   listWarningsRecorder.list   s     zzr%   c                     U R                   U   $ )z Get a recorded warning by index.rI   )rE   is     r   __getitem__WarningsRecorder.__getitem__   s    zz!}r%   c                ,    [        U R                  5      $ )z&Iterate through the recorded warnings.)iterrD   rJ   s    r   __iter__WarningsRecorder.__iter__   s    DJJr%   c                ,    [        U R                  5      $ )z The number of recorded warnings.)lenrD   rJ   s    r   __len__WarningsRecorder.__len__   s    4::r%   c                   Sn[        U R                  5       H  u  p4UR                  U:X  a  U R                  R                  U5      s  $ [	        UR                  U5      (       d  MO  Ub4  [	        UR                  U R                  U   R                  5      (       a  M  UnM     Ub  U R                  R                  U5      $ Sn[        U< S35      e)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)	enumeraterD   categorypop
issubclassAssertionError)rE   clsbest_idxrN   wr/   s         r   r\   WarningsRecorder.pop   s    
  $djj)DAzzS zz~~a((!**c** !!**djj.B.K.KLL * ::>>(++ w&@ABBr%   c                "    / U R                   SS& g)z$Clear the list of recorded warnings.NrI   rJ   s    r   clearWarningsRecorder.clear   s    

1r%   c                   > U R                   (       a  Sn[        SU < S35      e[        TU ]  5       nUc   eX l        [
        R                  " S5        U $ )NTzCannot enter z twicealways)rC   RuntimeErrorrA   	__enter__rD   r   r   )rE   r/   rD   rF   s      r   ri   WarningsRecorder.__enter__   sU    == $thf=>>!#   
h'r%   c                z   > U R                   (       d  Sn[        SU < S35      e[        TU ]  XU5        SU l         g )NTzCannot exit z without entering firstF)rC   rh   rA   __exit__)rE   exc_typeexc_valexc_tbr/   rF   s        r   rl   WarningsRecorder.__exit__   s?     }} $dX5LMNNF3 r%   )rC   rD   )r   boolreturnNone)rr   zlist[warnings.WarningMessage])rN   intrr   warnings.WarningMessage)rr   z!Iterator[warnings.WarningMessage])rr   rt   )r_   ztype[Warning]rr   ru   )rr   rs   )rr   r   rm   ztype[BaseException] | Nonern   zBaseException | Nonero   zTracebackType | Nonerr   rs   )__name__
__module____qualname____firstlineno____doc__rB   propertyrK   rO   rS   rW   Warningr\   rd   ri   rl   __static_attributes____classcell__rF   s   @r   r   r      s    
 -2 7 7    (/ C&	, & %	
 
 r%   c                  t   ^  \ rS rSr\S4SS.       S	U 4S jjjjrS
S jr        SU 4S jjrSrU =r	$ )r0      NFr   c               v  > [        U5        [        TU ]	  SS9  Sn[        U[        5      (       a8  U H/  n[        U[        5      (       a  M  [        U[        U5      -  5      e   UnOE[        U[        5      (       a  [        U[        5      (       a  U4nO[        U[        U5      -  5      eX`l	        X l
        g )NTr   z/exceptions must be derived from Warning, not %s)r   rA   rB   
isinstancetupler]   r}   r9   r;   r2   r5   )rE   r2   r5   r   msgexcexpected_warning_tuprF   s          r   rB   WarningsChecker.__init__  s     	y!4(?&..'!#w//#C$s)O44 ( $4 ($//Jg5
 5
 %5#6 C$'7"8899 4$r%   c                   U R                   c   e[        UR                  U R                   5      =(       aR    [        U R                  S L =(       d4    [
        R                  " U R                  [        UR                  5      5      5      $ r"   )	r2   r]   r[   rq   r5   researchstrmessage)rE   warnings     r   matchesWarningsChecker.matches  se    $$000'**D,A,AB 
tOOt#Wryy#gooBV'WH
 	
r%   c                  >^  [         TT ]  XU5        SnUb+  [        U[        5      (       a  [        U[        5      (       a  g SU 4S jjn [        U 4S jT  5       5      (       d"  [        ST R                   SU" 5        S35        OH[        U 4S jT  5       5      (       d.  [        ST R                   ST R                   S	U" 5        S35        T  Hp  nT R                  U5      (       a  M  [        R                  " UR                  UR                  UR                  UR                  UR                   UR"                  S
9  Mr     T  H  n[%        UR                  5      [&        La  M!  UR                  R(                  (       d  M>  UR                  R(                  S   n[        U[*        5      (       a  Mn  [-        SU< S[%        U5      R.                   S35      e   g ! T  Hp  nT R                  U5      (       a  M  [        R                  " UR                  UR                  UR                  UR                  UR                   UR"                  S
9  Mr     T  H  n[%        UR                  5      [&        La  M!  UR                  R(                  (       d  M>  UR                  R(                  S   n[        U[*        5      (       a  Mn  [-        SU< S[%        U5      R.                   S35      e   f = f)NTc                 V   > [        T V s/ s H  o R                  PM     sn SS9$ s  sn f )N   )indent)r   r   )r@   rE   s    r   	found_str+WarningsChecker.__exit__.<locals>.found_str7  s$    >vNN>qII>s   &c              3  d   >#    U  H%  n[        UR                  TR                  5      v   M'     g 7fr"   )r]   r[   r2   .0ra   rE   s     r   	<genexpr>+WarningsChecker.__exit__.<locals>.<genexpr>;  s%     Sdz!**d.C.CDDds   -0z"DID NOT WARN. No warnings of type z" were emitted.
 Emitted warnings: .c              3  F   >#    U  H  nTR                  U5      v   M     g 7fr"   )r   r   s     r   r   r   @  s     7$Q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 ))rr   r   )rA   rl   r   	Exceptionr   anyr   r2   r5   r   r   warn_explicitr   r[   r   r   rx   r   r;   UserWarningr(   r   r9   rw   )	rE   rm   rn   ro   r/   r   ra   r   rF   s	   `       r   rl   WarningsChecker.__exit__"  sq    	F3  7I..'4((	J2	SdSSS89N9N8O P**3+a9 7$77789N9N8O P#/ 0**3+a9 ||A** !		!"!" xx || xx & 		?+5 yy~~iinnQ'c3''  :3'cI[I[H\\]^  ' ||A** !		!"!" xx || xx & 		?+5 yy~~iinnQ'c3''  :3'cI[I[H\\]^  s   BG K/=C2K/)r2   r5   )r2   )type[Warning] | tuple[type[Warning], ...]r5   str | Pattern[str] | Noner   rq   rr   rs   )r   ru   rr   rq   rv   )
rw   rx   ry   rz   r}   rB   r   rl   r~   r   r   s   @r   r0   r0      s     GN04%
  %C% .%
 % 
% %4
J,J &J %	J
 
J Jr%   )rr   zGenerator[WarningsRecorder])r    r   rr   r   )r'   Callable[..., T]r(   r   r)   r   rr   r   r"   )r'   zCallable[..., Any] | Noner(   r   r)   r   rr   zWarningsRecorder | Any).)r2   r   r    r   rr   r0   )
r2   r   r'   r   r(   r   r)   r   rr   r   )
r2   r   r(   r   r    r   r)   r   rr   zWarningsChecker | Any)$r{   
__future__r   pprintr   r   typesr   typingr   r   r   r	   r
   r   r   r   r   typing_extensionsr   r   _pytest.deprecatedr   _pytest.fixturesr   _pytest.outcomesr   r   r   r   r$   r+   r}   catch_warningsr   r0   r#   r%   r   <module>r      s   5 "  	            &  - $ ! ! CL 	 	 
:= W 
 W 
 P 
 P '+
#36BED 
BE (+? % 	 
 
?
  	
  
 CJ=- (,=-?=-=- %=- 	=-
 =-@Tx.. Tn k& k kr%   