'L icJUdZddlmZddlZddlmZddlmZddlmZddlmZddl m Z ddl Z ddl Z ddl Z dd lmZddlZddlZddlZddlZddlZdd lmZdd lmZddlZdd lmZdd lmZddlmZddlmZddlm Z ddl!m"Z"ddl#m$Z$ddl%m&Z&ddl'm(Z(ddl'm)Z)ddl*m+Z+ddl*m,Z,ddl-m.Z.ddl-m/Z/ddl0m1Z1ddl2m3Z3ddl4m5Z5ddl6m7Z7er ddl8Z8ddl9m:Z:dZ;d Zd#Z?e;ee?fZ@daAdaBd$eCd%<dGd&ZDdHd'ZE dId(ZFdJd)ZGdKd*ZHdJd+ZIGd,d-eZJGd.d/eKZLdLd0ZM dM dNd1ZNGd2d3e,ZOdOd4ZPdPd5ZQdQd6ZRGd7d8e3ZSdRd9ZTdSd:ZUe dTd;ZVGd<d=e3ZWdUd>ZXdVd?ZYdWd@ZZdWdAZ[dWdBZ\dXdCZ]e(dDEdYdFZ^y)Zz4Discover and run doctests in modules and test files.) annotationsN)Callable) Generator)Iterable)Sequence)contextmanager)Path)Any) TYPE_CHECKING)outcomes) ExceptionInfo)ReprFileLocation) TerminalRepr)TerminalWriter safe_getattr)Config)Parser)fixture) TopRequest) Collector)Item)OutcomeException)skip fnmatch_ex)Module)approx) PytestWarning)Selfnonecdiffndiffudiffonly_first_failurez"type[doctest.OutputChecker] | None CHECKER_CLASSc~|jddddg|jddd |jd }|jd d d dd|jdtjddt d|jddgddd|jdd d dd|jdd d d d!y)"Ndoctest_optionflagszOption flags for doctestsargsELLIPSIS)typedefaultdoctest_encodingzEncoding used for doctest fileszutf-8)r,collectz--doctest-modules store_trueFzRun doctests in all .py modulesdoctestmodules)actionr,helpdestz--doctest-reportr$z9Choose another output format for diffs on doctest failure doctestreport)r+r,r2choicesr3z--doctest-globappendpatz2Doctests file matching pattern, default: test*.txt doctestglob)r1r,metavarr2r3z--doctest-ignore-import-errorsz Ignore doctest collection errorsdoctest_ignore_import_errorsz--doctest-continue-on-failurezz_is_doctest..s8$z$%8s)rPsession isinitpath getoptionrR)rOrNrZglobss ` rDrWrWsL {{&&6>>+D+DT+J   ] + < }E 8%8 88rFc |jdk(S)Nz __main__.py)r]rMs rDrTrTs 99 %%rFc$eZdZ ddZddZy)ReprFailDoctestc||_yrH)reprlocation_lines)selfrms rD__init__zReprFailDoctest.__init__s #5rFc~|jD].\}}|D]}|j||j|0yrH)rmline toterminal)rntw reprlocationlinesrqs rDrrzReprFailDoctest.toterminalsD#'#:#: ( L%     # #B ' (rFN)rmz0Sequence[tuple[ReprFileLocation, Sequence[str]]]returnNone)rsrrvrw)__name__ __module__ __qualname__rorrrJrFrDrkrks5"R5 5 (rFrkc eZdZdfd ZxZS)MultipleDoctestFailuresc0t|||_yrH)superrofailures)rnr __class__s rDroz MultipleDoctestFailures.__init__s   rF)rz Sequence[doctest.DocTestFailure]rvrw)rxryrzro __classcell__rs@rDr|r|s !!rFr|c>ddlGfddj}|S)NrczeZdZdZ d dfd Z dfd Z dfd ZxZS) /_init_runner_class..PytestDoctestRunnerzRunner to collect failures. Note that the out variable in this case is a list instead of a stdout-like object. c8t||||||_y)N)checkerverbose optionflags)r~rocontinue_on_failure)rnrrrrrs rDroz8_init_runner_class..PytestDoctestRunner.__init__s! G Wg;  W':D $rFclj|||}|jr|j|y|rH)DocTestFailurerr6)rnouttestexamplegotfailuredoctests rDreport_failurez>_init_runner_class..PytestDoctestRunner.report_failures5-g,,T7C@G'' 7# rFct|dtr|dt|dtjrt j dj |||}|jr|j|y|)NzQuitting debugger) isinstancerbdbBdbQuitr exitUnexpectedExceptionrr6)rnrrrexc_inforrs rDreport_unexpected_exceptionzK_init_runner_class..PytestDoctestRunner.report_unexpected_exceptionsm(1+'78qk!(1+s{{3 121g11$JG'' 7# rFNNrT) rdoctest.OutputChecker | Noner bool | Nonerintrboolrvrw)rdoctest.DocTestrdoctest.Examplerr?rvrw)rrrrrz>tuple[type[BaseException], BaseException, types.TracebackType]rvrw)rxryrz__doc__rorrr)rrs@rDPytestDoctestRunnerrs 59#' (,  ;1 ;! ;  ; "&  ;   ; " %      " %   U      rFr)r DebugRunner)rrs @rD_init_runner_classrs!-g11-^ rFc@t tat||||S)Nrrrr)rIrrs rD _get_runnerrs-)+  /  rFceZdZ d fd Ze d fd Zd dZd dZd dZd dZ d fd Z d dZ xZ S) DoctestItemct|||||_||_d|_|j j }|j|dd}||_|j|_ |jy)N)nodefunccls) r~rorunnerdtestobjre_fixturemanagergetfixtureinfo _fixtureinfo names_closure fixturenames _initrequest)rnr]rZrrfm fixtureinfors rDrozDoctestItem.__init__ss v&   \\ ) )''T$'G ''55 rFc*t|||||S)zThe public named constructor.)r]rZrr)r~rV)rrZr]rrrs rDrVzDoctestItem.from_parentsw"VFRW"XXrFc6i|_t|d|_y)NT) _ispytest)funcargsr_requestrns rDrzDoctestItem._initrequests+- "448 rFc.|jjt|jj}|jjdj D] \}}|||< |j j j|y)N) getfixturedoctest_namespace)r _fillfixturesdictgetfixturevalueitemsrrhupdate)rnrhr]values rDsetupzDoctestItem.setup ss ##% = =>==889LMSSU KD%E$K  &rFct|j|jg}|jj |j||r t |y)N)r)_check_all_skippedr$_disable_output_capturing_for_darwinrrunr|)rnrs rDruntestzDoctestItem.runtest'sL4::& 11313  1 )(3 3 rFcJtjdk7ry|jjj d}|rd|j d|j \}}tjj|tjj|yy)zFDisable output capturing. Otherwise, stdout is lost to doctest (#985).DarwinNcapturemanagerT)in_) platformsystemrO pluginmanager getpluginsuspend_global_captureread_global_capturesysstdoutwritestderr)rncapmanrerrs rDrz0DoctestItem._disable_output_capturing_for_darwin1s~ ??  ( **445EF   ) )d ) 3113HC JJ  S ! JJ  S ! rFcddl}d}t|j|j|jzr|jg}n0t|jt r|jj }|t|!|Sg}|D]P}|j}|j}|j}|jd} n|j|jzdz} t|j} t|| | } t!} t#|j$j'd} | |jj(J|jj(j+d}|jJt-|Dcgc]\}}||jzdzdd|}}}|t/|jdz d|jdz}n=dg}d }|j0j+D]}|j3d |d|d }t||jr0|| j5||j6| j9d z }nnt;j<|j>}|d |jgz }|tAjB|j>Dcgc]}|jEd c}z }|j3| |fStG|Scc}}wcc}w)Nrrr4F03d z?EXAMPLE LOCATION UNKNOWN, not showing all tests of that examplez>>>z??? z... zUNEXPECTED EXCEPTION: )$rrrrrr|rr~ repr_failurerrfilenamelinenor+rxr _get_checker_get_report_choicerOrg docstring splitlines enumeratemaxsourcer6output_differencersplitr from_exc_infor tracebackformat_exceptionstriprk)rnexcinforrrmrrrrrmessagertr report_choiceruixindentrq inner_excinfors rDrzDoctestItem.repr_failure=s     MM711G4O4OO  H  '> ?}}--H  7'0 0) =GooG<>A;MNV#NN557#DLL4xq!78"F#'7#9#9:22W[[-%+!. ; ;G3->3 >3@FrFrc ddl}t|j|j|j|j |j |jttt S)Nr) DONT_ACCEPT_TRUE_FOR_1DONT_ACCEPT_BLANKLINENORMALIZE_WHITESPACEr*IGNORE_EXCEPTION_DETAILCOMPARISON_FLAGS ALLOW_UNICODE ALLOW_BYTESNUMBER) rrrrrr*rr_get_allow_unicode_flag_get_allow_bytes_flag_get_number_flagrs rD_get_flag_lookuprsZ &==%;;$99!! ' ? ? 11-/)+!  rFc^|jd}t}d}|D] }|||z} |S)Nr(r)getinir)rOoptionflags_strflag_lookup_tableflag_accflags rDget_optionflagsrsCmm$9:O(*H,%d++, OrFcR|jd}|r|jdrd}|S)Nr;usepdbF)getvalue)rOrs rD_get_continue_on_failurers/ &0M N ??8 $"'  rFceZdZdZddZy)rXNc# Kddl}|jjd}|jj |}t |j}|jj }ddi}t|j}td|tt|j}|j} | j||||d} | jr&tj|| j || yyw)Nrr-rx__main__Frrrrr]rr)rrOrrN read_textr?r]rrrr DocTestParser get_doctestexamplesrrV) rnrencodingtextrr]rhrrrBrs rDr.zDoctestTextfile.collects;;%%&89yy""8,tyy>yy~~Z(%dkk2 # N 8 E  '&&(!!$tXqA ==))499V4*  sDDrvzIterable[DoctestItem])rxryrzrr.rJrFrDrXrXs CrFrXcfddltfd|jD}|r tdyy)zVRaise pytest.skip() if all examples in the given DocTest have the SKIP option set.rNc3jK|]*}|jjjd,yw)FN)optionsgetSKIP)rbrrs rDrdz%_check_all_skipped..s$PQaiimmGLL%8Ps03z!all tests skipped by +SKIP option)rallr$r)r all_skippedrs @rDrrs-P$--PPK 01rFc t|ddduS)zmReturn if an object is possibly a mock object by checking the existence of a highly improbable attribute.1pytest_mock_example_attribute_that_shouldnt_existNr)rs rD _is_mockedr1s SMtT rFc#Ktjdd dfd}|t_ dt_y#t_wxYww)zContext manager which replaces ``inspect.unwrap`` with a version that's aware of mock objects and doesn't recurse into them.Nstopc ||turtS|fdS#t$r'}tjd|ddtd}~wwxYw)Nr3c.t|xsSrH)r1)r_stoprs rDzF_patch_unwrap_mock_aware.._mock_aware_unwrap..sjo6TtrFzGot z when unwrapping z. This is usually caused by a violation of Python's object protocol; see e.g. https://github.com/pytest-dev/pytest/issues/5080)r1 Exceptionwarningswarnr)rr4er7 real_unwraps` @rD_mock_aware_unwrapz4_patch_unwrap_mock_aware.._mock_aware_unwrapst |tz1"4j99Et*TU U  MMqe,TH5CC    s-- A"AA)rzCallable[..., Any]r4zCallable[[Any], Any] | Nonervr )inspectunwrap)r>r=s @rD_patch_unwrap_mock_awarerAsU..KJN +F "(GN% $s*A> A A  AceZdZddZy)rUc#rKddl}Gdd|j} |j}|jjj||}t|j }td|tt|j }|j!|j"D]4}|j$st&j)||j*||6y#tj$r9|j j drtd|jnY wxYww) NrceZdZejddZedkxsBedk(xrejj dkxs edk(xrejj dkZerfdZejdkr fd Z xZ SxZ S) 5DoctestModule.collect..MockAwareDocTestFinderN) r)rG rGct|tr t|d|}t|drt j |}t |||S)a%On older Pythons, doctest code does not take into account `@property`. https://github.com/python/cpython/issues/61648 Moreover, wrapped Doctests need to be unwrapped so the correct line number is returned. #8796 fget __wrapped__)rpropertygetattrhasattrr?r@r~ _find_lineno)rnr source_linesrs rDrPzBDoctestModule.collect..MockAwareDocTestFinder._find_linenosO"#x0%c637sM2%nnS1!7/$rF)rG cpt|tjr |j}t|||S)a>`cached_property` objects are never considered a part of the 'current module'. As such they are skipped by doctest. Here we override `_from_module` to check the underlying function instead. https://github.com/python/cpython/issues/107995 )r functoolscached_propertyrr~ _from_module)rnmoduleobjectrs rDrVzBDoctestModule.collect..MockAwareDocTestFinder._from_modules2 "&)*C*CD!'!7/??rF) rxryrzr version_infopy_ver_info_minormicrois_find_lineno_brokenrPrVrrs@rDMockAwareDocTestFinderrEs # 0 0! 4 !G+Q%0OS5E5E5K5Ka5OQ%0OS5E5E5K5Ka5O " %(') @ @*rFr]r:zunable to import module Frr )r DocTestFinderrr CollectErrorrOrrrNrerparsefactoriesrrrrfindrxr$rrVr])rnrr]rWfinderrrrs rDr.zDoctestModule.collects) @W%:%:) @V XXF $$33D9()%dkk2 # N 8 E  KK8 D}}!--tyyt. )%% {{##$BC/ }=>? s/D7 C(BD7(D7(AD40D73D44D7Nr')rxryrzr.rJrFrDrUrUsHrFrUc8ddl}Gdd|j}|S)NrceZdZejdej Zejdej ZejdejZ dfd Z ddZ xZ S)2_init_checker_class..LiteralsOutputCheckerz(\W|^)[uU]([rR]?[\'\"])z(\W|^)[bB]([rR]?[\'\"])a (?P (?P (?P [+-]?\d*)\.(?P\d+) | (?P [+-]?\d+)\. ) (?: [Ee] (?P [+-]?\d+) )? | (?P [+-]?\d+) (?: [Ee] (?P [+-]?\d+) ) ) cvt||||ry|tz}|tz}|t z}|s|s|sydd}|r&||j |}||j |}|r&||j |}||j |}|r|j||}t||||S)NTFc0tj|d|S)Nz\1\2)resub)regextxts rDremove_prefixeszX_init_checker_class..LiteralsOutputChecker.check_output..remove_prefixeslsvveWc22rF)rjzre.Pattern[str]rkr?rvr?)r~ check_outputr r r_unicode_literal_re_bytes_literal_re_remove_unwanted_precision) rnwantrr allow_unicode allow_bytes allow_numberrlrs rDrmz?_init_checker_class..LiteralsOutputChecker.check_outputasw#D#{;'*A*CCM%(=(??K&)9);;L \ 3&t'?'?F%d&>&>D&t'='=tD%d&<&.LiteralsOutputChecker._remove_unwanted_precision|se11$78E0056D5zSY& FE45 J1'(wwz':'(ww{';# ww{3H!)!1As8} 'X.I#veAGGI.>B N'SS 0aggi&01AGGI=AEEGfDTDV@WWaeeg 1QUUWqwwy5HIIF J JrF)rqr?rr?rrrvr)rqr?rr?rvr?) rxryrzrhcompileUNICODErnroVERBOSEr}rmrprrs@rDLiteralsOutputCheckerreCs^ )bjj)CRZZP&BJJ'A2::NRZZ & JJ) . @6 rFr)r OutputChecker)rrs rD_init_checker_classr@s"O 5 5Ob ! rFc6t tatS)aReturn a doctest.OutputChecker subclass that supports some additional options: * ALLOW_UNICODE and ALLOW_BYTES options to ignore u'' and b'' prefixes (respectively) in string literals. Useful when the same doctest should run in Python 2 and Python 3. * NUMBER to ignore floating-point differences smaller than the precision of the literal number in the doctest. An inner class is used to avoid importing "doctest" at the module level. )r&rrJrFrDrrs+- ?rFc.ddl}|jdS)z+Register and return the ALLOW_UNICODE flag.rNr rregister_optionflagrs rDr r s &7 & & 77rFc.ddl}|jdS)z)Register and return the ALLOW_BYTES flag.rNr rrs rDr r s &7 & &} 55rFc.ddl}|jdS)z$Register and return the NUMBER flag.rNr rrs rDrrs &7 & &x 00rFc ddl}t|jt|jt |j t|jtdi|S)zReturn the actual `doctest` module flag value. We want to do it as late as possible to avoid importing `doctest` and all its dependencies when parsing options, as it adds overhead and breaks tests. rN) rDOCTEST_REPORT_CHOICE_UDIFF REPORT_UDIFFDOCTEST_REPORT_CHOICE_CDIFF REPORT_CDIFFDOCTEST_REPORT_CHOICE_NDIFF REPORT_NDIFF(DOCTEST_REPORT_CHOICE_ONLY_FIRST_FAILUREREPORT_ONLY_FIRST_FAILUREDOCTEST_REPORT_CHOICE_NONE)keyrs rDrrsN  $W%9%9#W%9%9#W%9%90'2S2S"A      rFre)scopectS)aFixture that returns a :py:class:`dict` that will be injected into the namespace of doctests. Usually this fixture is used in conjunction with another ``autouse`` fixture: .. code-block:: python @pytest.fixture(autouse=True) def add_np(doctest_namespace): doctest_namespace["np"] = numpy For more details: :ref:`doctest_namespace`. )rrJrFrDrrs  6MrF)rBrrvrwr)rYr rZrrvz&DoctestModule | DoctestTextfile | None)rNr rvr)rOrrNr rZrrvr)rvztype[doctest.DocTestRunner]r) rrrrrrrrrvr)rvzdict[str, int])rOrrvr)rOrrvr)rrrvrw)rrXrvr)rvzGenerator[None])rvztype[doctest.OutputChecker])rvzdoctest.OutputChecker)rvr)rr?rvr)rvzdict[str, Any])_r __future__rrcollections.abcrrrr contextlibrrTr?ospathlibr rrhrrtypestypingr r r:_pytestr _pytest._code.coder rr _pytest._ior_pytest.compatr_pytest.configr_pytest.config.argparsingr_pytest.fixturesrr _pytest.nodesrr_pytest.outcomesrr_pytest.pathlibr_pytest.pythonr_pytest.python_apir_pytest.warning_typesrrtyping_extensionsr rrrrrrArIr&__annotations__rErKr[rSrWrTrkr9r|rrrrrrrXrr1rArUrrr r rrrrJrFrDrs:" $%$$%    ,/+&'!,$'#-!&!%/&#%%%+?(,  48 18/d    , A9& (l (!i! 2l-1 $  )    (CF$CFL  f>2%%:IFIXT!n(861 " yrF