o
    #`^hd                     @   sP  d dl Z d dlmZmZ d dlmZmZ d dlmZ d dlm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZ d dlZd dlmZ d dlmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1 d dl2m3Z3 d d	l4m5Z5m6Z6 d d
l7m8Z8m9Z9 d dl:m;Z; d dl<m=Z= d dl>m?Z?m@Z@ d dlAmBZB d dlCmDZDmEZE d dlFmGZG d dlHmIZI d dlJm3ZK d dlLmMZM d dlNmOZOmPZPmQZQmRZRmSZS d dlTmUZUmVZV d dlWmXZX d dlYmZZZ d dl[m\Z\m]Z]m^Z^ dZ_dZ`d~ddZadddebdejcd ebd!eeeb  de8f
d"d#Zddejcd ebde8fd$d%Zeddd&dejcd'e
d(e	f d ebd)eeb d!eeeb  de8fd*d+Zfeee
d(e	f  eebd(f f Zgd,dd-d.e8d/ehd0eeeg  de8fd1d2Zid3ee dee fd4d5Zjd.e8dee fd6d7Zkd8e
d(e	f de jlfd9d:Zmd;e	d<eebe	f de	fd=d>Znd8e
d(e	f de	fd?d@ZodddAdBd ebd8e
d(e	f d)eeb d!eeeb  dCehde8fdDdEZpdebdFe	d.e8deeh fdGdHZqeG dIdJ dJZrdebd;e	dKe	dLehderf
dMdNZsdOed.e8ddfdPdQZtd8e
d(e	f dehfdRdSZud8e
d(e	f dehfdTdUZvd8e
d(e	f dehfdVdWZwd8e
d(e	f dXedYeebe	f de	fdZd[ZxeG d\d] d]Zydddddd^d_eeVeZf d.e8d`eeeebe	f eOf  daeeK dbeeX dcee	 ddeeee
d(e	f eeb f e	f  deedfehdeyfdgdhZzdOedKe	dieebe	f djeebd(f dee	ee	 f f
dkdlZ{	ddOedieebe	f dmeebdf de	fdndoZ|d3ee dpeeebe	f eRePf deeebe	f ee	 f fdqdrZ}d3ee dehfdsdtZ~duee dveOdeebe	f fdwdxZduee dveeeebe	f eOf  dfehdeeebe	f eeebe	f  f fdydzZd{e8d)ebdfehdee fd|d}ZdS )    N)AsyncExitStackcontextmanager)copydeepcopy)	dataclass)AnyCallable	CoroutineDict
ForwardRefListMappingOptionalSequenceTupleTypeUnioncast)params)PYDANTIC_V2ErrorWrapper
ModelFieldRequired	Undefined_regenerate_error_with_loccopy_field_infocreate_body_modelevaluate_forwardreffield_annotation_is_scalarget_annotation_from_field_infoget_cached_model_fieldsget_missing_field_erroris_bytes_fieldis_bytes_sequence_fieldis_scalar_fieldis_scalar_sequence_fieldis_sequence_field.is_uploadfile_or_nonable_uploadfile_annotation!is_uploadfile_sequence_annotationlenient_issubclasssequence_typesserialize_sequence_valuevalue_is_sequence)BackgroundTasks)asynccontextmanagercontextmanager_in_threadpool)	DependantSecurityRequirement)logger)SecurityBase)OAuth2SecurityScopes)OpenIdConnect)create_model_fieldget_path_param_names)	BaseModel)	FieldInfo)run_in_threadpool)FormDataHeadersImmutableMultiDictQueryParams
UploadFile)HTTPConnectionRequest)Response)	WebSocket)	Annotatedget_args
get_originzForm data requires "python-multipart" to be installed. 
You can install "python-multipart" with: 

pip install python-multipart
zForm data requires "python-multipart" to be installed. It seems you installed "multipart" instead. 
You can remove "multipart" with: 

pip uninstall multipart

And then install "python-multipart" with: 

pip install python-multipart
returnc                  C   sx   z*ddl m}  | sJ zddlm} |sJ W W d S  ty*   tt ttd w  ty;   tt	 tt	d w )Nr   )__version__)parse_options_header)
	multipartrI   multipart.multipartrJ   ImportErrorr2   error!multipart_incorrect_install_errorRuntimeErrormultipart_not_installed_error)rI   rJ    rR   X/home/air/shanriGPT/back/venv/lib/python3.10/site-packages/fastapi/dependencies/utils.pyensure_multipart_is_installed[   s   



rT   )security_scopes
param_namedependspathrU   c                 C   s   |j sJ t||j || |dS )N)rW   
dependencyrX   namerU   )rY   get_sub_dependantrV   rW   rX   rU   rR   rR   rS   get_param_sub_dependantn   s   
r]   c                 C   s"   t | js	J dt| | j|dS )Nz;A parameter-less dependency must have a callable dependency)rW   rY   rX   )callablerY   r[   )rW   rX   rR   rR   rS   get_parameterless_sub_dependant   s   r_   )rZ   rU   rY   .rZ   c           	      C   s|   d }|pg }t | tjr| j}|| t |tr*g }t |ttfr$|}t||d}t	||||| j
d}|r<|j| |S )N)security_schemescopes)rX   callrZ   rU   	use_cache)
isinstancer   Securityra   extendr3   r4   r6   r1   get_dependantrc   security_requirementsappend)	rW   rY   rX   rZ   rU   security_requirementdependency_scopes
use_scopessub_dependantrR   rR   rS   r[      s,   

r[   Fskip_repeatsvisited	dependantro   rp   c             
   C   s   |d u rg }| | j t| j | j | j | j | j | j	 | j
| jd}| jD ];}|r9|j|v r9q/t|||d}|j|j |j|j |j|j |j|j |j|j |j	|j	 q/|S )N)path_paramsquery_paramsheader_paramscookie_paramsbody_paramsrh   rc   rX   rn   )ri   	cache_keyr0   rr   r   rs   rt   ru   rv   rh   rc   rX   dependenciesget_flat_dependantrf   )rq   ro   rp   flat_dependantrm   flat_subrR   rR   rS   ry      s4   

ry   fieldsc                 C   s:   | s| S | d }t | dkrt|jtrt|j}|S | S )Nr      )lenr)   type_r9   r    )r|   first_fieldfields_to_extractrR   rR   rS   _get_flat_fields_from_params   s   
r   c                 C   sD   t | dd}t|j}t|j}t|j}t|j}|| | | S )NT)ro   )ry   r   rr   rs   rt   ru   )rq   rz   rr   rs   rt   ru   rR   rR   rS   get_flat_params   s   



r   rb   c                    s<   t | }t| di   fdd|j D }t |}|S )N__globals__c              	      s,   g | ]}t j|j|j|jt|j d qS ))rZ   kinddefault
annotation)inspect	ParameterrZ   r   r   get_typed_annotationr   ).0paramglobalnsrR   rS   
<listcomp>   s    
z'get_typed_signature.<locals>.<listcomp>)r   	signaturegetattr
parametersvalues	Signature)rb   r   typed_paramstyped_signaturerR   r   rS   get_typed_signature   s   


	r   r   r   c                 C   s"   t | trt| } t| ||} | S N)rd   strr   r   )r   r   rR   rR   rS   r      s   
r   c                 C   s6   t | }|j}|t jju rd S t| di }t||S )Nr   )r   r   return_annotationr   emptyr   r   )rb   r   r   r   rR   rR   rS   get_typed_return_annotation   s   

r   T)rZ   rU   rc   rc   c                 C   s   t | }t|}|j}t||| ||d}| D ]Z\}	}
|	|v }t|	|
j|
j|d}|jd ur?t	|	|j| |d}|j
| qt|	|j|drT|jd u sSJ d|	q|jd us[J t|jjtjrk|j|j qt|j|d q|S )N)rb   rZ   rX   rU   rc   )rV   r   valueis_path_paramr\   rV   type_annotationrq   z0Cannot specify multiple FastAPI annotations for )fieldrq   )r8   r   r   r0   itemsanalyze_paramr   r   rW   r]   rx   ri   !add_non_field_param_to_dependencyr   r   rd   
field_infor   Bodyrv   add_param_to_fields)rX   rb   rZ   rU   rc   path_param_namesendpoint_signaturesignature_paramsrq   rV   r   r   param_detailsrm   rR   rR   rS   rg     sR   
rg   r   c                 C   s|   t |tr
| |_dS t |tr| |_dS t |tr| |_dS t |tr(| |_dS t |t	r2| |_
dS t |tr<| |_dS d S )NT)r)   rB   request_param_namerD   websocket_param_namerA   http_connection_param_namerC   response_param_nameStarletteBackgroundTasksbackground_tasks_param_namer5   security_scopes_param_namer   rR   rR   rS   r   5  s&   





r   c                   @   s0   e Zd ZU eed< eej ed< ee ed< dS )ParamDetailsr   rW   r   N)	__name__
__module____qualname__r   __annotations__r   r   Dependsr   rR   rR   rR   rS   r   M  s   
 r   r   r   c                 C   s  d }d }t }t }|tjjur|}|}t|tu ryt|}|d }dd |dd  D }	dd |	D }
|
r9|
d }nd }t|trqt	||d}|j
tu s]|j
tu s]J d|jj d	| d
|tjjurm|riJ d||_
nt|_
nt|tjry|}t|tjr|d u sJ d| |d u sJ d| |}nt|tr|d u sJ d| |}tr||_|d ur|jd u rt|}||_t|ttttttfr|d u sJ d||d u sJ d|nD|d u r%|d u r%|tjjur|nt}|rtj|d}n't|st|rtj||d}nt |dstj!||d}ntj"||d}d }|d ur|rCt|tjsBJ d|jj d| nt|tj#rXt$|dd d u rXtj%j&|_'t(||| }t|tj)rht*  |j+szt$|dd rz| ,dd}n|j+p| }||_+t-| ||j
||j
ttfv |d}|rt.|dsJ dn#t|tj"rt.|st/|st|j0t1rt$|dddksJ t2|||dS )Nr   c                 S   s    g | ]}t |ttjfr|qS rR   )rd   r:   r   r   r   argrR   rR   rS   r   f  s    z!analyze_param.<locals>.<listcomp>r}   c                 S   s&   g | ]}t |tjtjtjfr|qS rR   )rd   r   Paramr   r   r   rR   rR   rS   r   k  s    )r   r   `z1` default value cannot be set in `Annotated` for z). Set the default value with `=` instead.z*Path parameters cannot have default valueszGCannot specify `Depends` in `Annotated` and default value together for zaCannot specify a FastAPI annotation in `Annotated` and `Depends` as a default value together for zQCannot specify FastAPI annotations in `Annotated` and default value together for z"Cannot specify `Depends` for type z+Cannot specify FastAPI annotation for type )r   )r   r   zCannot use `z` for path param in_convert_underscores_-)rZ   r   r   aliasrequiredr   )r   z1Path params must be of one of the supported typesshape)r   rW   r   )3r   r   r   r   rG   rE   rF   rd   r:   r   r   r   r   	__class__r   r   r   r   r   rY   r   r)   rB   rD   rA   rC   r   r5   Pathr'   r(   Filer   r   Queryr   r   
ParamTypesqueryr   r   FormrT   r   replacer7   r$   r%   r   r9   r   )rV   r   r   r   r   rW   r   use_annotationannotated_argsfastapi_annotationsfastapi_specific_annotationsfastapi_annotationdefault_valuer   use_annotation_from_field_infor   rR   rR   rS   r   T  s  











r   r   c                 C   s   | j }t|dd }|tjjkr|j|  d S |tjjkr%|j|  d S |tjj	kr3|j
|  d S |tjjksAJ d| j |j|  d S )Nr   z>non-body parameters must be in path, query, header or cookie: )r   r   r   r   rX   rr   ri   r   rs   headerrt   cookierZ   ru   )r   rq   r   field_info_inrR   rR   rS   r     s   
r   c                 C   s8   t | r
t | S t | rdS t| dd }t |S )NF__call__)r   	isroutineiscoroutinefunctionisclassr   rb   dunder_callrR   rR   rS   is_coroutine_callable  s   



r   c                 C   $   t | rdS t| dd }t |S NTr   )r   isasyncgenfunctionr   r   rR   rR   rS   is_async_gen_callable     

r   c                 C   r   r   )r   isgeneratorfunctionr   r   rR   rR   rS   is_gen_callable  r   r   stack
sub_valuesc                    sL   t | rtt| di |}nt| rt| di |}||I d H S )NrR   )r   r/   r   r   r.   enter_async_context)rb   r   r   cmrR   rR   rS   solve_generator  s   r   c                   @   sb   e Zd ZU eeef ed< ee ed< ee	 ed< e
ed< eeedef ee f ef ed< dS )SolvedDependencyr   errorsbackground_tasksresponse.dependency_cacheN)r   r   r   r
   r   r   r   r   r   r   rC   r   r   rR   rR   rR   rS   r   )  s   
 (r   )bodyr   r   dependency_overrides_providerr   requestr   r   r   r   r   async_exit_stackembed_body_fieldsc        	            s  i }	g }
|d u rt  }|jd= d |_|pi }|jD ]}ttdtf |j|_tttdtf tt	 f |j
|_
|j}|}|r\|jr\|j}t|di ||}|j}t|||j|jd}t| ||||||||d	I d H }|j}||j |jr|
|j q|jr|j
|v r||j
 }n0t|st|rt|||jdI d H }nt|r|d
i |jI d H }nt|fi |jI d H }|jd ur||	|j< |j
|vr|||j
< qt|j | j \}}t|j!| j!\}}t|j"| j\}}t|j#| j$\}}|	| |	| |	| |	| |
|| | | 7 }
|j%r/t&|j%||dI d H \}}|	| |
| |j'r8| |	|j'< |j(rHt)| t*rH| |	|j(< n|j+rWt)| t,rW| |	|j+< |j-rh|d u rct. }||	|j-< |j/rq||	|j/< |j0r~t1|jd|	|j0< t2|	|
|||d	S )Nzcontent-length.dependency_overrides)rX   rb   rZ   rU   )	r   rq   r   r   r   r   r   r   r   )rb   r   r   )body_fieldsreceived_bodyr   )ra   )r   r   r   r   r   rR   )3rC   headersstatus_coderx   r   r   r   rb   r   r   rw   r   r   getrX   rg   rZ   rU   solve_dependenciesr   updater   r   rf   rc   r   r   r   r   r   r;   request_params_to_argsrr   rs   rt   ru   cookiesrv   request_body_to_argsr   r   rd   rB   r   rD   r   r-   r   r   r5   r   )r   rq   r   r   r   r   r   r   r   r   r   rm   rb   use_sub_dependantoriginal_calluse_pathsolved_resultsolvedpath_valuespath_errorsquery_valuesquery_errorsheader_valuesheader_errorscookie_valuescookie_errorsbody_valuesbody_errorsrR   rR   rS   r   2  s   
















r   r   locc                 C   sz   |d u r| j rd t|dgfS t| jg fS | j|||d\}}t|tr*d |gfS t|tr9t|dd}d |fS |g fS )N)r  rR   )r   
loc_prefix)	r   r!   r   r   validaterd   r   listr   )r   r   r   r  v_errors_
new_errorsrR   rR   rS    _validate_value_with_model_field  s   


r  r   c                 C   s   |p| j }t| rt|ttfr||}n||d }|d u s:t| jtj	r0t|t
r0|dks:t| rDt|dkrD| jr?d S t| jS |S )N r   )r   r&   rd   r>   r=   getlistr   r   r   r   r   r~   r   r   r   )r   r   r   r   rR   rR   rS   _get_multidict_value  s"   

r  received_paramsc                 C   s  i }g }| s
||fS | d }| }d}t | dkr%t|jtr%t|j}d}i }t }|D ]@}	d }
t|trNt|	j	dd}|rN|	j
|	jkrG|	j
n|	jdd}
t|	||
d}|d ur^|||	j< ||
pd|	j
 ||	j q,| D ]\}}||vr}|||< qq|r|j	}t|tjsJ d	|jjf}t||||d
\}}|j|i|fS | D ]2}	t|	|}|	j	}t|tjsJ d	|jj|	j
f}t|	|||d
\}}|r|| q|||	j< q||fS )Nr   Fr}   Tr   r   r   )r   z"Params must be subclasses of Paramr   r   r   r  )r~   r)   r   r9   r    setrd   r=   r   r   r   rZ   r   r  addr   r   r   r   r   r  rf   )r|   r  r   r   r   r   single_not_embedded_fieldparams_to_processprocessed_keysr   r   r   r   keyr   r  r  r  rR   rR   rS   r     st   






r   c                 C   sb   | sdS dd | D }t |dkrdS | d }t|jdd r dS t|jtjr/t|jts/dS dS )NFc                 S   s   h | ]}|j qS rR   )rZ   )r   r   rR   rR   rS   	<setcomp>&  s    z,_should_embed_body_fields.<locals>.<setcomp>r}   Tr   embed)	r~   r   r   rd   r   r   r)   r   r9   )r|   body_param_names_setr   rR   rR   rS   _should_embed_body_fields!  s   r$  r   r   c              
      sD  i }| d }|j }| D ]}t||}t|tjr*t|r*t|tr*| I d H }n[t|rt|tjrt	|rt|t
s?J g  dtg ttttf f dd f fdd}t 4 I d H }|D ]	}	|||	j q`W d   I d H  n1 I d H szw   Y  t| d}|d ur|||j< q| D ]\}
}|
|vr|||
< q|S )Nr   fnrH   c                    s   |  I d H }  | d S r   )ri   )r%  resultresultsrR   rS   
process_fnP  s   z&_extract_form_body.<locals>.process_fn)r   r   )r   r  rd   r   r   r"   r@   readr#   r,   r*   r   r	   r   anyiocreate_task_group
start_soonr+   r   r   )r   r   r   r   first_field_infor   r   r)  tg	sub_valuer   rR   r'  rS   _extract_form_body7  sP   


(
r1  c              	      s  i }g }| sJ dt | dko| }| d }|}| }|r)t|jtr)t|j}t|tr6t||I d H }|rKd}	t||||	d\}
}|j	|
i|fS | D ]=}d|j
f}	d }|d ursz||j
}W n tyr   |t|	 Y qMw t||||	d\}
}|r|| qM|
||j	< qM||fS )Nz3request_body_to_args() should be called with fieldsr}   r   )r   r  r   )r~   r)   r   r9   r    rd   r<   r1  r  rZ   r   r   AttributeErrorri   r!   rf   )r   r   r   r   r   r  r   body_to_processr   r  r  r  r   r   rR   rR   rS   r   b  sF   




r   rz   c              	   C   s   | j sdS | j d }|s|S d| }t| j |d}tdd | j D }|dd}|s.d|d	< td
d | j D r<tj}n'tdd | j D rJtj}ntj}dd | j D }	tt|	dkrc|	d |d< t	d||d|di |d}
|
S )a  
    Get a ModelField representing the request body for a path operation, combining
    all body parameters into a single field if necessary.

    Used to check if it's form data (with `isinstance(body_field, params.Form)`)
    or JSON and to generate the JSON Schema for a request body.

    This is **not** used to validate/parse the request body, that's done with each
    individual body parameter.
    Nr   Body_)r|   
model_namec                 s   s    | ]}|j rd V  qdS )TN)r   r   frR   rR   rS   	<genexpr>  s    z!get_body_field.<locals>.<genexpr>r   )r   r   r   c                 s       | ]
}t |jtjV  qd S r   )rd   r   r   r   r6  rR   rR   rS   r8        c                 s   r9  r   )rd   r   r   r   r6  rR   rR   rS   r8    r:  c                 S   s"   g | ]}t |jtjr|jjqS rR   )rd   r   r   r   
media_typer6  rR   rR   rS   r     s    z"get_body_field.<locals>.<listcomp>r}   r;  )rZ   r   r   r   r   rR   )
rv   r   anyr   r   r   r   r~   r  r7   )rz   rZ   r   first_paramr5  	BodyModelr   BodyFieldInfo_kwargsBodyFieldInfobody_param_media_typesfinal_fieldrR   rR   rS   get_body_field  sB   
rC  )rH   Nr   )r   
contextlibr   r   r   r   dataclassesr   typingr   r   r	   r
   r   r   r   r   r   r   r   r   r   r+  fastapir   fastapi._compatr   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   fastapi.backgroundr-   fastapi.concurrencyr.   r/   fastapi.dependencies.modelsr0   r1   fastapi.loggerr2   fastapi.security.baser3   fastapi.security.oauth2r4   r5   $fastapi.security.open_id_connect_urlr6   fastapi.utilsr7   r8   pydanticr9   pydantic.fieldsr:   starlette.backgroundr   starlette.concurrencyr;   starlette.datastructuresr<   r=   r>   r?   r@   starlette.requestsrA   rB   starlette.responsesrC   starlette.websocketsrD   typing_extensionsrE   rF   rG   rQ   rO   rT   r   r   r]   r_   r[   CacheKeyboolry   r   r   r   r   r   r   rg   r   r   r   r   r   r   r   r   r   r   r  r  r   r$  r1  r   rC  rR   rR   rR   rS   <module>   s   <h






  

#
	


4

 %	




"	

~






G

+
.