o
     h1                     @  s   d dl mZ d dlZd dlZd dlm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 ejjG dd deZdS )    )annotationsN)Path   )TestBase   )config)eq_   )utilc                   @  s|   e Zd ZdZejdddd Zejdddd Zd	d
 Ze dd Z	ejdd Z
edd Zdd ZdddZdS )MypyTest)no_sqlalchemy2_stubsfunction)scopec                 c      |   E d H  d S N	_cachedirself r   S/var/www/html/venv/lib/python3.10/site-packages/sqlalchemy/testing/fixtures/mypy.pyper_func_cachedir      zMypyTest.per_func_cachedirclassc                 c  r   r   r   r   r   r   r   cachedir!   r   zMypyTest.cachedirc              	   c  s    d}t  P}tt|d d}|d| d W d    n1 s%w   Y  tt|d d}|d| d W d    n1 sGw   Y  |V  W d    d S 1 sZw   Y  d S )	N sqla_mypy_config.cfgwz
                    [mypy]

                    plugins = sqlalchemy.ext.mypy.plugin

                    show_error_codes = True

                    z
                    disable_error_code = no-untyped-call

                    [mypy-sqlalchemy.*]
                    ignore_errors = True

                    plain_mypy_config.cfgz^
                    [mypy]

                    show_error_codes = True

                    z
                    disable_error_code = var-annotated,no-untyped-call
                    [mypy-sqlalchemy.*]
                    ignore_errors = True

                    )tempfileTemporaryDirectoryopenr   write)r   	mypy_pathr   config_filer   r   r   r   %   s2   
"zMypyTest._cachedirc                   s    ddl m  d fdd	}|S )Nr   )apiFc           	   	     s   |d u r}ddd|dt j||rdndg}t j| }t j||}| |kr.t| | || t jdd   	|\}}}|||fS )Nz--strictz--raise-exceptionsz--cache-dirz--config-filer   r   MYPY_FORCE_COLOR)
ospathjoinbasenameshutilcopyfileappendenvironpoprun)	r(   
use_pluginuse_cachedirargsfilenametest_programstdoutstderrexitcoder%   r   r   r   r0   N   s,   

z!MypyTest.mypy_runner.<locals>.run)FN)mypyr%   )r   r   r0   r   r9   r   mypy_runnerJ   s   %zMypyTest.mypy_runnerc                   s   d fdd	}|S )NFc                   s2    | } | |d\}}}| |||| d S )N)r1   )_collect_messages_check_output)r(   r1   expected_messagesr6   r7   r8   r;   r   r   r   r0   w   s
   

z)MypyTest.mypy_typecheck_file.<locals>.run)Fr   )r   r;   r0   r   r?   r   mypy_typecheck_fileu   s   zMypyTest.mypy_typecheck_filec                 C  s   t j| r	| }nt d j}t jt j|| }tt	|
d}tjjD ]}|r>t j|r>|t	||  
d q(|S )Nr   z**/*.py)r'   r(   isabsinspectstackr4   r)   dirnamelistr   globr   optionsmypy_extra_test_pathsisdirextend)rD   r(   caller_pathfiles	extra_dirr   r   r   file_combinations   s   zMypyTest.file_combinationsc              
     s.  ddl m} g }td}td}t|}g }t|dD ]\ }||}	|	rJ|	dd\}
}}t	j
t|
t|fk rItd|	d  q||}	|	rt|	d}t|	d}t|	d	}t|	d
}tdd|	d}|rtddd |}tddd |}|r|stddd |}tddd |}tdd|}tddd |}d }}d| d}|rtjr|d t|rdndd d |dd   }|rtjrtd!d"d |}|||| f q|r| fd#d$|D  g |d d < qW d    |S 1 sw   Y  |S )%Nr   )mypy_14z0\s*# EXPECTED(_MYPY)?(_RE)?(_ROW)?(_TYPE)?: (.+)z%^#\s*PYTHON_VERSION\s?>=\s?(\d+\.\d+)r   .zRequires python >= %sr   r	      z# noqa[:]? ?.*r      zRow\[([^\]]+)\]c                 S  s   d|  d d|  d dS )Nztuple[r   z!, fallback=sqlalchemy.engine.row.r   ]groupmr   r   r   <lambda>   s    z,MypyTest._collect_messages.<locals>.<lambda>zOptional\[(.*)\]c                 S     d|  d dS )NzUnion[r   z, None]rT   rV   r   r   r   rX          z([\[\]])c                 S  s   d|  d S )N\r   rT   rV   r   r   r   rX          z([\w_]+)c                 S  rY   )Nz	(?:.*\.)?r   \*?rT   rV   r   r   r   rX      rZ   Listzbuiltins.listz\b(int|str|float|bool)\bc                 S  rY   )Nz	builtins.r   r]   rT   rV   r   r   r   rX      rZ   TzRevealed type is ""z\b(List|Tuple|Dict|Set)\bz\b(List|Tuple|Dict|Set|Type)\bc                 S  s   |  d S )Nr   )rU   lowerrV   r   r   r   rX      s    zOptional\[(.*?)\]c                 S  s   |  d dS )Nr   z | NonerT   rV   r   r   r   rX      r\   c                 3  s"    | ]\}}} |||fV  qd S r   r   ).0is_mypyis_reexpected_msgnumr   r   	<genexpr>   s    

z-MypyTest._collect_messages.<locals>.<genexpr>)sqlalchemy.ext.mypy.utilrO   recompiler!   	enumeratematchrU   	partitionsysversion_infointr   	skip_testboolsubr
   py39py310r-   striprJ   )r   r(   rO   r>   expected_re	py_ver_refile_current_assert_messageslinerW   major_minorrb   rc   is_rowis_typerd   r   re   r   r<      s   



	





oozMypyTest._collect_messagesr6   strc                 C  s  g }t j|}|rg }|d}	|	r[|	d}
td|
r&|d|
f n3td|
rC|	rB|	d}td|s@|	d| n|	s.ntd|
tj	rLntd|
rY|d	|
f |	s|D ]V\}}}}|
d
d}|smdnd}t|D ]6\}\}}|rtd| d| d| d| | 	|r nqs| d| d| d| | |
d
dv r nqs|| q]||= q]|rd|}td|dd |rddd |D }td|dd J d|rtt| d tddd |D  J dd S |dkrt||dd t|d|d d S )N
r   z.+\.py:\d+: error: .*errorz/.+\.py:\d+: note: +(?:Possible overload|def ).*z.+\.py:\d+: note: +def .*z*.+\.py:\d+: note: .*(?:perhaps|suggestion)z.+\.py:\d+: note: .*note'r_   z[SQLAlchemy Mypy plugin] r   z.*z\:z\: :z: z"Couldn't locate expected messages:)sepc                 s      | ]\}}|V  qd S r   r   ra   r}   msgr   r   r   rg   ?      z)MypyTest._check_output.<locals>.<genexpr>zRemaining messages:Fz'expected messages not found, see stdoutz& messages from mypy were not consumed:c                 s  r   r   r   r   r   r   r   rg   E  r   z&errors and/or notes remain, see stdout)r   )r'   r(   r*   splitr/   ri   rl   r-   insertIreplacerk   r)   printlenr   )r   r(   r>   r6   r7   r8   not_locatedr4   output	raw_lineseolrf   rb   rc   r   prefixidxtyperrmsgmissingextrar   r   r   r=     sx   






zMypyTest._check_outputN)r6   r   )__name__
__module____qualname____requires__r   fixturer   r   r   r;   r@   staticmethodrN   r<   r=   r   r   r   r   r      s    



%
*


yr   )
__future__r   rB   r'   pathlibr   ri   r+   rn   r   baser   r   r   
assertionsr   r
   add_to_markerr:   r   r   r   r   r   <module>   s   