'L iJ4dZddlmZddlmZddlmZddlmZddlmZddl Z ddl m Z dd l m Z dd l mZdd l mZdd l mZdd l mZerddlmZddlZddlmZddlmZddlmZddlmZedZed!dZedd d"dZed#dZ d$ d%dZe d&dd d'dZe d(dZe fdd d)dZGddejBZ"eGd de"Z#y)*z/Record warnings during test function execution.) annotations)Callable) Generator)Iterator)pformatN) TracebackType)Any)final)overload) TYPE_CHECKING)TypeVar)Self)check_ispytest)fixture)Exit)failTc#Ktd}|5tjd|dddy#1swYyxYww)zReturn a :class:`WarningsRecorder` instance that records all warnings emitted by test functions. See :ref:`warnings` for information on warning categories. T _ispytestdefaultN)WarningsRecorderwarnings simplefilter)wrecs U/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/_pytest/recwarn.pyrecwarnr!s< d +D i( sA4 A=A.matchrcyNrs rdeprecated_callr#-scyr!r")funcargskwargss rr#r#3sMPr$cRd}||g|}ttttfg|i|S)aAssert 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)warnsDeprecationWarningPendingDeprecationWarning FutureWarning)r&r'r(__tracebackhide__s rr#r#7sC4 }t}  6 F IM QW r$WarningsCheckercyr!r")expected_warningrs rr*r*Ys r$cyr!r")r1r&r'r(s rr*r*as r$c,d}|s9|r)djt|}td|dt||dS|d}t |st|dt |dt|d 5||d d i|cd d d S#1swYy xYw) aAssert 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_exprrrz object (type: z) must be callablerN)joinsorted TypeErrorr/callabletype)r1rr'r(r.argnamesr&s rr*r*js^  yy0HGz56 /ETRRAw~thod4j\ASTU U - > -ab,V, - - -s 5 B  BceZdZdZddd fdZeddZddZddZddZ e fdd Z dd Z dfd Z dfd ZxZS)raFA 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`. FrcXt|t| dd|_g|_y)NT)recordF)rsuper__init___entered_list)selfr __class__s rr@zWarningsRecorder.__init__s)y! % 46 r$c|jS)zThe list of recorded warnings.rBrCs rlistzWarningsRecorder.listszzr$c |j|S)z Get a recorded warning by index.rF)rCis r __getitem__zWarningsRecorder.__getitem__szz!}r$c,t|jS)z&Iterate through the recorded warnings.)iterrBrGs r__iter__zWarningsRecorder.__iter__sDJJr$c,t|jS)z The number of recorded warnings.)lenrBrGs r__len__zWarningsRecorder.__len__s4::r$cd}t|jD]z\}}|j|k(r|jj|cSt |j|sI|.t |j|j|jry|}|||jj|Sd}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) enumeraterBcategorypop issubclassAssertionError)rCclsbest_idxrJwr.s rrUzWarningsRecorder.pops $djj) DAqzzS zz~~a((!**c* !!**djj.B.K.KL   ::>>(+ + w&@ABBr$c"g|jddy)z$Clear the list of recorded warnings.NrFrGs rclearzWarningsRecorder.clears 1 r$c|jrd}td|dt| }|J||_t j d|S)NTz Cannot enter z twicealways)rA RuntimeErrorr? __enter__rBrr)rCr.rBrDs rr`zWarningsRecorder.__enter__sW == $ thf=> >!#    h' r$cp|jsd}td|dt| |||d|_y)NTz Cannot exit z without entering firstF)rAr_r?__exit__)rCexc_typeexc_valexc_tbr.rDs rrbzWarningsRecorder.__exit__sA }} $ dX5LMN N 7F3 r$)rboolreturnNone)rgzlist[warnings.WarningMessage])rJintrgwarnings.WarningMessage)rgz!Iterator[warnings.WarningMessage])rgri)rXz type[Warning]rgrj)rgrh)rgrrcztype[BaseException] | NonerdzBaseException | NonerezTracebackType | Nonergrh)__name__ __module__ __qualname____doc__r@propertyrHrKrNrQWarningrUr\r`rb __classcell__rDs@rrrsz -27  (/C& ,&%   r$c^eZdZedfdd dfdZddZ d fd ZxZS) r/NFrcTt|t| dd}t|tr1|D])}t |t rt|t|z|}n;t|trt |t r|f}nt|t|z||_ ||_ y)NTrz/exceptions must be derived from Warning, not %s) rr?r@ isinstancetuplerVrqr8r:r1r4)rCr1r4rmsgexcexpected_warning_tuprDs rr@zWarningsChecker.__init__s y! 4(? & .' 5!#w/#C$s)O44 5$4 ($ /J g5 %5#6 C$'7"889 9 4$r$c|jJt|j|jxrLt|jduxs3t j |jt|jSr!) r1rVrTrfr4researchstrmessage)rCwarnings rmatcheszWarningsChecker.matchesse$$000'**D,A,AB t OOt # Wryy#gooBV'WH  r$c \t|||d}|!t|trt|trydfd } t fdDs"t djd|dnBt fdDs.t djdjd |dD]k}j|rtj|j|j|j|j|j |j" mD]}t%|jt&ur|jj(s6|jj(d }t|t*r`t-d |d t%|j.dy#D]k}j|rtj|j|j|j|j|j |j" mD]}t%|jt&ur|jj(s6|jj(d }t|t*r`t-d |d t%|j.dwxYw)NTcXtDcgc]}|jc}dScc}w)N)indent)rr)r>rCs r found_strz+WarningsChecker.__exit__..found_str:s">vFNN>qI I>s'c3^K|]$}t|jj&ywr!)rVrTr1.0rZrCs r z+WarningsChecker.__exit__..>s"Sz!**d.C.CDSs*-z"DID NOT WARN. No warnings of type z" were emitted. Emitted warnings: .c3@K|]}j|ywr!)rrs rrz+WarningsChecker.__exit__..Cs7Qa7sz* matching the regex were emitted. Regex: z Emitted warnings: )rrTfilenamelinenomodulesourcerz$Warning must be str or Warning, got z (type ))rgr~)r?rbrv Exceptionranyrr1r4rr warn_explicitrrTrrrmrr: UserWarningr'r~r8rl) rCrcrdrer.rrZrxrDs ` rrbzWarningsChecker.__exit__%sq 7F3   7I.'4(  J2 SdSS89N9N8OP**3+a97$7789N9N8OP#/0**3+a9 ||A** ! !"!" xx || xx  &  ?+5yy~~iinnQ'c3' :3'cI[I[H\\]^ ' ||A** ! !"!" xx || xx  &  ?+5yy~~iinnQ'c3' :3'cI[I[H\\]^ sA8F11J+ C"J+)r1)type[Warning] | tuple[type[Warning], ...]r4str | re.Pattern[str] | Nonerrfrgrh)rrjrgrfrk)rlrmrnrqr@rrbrrrss@rr/r/s}GN37%  %C%1%  %  %4 J,J&J% J  JJr$)rgzGenerator[WarningsRecorder])rrrgr)r&Callable[..., T]r'r r(r rgrr!)r&zCallable[..., Any] | Noner'r r(r rgzWarningsRecorder | Any).)r1rrrrgr/) r1rr&rr'r r(r rgr) r1rr'r rrr(r rgzWarningsChecker | Any)$ro __future__rcollections.abcrrrpprintrr|typesrtypingr r r r r typing_extensionsrr_pytest.deprecatedr_pytest.fixturesr_pytest.outcomesrrrrr#r*rqcatch_warningsrr/r"r$rrs5"$%$  &-$!! CL   -0*   P P'+ #36BED BE+.? (    ?          CJ=-+/=-?=- =- (=- =-  =-@Vx..Vrk&kkr$