o
    v&ib                     @   s  U d dl Z d dlZd dlZd dl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ZddlmZ ddlmZ edZejjZeejjejjejj f Z!G dd de"Z#eG dd dZ$eG dd dZ%G dd dZ&dedee&ddf fddZ'e( e'Z)ej(dd e'Z*ej(d!d e'Z+ej(d"d e'Z,ej(d#d e'Z-g Z.e/e e0d$< i Z1e2e3ef e0d%< d&ed'ef d(ed)eddfd*d+Z4d&ed'ef d(ed)eddfd,d-Z5d(ed)eddfd.d/Z6d(ed)eddfd0d1Z7d(ed)eddfd2d3Z8d(ed)eddfd4d5Z9d(ed)eddfd6d7Z:d(ed)eddfd8d9Z;d(ed)eddfd:d;Z<d(ed)eddfd<d=Z=d(ed)eddfd>d?Z>d(ed)eddfd@dAZ?d(ed)eddfdBdCZ@d(ed)eddfdDdEZAd(ed)eddfdFdGZBd(ed)eddfdHdIZCdJeddfdKdLZDdTdMdNZEdOeddfdPdQZFdJeddfdRdSZGdS )U    N)	Generator)Iterable)Iterator)Mapping)	dataclass)field)Any)Callable)Optional)TypeVar)Union)cast)overload   )get_mock_module)parse_ini_boolean_Tc                   @   s   e Zd ZdZdS )PytestMockWarningz3Base class for all warnings emitted by pytest-mock.N)__name__
__module____qualname____doc__ r   r   X/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/pytest_mock/plugin.pyr   $   s    r   c                   @   s&   e Zd ZU eed< dZee ed< dS )MockCacheItemmockNpatch)r   r   r   MockType__annotations__r   r
   r   r   r   r   r   r   (   s   
 r   c                   @   s~   e Zd ZU dZeedZee ed< de	defddZ
de	dedefd	d
Zde	ddfddZdddZdee fddZdS )	MockCachezJ
    Cache MagicMock and Patcher instances so we can undo them later.
    )default_factorycacher   returnc                 C   s&   | j D ]}|j|u r|  S qtd)Nz"This mock object is not registered)r!   r   
ValueErrorselfr   	mock_itemr   r   r   _find6   s
   

zMockCache._findkwargsc                 K   s$   | j tdd|i| | j d S )Nr   r   )r!   appendr   )r%   r   r(   r   r   r   add<   s   
zMockCache.addNc                 C   s*   |  |}|jr|j  | j| d S N)r'   r   stopr!   remover$   r   r   r   r.   @   s   

zMockCache.removec                 C   s2   t | jD ]}|jd ur|j  q| j  d S r,   )reversedr!   r   r-   clear)r%   r&   r   r   r   r0   F   s
   

zMockCache.clearc                 C   s
   t | jS r,   )iterr!   r%   r   r   r   __iter__L   s   
zMockCache.__iter__r"   N)r   r   r   r   r   listr!   r   r   r   r'   r   r+   r.   r0   r   r3   r   r   r   r   r   .   s   
 
r   c                   @   s   e Zd ZdZdeddfddZ	d#ded	ed
ededef
ddZddddededdfddZ	d$ddZ
dejjddfddZ	d%dedededefddZd&dee dejjfddZd&dee defdd ZG d!d" d"ZdS )'MockerFixturez
    Fixture that provides the same interface to functions in the mock module,
    ensuring that they are uninstalled at the end of each test.
    configr"   Nc                 C   s   t  | _t| | _}| | j|| _|j| _|j| _|j| _|j	| _	|j
| _
t|dr0|j| _|j| _|j| _|j| _|j| _|j| _t|drO|j| _d S d S )N	AsyncMockseal)r   _mock_cacher   mock_module_Patcherr   Mock	MagicMockNonCallableMockNonCallableMagicMockPropertyMockhasattrr8   callANYDEFAULTsentinel	mock_openr9   )r%   r7   r;   r   r   r   __init__V   s$   

zMockerFixture.__init__Fspecspec_setinstancer(   c                 K   s(   | j j|||fi |}| j| |S r,   )r;   create_autospecr:   r+   )r%   rI   rJ   rK   r(   mr   r   r   rL   j   s   zMockerFixture.create_autospecreturn_valueside_effectrO   rP   c                C   s   t | dr| j| jf}n| jf}| jD ]1}t |jdsqt |jdr&g |j_t |jdr0d|j_t|j|r?|jj||d q|j  qdS )z
        Call reset_mock() on all patchers started by this fixture.

        :param bool return_value: Reset the return_value of mocks.
        :param bool side_effect: Reset the side_effect of mocks.
        r8   
reset_mockspy_return_listspy_return_iterNrN   )	rB   r=   r8   r:   r   rR   rS   
isinstancerQ   )r%   rO   rP   supports_reset_mock_with_argsr&   r   r   r   resetalls   s    


zMockerFixture.resetallc                 C   s   | j   dS )zi
        Stop all patchers started by this fixture. Can be safely called multiple
        times.
        N)r:   r0   r2   r   r   r   stopall   s   zMockerFixture.stopallr   c                 C   s   | j | dS )zp
        Stops a previous patch or spy call by passing the ``MagicMock`` object
        returned by it.
        N)r:   r.   )r%   r   r   r   r   r-      s   zMockerFixture.stopobjnameduplicate_iteratorsc                    s   t || fdd}fdd}tr t|}nt|}tp/t}| jj||||dd_	d_
g _d_S )ak  
        Create a spy of method. It will run method normally, but it is now
        possible to use `mock` call features with it, like call count.

        :param obj: An object.
        :param name: A method in object.
        :param duplicate_iterators: Whether to keep a copy of the returned iterator in `spy_return_iter`.
        :return: Spy object.
        c               
      s   d _ d _z	| i |}W n ty } z|_ d }~ww  r3t|tr3t|d\}}|_nd _|_ j	| |S )N   )

spy_returnspy_exceptionBaseExceptionrT   r   	itertoolsteerS   rR   r*   )argsr(   reduplicated_iteratorrZ   methodspy_objr   r   wrapper   s    z"MockerFixture.spy.<locals>.wrapperc               
      s^   d _ d _z | i |I d H }W n ty# } z|_ d }~ww |_ j| |S r,   )r\   r]   r^   rR   r*   )ra   r(   rb   rc   )rf   rg   r   r   async_wrapper   s   z(MockerFixture.spy.<locals>.async_wrapper)rP   autospecN)getattrinspectiscoroutinefunction	functoolsupdate_wrapperismethod
isfunctionr   objectr\   rS   rR   r]   )r%   rX   rY   rZ   rh   ri   wrappedrj   r   re   r   spy   s   

zMockerFixture.spyc                 C   s   t tjj| jjdd |dS )z
        Create a stub method. It accepts any arguments. Ideal to register to
        callbacks in tests.

        :param name: the constructed stub's name as used in repr
        :return: Stub object.
        c                  _      d S r,   r   ra   r(   r   r   r   <lambda>       z$MockerFixture.stub.<locals>.<lambda>rI   rY   )r   unittestr   r>   r;   r%   rY   r   r   r   stub   s   zMockerFixture.stubc                 C   s   t t| jjdd |dS )z
        Create a async stub method. It accepts any arguments. Ideal to register to
        callbacks in tests.

        :param name: the constructed stub's name as used in repr
        :return: Stub object.
        c                  _   ru   r,   r   rv   r   r   r   rw      rx   z*MockerFixture.async_stub.<locals>.<lambda>ry   )r   AsyncMockTyper;   r8   r{   r   r   r   
async_stub   s   zMockerFixture.async_stubc                   @   s*  e Zd ZdZe Zdd Zdedededede	f
d	d
Z
edddddfdedededee dedee dee dedede	fddZedddddfdejdedejdeej dedeej deej dejdede	fddZ					d(dejdeej dedeej deej deej dedeee	f fddZ		d)deeeef ef deeeef eeeef  f dededef
dd Ze	!	!	!	!	!	!d*dedddeej dedeej deej dddede	fd"d#Ze	!	!	!	!	!d+dededeej dedeej deej dddedefd$d#Zededddeej dedeej deej deg ef dedefd%d#Ze	!	!	!	!	!d+dedddeej dedeej deej deg ef dedefd&d#Zedddddfdedejdeej dedeej deej deeg ef  dedefd'd#ZdS ),zMockerFixture._Patcherz
        Object to provide the same interface as mock.patch, mock.patch.object,
        etc. We need this indirection to keep the same API of the mock package.
        c                 C   s   || _ || _d S r,   )_Patcher__mock_cacher;   )r%   
mock_cacher;   r   r   r   rH      s   
zMockerFixture._Patcher.__init__	mock_funcwarn_on_mock_enterra   r(   r"   c                 O   sN   ||i |}|  }| jj||d t|dr%t|dr%|r%dd |j_|S )zPatches something by calling the given function from the mock
            module, registering the patch to stop it later and returns the
            mock object resulting from the mock call.
            )r   r   rQ   	__enter__c                   S   s   t jdtddS )Na.  Mocks returned by pytest-mock do not need to be used as context managers. The mocker fixture automatically undoes mocking at the end of a test. This warning can be ignored if it was triggered by mocking a context manager. https://pytest-mock.readthedocs.io/en/latest/usage.html#usage-as-context-manager   )
stacklevel)warningswarnr   r   r   r   r   rw     s
    z5MockerFixture._Patcher._start_patch.<locals>.<lambda>)startr   r+   rB   r   rP   )r%   r   r   ra   r(   pmockedr   r   r   _start_patch  s   
z#MockerFixture._Patcher._start_patchNFtarget	attributenewrI   createrJ   rj   new_callablec	           
   	   K   >   || j u r	| jj }| j| jjjd||f||||||d|	S )zAPI to mock.patch.objectTr   rI   r   rJ   rj   r   rE   r;   r   r   rr   
r%   r   r   r   rI   r   rJ   rj   r   r(   r   r   r   rr     s"   
zMockerFixture._Patcher.objectc	           
   	   K   r   )zThis is equivalent to mock.patch.object except that the returned mock
            does not issue a warning when used as a context manager.Fr   r   r   r   r   r   context_manager7  s"   
z&MockerFixture._Patcher.context_managerc                 K   s(   | j | jjjd|f|||||d|S )zAPI to mock.patch.multipleT)rI   r   rJ   rj   r   )r   r;   r   multiple)r%   r   rI   r   rJ   rj   r   r(   r   r   r   r   U  s   	zMockerFixture._Patcher.multipler   in_dictvaluesr0   c                 K   s"   | j | jjjd|f||d|S )zAPI to mock.patch.dictT)r   r0   )r   r;   r   dict)r%   r   r   r0   r(   r   r   r   r   l  s   zMockerFixture._Patcher.dict.c           	      K   ru   r,   r   	r%   r   r   rI   r   rJ   rj   r   r(   r   r   r   __call__}     zMockerFixture._Patcher.__call__c           	      K   ru   r,   r   r   r   r   r   r     r   c           	      K   ru   r,   r   r   r   r   r   r     r   c          	      K   ru   r,   r   r   r   r   r   r     s   c           	   	   K   s:   || j u r	| jj }| j| jjd|f||||||d|S )zAPI to mock.patchTr   )rE   r;   r   r   r   r   r   r   r     s    

)NFNNN)r   F)......).....)r   r   r   r   rr   rE   rH   r   boolr   r   strr
   builtinsr   r   r   r   r   r   tupler   r   r   r	   r   r   r   r   r<      s   
	

!	

!
	

	
	

	

	
	
r<   )FFr4   )Fr,   )r   r   r   r   r   rH   r   r   rL   rV   rW   rz   r   r>   r-   rr   r   rt   r
   r|   r}   r~   r<   r   r   r   r   r6   P   sL    




<r6   pytestconfigr"   c                 c   s    t | }|V  |  dS )z
    Return an object that has the same interface to the `mock` module, but
    takes care of automatically undoing all patches after each test method.
    N)r6   rW   )r   resultr   r   r   _mocker  s   r   class)scopemodulepackagesession_mock_module_patches_mock_module_originals__wrapped_mock_method__.ra   r(   c                 O   s&  d}z
| |i | W d S  t y } zzt|ddrt|}nf|d }t|}|jd ur|j\}}d}	z||dd  ks>J W n t yY }
 z|	dt|
 7 }	W Y d }
~
nd }
~
ww z||ksaJ W n t y| } z|	dt| 7 }	W Y d }~nd }~ww |	r|d|	 7 }t |}d|_|d }~ww )	NT_mock_introspection_appliedr    r   
Args:
	
Kwargs:
 

pytest introspection follows:
)AssertionErrorrk   r   	call_argsr   )r   ra   r(   __tracebackhide__rc   msg__mock_selfactual_argsactual_kwargsintrospectione_argse_kwargsr   r   r   assert_wrapper  s>   


r   c                 O   s  d}z
| |i | W d S  t y } z|dd}t|dds"|r't|}n|d }t|}|jd urt|j}|d }	d}
ddlm} |||	D ]f\}}|d urW|\}}nt }i }|d urf|\}}}nt }i }z||ksrJ W n t y } z|
d	t| 7 }
W Y d }~nd }~ww z||ksJ W qJ t y } z|
d
t| 7 }
W Y d }~qJd }~ww |
r|d|
 7 }t |}d|_	|d }~ww )NT	any_orderFr   r   r   r   )zip_longestr   r   r   )
r   getrk   r   call_args_listr5   r_   r   r   r   )r   ra   r(   r   rc   r   r   r   actual_callsexpect_callsr   r   actual_callexpect_callr   r   _expect_argsexpect_kwargsr   r   r   r   r   assert_has_calls_wrapper  sV   



r   c                  O   "   d}t td g| R i | d S )NTassert_not_calledr   r   ra   r(   r   r   r   r   wrap_assert_not_called2     r   c                  O   r   )NTassert_called_withr   r   r   r   r   wrap_assert_called_with7  r   r   c                  O   r   )NTassert_called_oncer   r   r   r   r   wrap_assert_called_once<  r   r   c                  O   r   )NTassert_called_once_withr   r   r   r   r   wrap_assert_called_once_withA  r   r   c                  O   r   )NTassert_has_calls)r   r   r   r   r   r   wrap_assert_has_callsF  s   
r   c                  O   r   )NTassert_any_callr   r   r   r   r   wrap_assert_any_callM  r   r   c                  O   r   )NTassert_calledr   r   r   r   r   wrap_assert_calledR  r   r   c                  O   r   )NTassert_not_awaitedr   r   r   r   r   wrap_assert_not_awaitedW  r   r   c                  O   r   )NTassert_awaited_withr   r   r   r   r   wrap_assert_awaited_with\  r   r   c                  O   r   )NTassert_awaited_oncer   r   r   r   r   wrap_assert_awaited_oncea  r   r   c                  O   r   )NTassert_awaited_once_withr   r   r   r   r   wrap_assert_awaited_once_withf  r   r   c                  O   r   )NTassert_has_awaitsr   r   r   r   r   wrap_assert_has_awaitsk  r   r   c                  O   r   )NTassert_any_awaitr   r   r   r   r   wrap_assert_any_awaitp  r   r   c                  O   r   )NTassert_awaitedr   r   r   r   r   wrap_assert_awaitedu  r   r   r7   c              	   C   s  t rdS t| }tttttttd}|	 D ],\}}zt
|j|}W n	 ty+   Y qw |t |< |j|j||}|  t| qt|drtttttttd}|	 D ],\}}zt
|j|}W n	 tyk   Y qVw |t |< |j|j||}|  t| qV| t dS )z
    Wrap assert methods of mock module so we can hide their traceback and
    add introspection information to specified argument asserts.
    N)r   r   r   r   r   r   r   r8   )r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   itemsrk   r?   AttributeErrorr   rr   r   r   r*   rB   r   r   r   r   r   r   r   r8   add_cleanupunwrap_assert_methods)r7   r;   wrappersrf   rh   originalpatcherasync_wrappersr   r   r   wrap_assert_methodsz  sR   	
	r   c                  C   sb   t D ]"} z|   W q ty$ } zt|dkrn W Y d }~qd }~ww g t d d < t  d S )Nz stop called on unstarted patcher)r   r-   RuntimeErrorr   r   r0   )r   rc   r   r   r   r     s   	r   parserc                 C   s$   | j dddd | j dddd d S )Nmock_traceback_monkeypatchzRMonkeypatch the mock library to improve reporting of the assert_called_... methodsTdefaultmock_use_standalone_modulezPUse standalone "mock" (from PyPI) instead of builtin "unittest.mock" on Python 3F)addini)r   r   r   r   pytest_addoption  s   
r   c                 C   s8   | j ddd}t| dr|dkrt|  d S d S d S )Nz--tbautor   r   native)	getoptionr   getinir   )r7   tbr   r   r   pytest_configure  s   r   r4   )Hr   rn   rl   r_   unittest.mockrz   r   collections.abcr   r   r   r   dataclassesr   r   typingr   r	   r
   r   r   r   r   pytest_utilr   r   r   r   r8   r}   r>   r@   r   UserWarningr   r   r   r6   r   fixturemockerclass_mockermodule_mockerpackage_mockersession_mockerr   r5   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s   
 !   




0
5