o
    3Ih=                     @   s   d dl mZ d dlmZ d dlmZmZ 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mZmZmZ G dd deZdS )    )contextmanager)BytesIO)TestCasemockN)FormatChecker
exceptions	protocols
validatorsc                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/S )0TestDeprecationsc                 C   sb   d}|  t|}ddlm} W d   n1 sw   Y  | |tjd | |jt	 dS )zY
        As of v4.0.0, __version__ is deprecated in favor of importlib.metadata.
        z.Accessing jsonschema.__version__ is deprecatedr   )__version__N
jsonschema)
assertWarnsRegexDeprecationWarningr   r   assertEqual	importlibmetadataversionfilename__file__)selfmessagewr    r   b/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/jsonschema/tests/test_deprecations.pytest_version   s   zTestDeprecations.test_versionc                 C   \   d}|  t|}ddlm} W d   n1 sw   Y  | |tj | |jt dS )z
        As of v4.0.0, importing ErrorTree from jsonschema.validators is
        deprecated in favor of doing so from jsonschema.exceptions.
        z2Importing ErrorTree from jsonschema.validators is r   	ErrorTreeN)r   r   jsonschema.validatorsr   r   r   r   r   r   r   r   r   r   r   r   test_validators_ErrorTree      z*TestDeprecations.test_validators_ErrorTreec                 C   r   )z
        As of v4.18.0, importing ErrorTree from the package root is
        deprecated in favor of doing so from jsonschema.exceptions.
        z9Importing ErrorTree directly from the jsonschema package r   r   N)r   r   r   r   r   r   r   r   r   r   r   r   test_import_ErrorTree)   r!   z&TestDeprecations.test_import_ErrorTreec                 C   s   t jddgd}t  }t j|gd}d}| t|}||d< W d   n1 s*w   Y  | |d | | |jt dS )zM
        As of v4.20.0, setting items on an ErrorTree is deprecated.
        z
some errorfoo)path)errorszErrorTree.__setitem__ is N)r   ValidationErrorr   r   r   r   r   r   )r   etreesubtreer   r   r   r   r   test_ErrorTree_setitem6   s   
z'TestDeprecations.test_ErrorTree_setitemc                 C   r   )z
        As of v4.18.0, importing FormatError from the package root is
        deprecated in favor of doing so from jsonschema.exceptions.
        z;Importing FormatError directly from the jsonschema package r   )FormatErrorN)r   r   r   r+   r   r   r   r   )r   r   r   r+   r   r   r   test_import_FormatErrorF   r!   z(TestDeprecations.test_import_FormatErrorc                 C   r   )z
        As of v4.19.0, importing Validator from the package root is
        deprecated in favor of doing so from jsonschema.protocols.
        z9Importing Validator directly from the jsonschema package r   )	ValidatorN)r   r   r   r-   r   r   r   r   )r   r   r   r-   r   r   r   test_import_ValidatorS   r!   z&TestDeprecations.test_import_Validatorc                 C   sV   d}|  t|}tj}W d   n1 sw   Y  | |tj | |jt dS )za
        As of v4.0.0, accessing jsonschema.validators.validators is
        deprecated.
        z8Accessing jsonschema.validators.validators is deprecatedN)r   r   r	   r   _VALIDATORSr   r   r   r   r   valuer   r   r   test_validators_validators`      z+TestDeprecations.test_validators_validatorsc                 C   sV   d}|  t|}tj}W d   n1 sw   Y  | |tj | |jt dS )zc
        As of v4.0.0, accessing jsonschema.validators.meta_schemas is
        deprecated.
        z:Accessing jsonschema.validators.meta_schemas is deprecatedN)r   r   r	   meta_schemasr   _META_SCHEMASr   r   r0   r   r   r   test_validators_meta_schemasm   r3   z-TestDeprecations.test_validators_meta_schemasc              	   C   sx   t ji }d}| t|}|d W d   n1 sw   Y  W d   n1 s.w   Y  | |jt dS )zC
        As of v4.0.0, RefResolver.in_scope is deprecated.
        z.jsonschema.RefResolver.in_scope is deprecated r#   N)	r	   _RefResolverfrom_schemar   r   in_scoper   r   r   )r   resolverr   r   r   r   r   test_RefResolver_in_scopez   s   z*TestDeprecations.test_RefResolver_in_scopec                 C   sf   t i }d}| t|}|dddi}W d   n1 s w   Y  | | | |jt dS )zz
        As of v4.0.0, calling is_valid with two arguments (to provide a
        different schema) is deprecated.
        z5Passing a schema to Validator.is_valid is deprecated r#   typenumberN)	r	   Draft7Validatorr   r   is_validassertFalser   r   r   )r   	validatorr   r   resultr   r   r   %test_Validator_is_valid_two_arguments   s   

z6TestDeprecations.test_Validator_is_valid_two_argumentsc                 C   sl   t i }d}| t|}|dddi\}W d   n1 s!w   Y  | |jd | |jt dS )z}
        As of v4.0.0, calling iter_errors with two arguments (to provide a
        different schema) is deprecated.
        z8Passing a schema to Validator.iter_errors is deprecated r#   r<   r=   N)	r	   r>   r   r   iter_errorsr   rA   r   r   )r   rA   r   r   errorr   r   r   (test_Validator_iter_errors_two_arguments   s   
z9TestDeprecations.test_Validator_iter_errors_two_argumentsc                 C   s\   t i }d}| t|}| |jt j W d   n1 s w   Y  | |jt	 dS )zL
        As of v4.18.0, accessing Validator.resolver is deprecated.
        z&Accessing Draft7Validator.resolver is N)
r	   r>   r   r   assertIsInstancer:   r7   r   r   r   )r   rA   r   r   r   r   r   test_Validator_resolver   s   
z(TestDeprecations.test_Validator_resolverc                 C   s   d}|  t|}ddlm} W d   n1 sw   Y  | |jt |  t|}ddlm} W d   n1 s<w   Y  | |jt dS )zA
        As of v4.18.0, RefResolver is fully deprecated.
        z$jsonschema.RefResolver is deprecatedr   )RefResolverN)r   r   r   rI   r   r   r   r   )r   r   r   rI   r   r   r   test_RefResolver   s   z!TestDeprecations.test_RefResolverc                 C   s   d}|  t|}ddlm} W d   n1 sw   Y  | |tj | |jt |  t|}ddl	m} W d   n1 sCw   Y  | |tj | |jt dS )z
        As of v4.18.0, RefResolutionError is deprecated in favor of directly
        catching errors from the referencing library.
        z6jsonschema.exceptions.RefResolutionError is deprecatedr   RefResolutionErrorN)
r   r   r   rL   r   r   _RefResolutionErrorr   r   jsonschema.exceptions)r   r   r   rL   r   r   r   test_RefResolutionError   s   z(TestDeprecations.test_RefResolutionErrorc                 C   st   t ddi}| tjj}|d W d   n1 sw   Y  tjjdd}| |jt	|jf|df dS )a  
        This behavior is the intended behavior (i.e. it's not deprecated), but
        given we do "tricksy" things in the iterim to wrap exceptions in a
        multiple inheritance subclass, we need to be extra sure it works and
        stays working.
        $refurn:nothing   N)refUnresolvable: urn:nothing)
r	   Draft202012ValidatorassertRaisesreferencingr   Unresolvablevalidater   	exceptionstr)r   rA   r'   expectedr   r   r   #test_catching_Unresolvable_directly   s   z4TestDeprecations.test_catching_Unresolvable_directlyc                 C   s   |  t ddlm} W d   n1 sw   Y  tddi}| tjj	}|
d W d   n1 s9w   Y  | |}|
d W d   n1 sSw   Y  | |jt|jf|jdf dS )z
        Until RefResolutionError is removed, it is still possible to catch
        exceptions from reference resolution using it, even though they may
        have been raised by referencing.
        r   rK   NrP   rQ   rR   rT   )assertWarnsr   r   rL   r	   rU   rV   rW   r   rX   rY   r   rZ   r[   r   rL   rA   ur'   r   r   r   1test_catching_Unresolvable_via_RefResolutionError   s   zBTestDeprecations.test_catching_Unresolvable_via_RefResolutionErrorc                 C   s   |  t ddlm} W d   n1 sw   Y  tddi}| tjj	}|
d W d   n1 s9w   Y  | |}|
d W d   n1 sSw   Y  | |j|jh | |j|jh dS )zS
        Ensure the wrapped referencing errors are hashable when possible.
        r   rK   NrP   rQ   rR   )r^   r   r   rL   r	   rU   rV   rW   r   rX   rY   assertInrZ   r_   r   r   r   (test_WrappedReferencingError_hashability   s   z9TestDeprecations.test_WrappedReferencingError_hashabilityc                 C   s   d}|  t|}G dd dtj}W d   n1 sw   Y  | |jt |  t|}G dd dtji d}W d   dS 1 sFw   Y  dS )ac  
        As of v4.12.0, subclassing a validator class produces an explicit
        deprecation warning.

        This was never intended to be public API (and some comments over the
        years in issues said so, but obviously that's not a great way to make
        sure it's followed).

        A future version will explicitly raise an error.
        z!Subclassing validator classes is c                   @      e Zd ZdS )z=TestDeprecations.test_Validator_subclassing.<locals>.SubclassN__name__
__module____qualname__r   r   r   r   Subclass      ri   Nc                   @   rd   )zDTestDeprecations.test_Validator_subclassing.<locals>.AnotherSubclassNre   r   r   r   r   AnotherSubclass  rj   rk   )meta_schema)r   r   r	   rU   r   r   r   create)r   r   r   ri   rk   r   r   r   test_Validator_subclassing
  s   "z+TestDeprecations.test_Validator_subclassingc                 C   s^   |  tjjdd d}| t|}td W d   n1 s!w   Y  | |jt	 dS )zd
        As of v4.14.0, FormatChecker.cls_checks is deprecated without
        replacement.
        boomNzFormatChecker.cls_checks )

addCleanupr   checkerspopr   r   
cls_checksr   r   r   )r   r   r   r   r   r   test_FormatChecker_cls_checks!  s   z.TestDeprecations.test_FormatChecker_cls_checksc           
      C   sZ  d}|  t|}ddlm} W d   n1 sw   Y  | |tjj | |j	t
 d}|  t|}ddlm} W d   n1 sFw   Y  | |tjj | |j	t
 d}|  t|}ddlm} W d   n1 ssw   Y  | |tjj | |j	t
 d	}|  t|}dd
lm} W d   n1 sw   Y  | |tjj | |j	t
 d}|  t|}ddlm} W d   n1 sw   Y  | |tjj | |j	t
 d}|  t|}ddlm} W d   n1 sw   Y  | |tjj | |j	t
 | t ddlm}	 W d   dS 1 s&w   Y  dS )z
        As of v4.16.0, accessing jsonschema.draftn_format_checker is deprecated
        in favor of Validator.FORMAT_CHECKER.
        z3Accessing jsonschema.draft202012_format_checker is r   )draft202012_format_checkerNz3Accessing jsonschema.draft201909_format_checker is )draft201909_format_checkerz.Accessing jsonschema.draft7_format_checker is )draft7_format_checkerz.Accessing jsonschema.draft6_format_checker is )draft6_format_checkerz.Accessing jsonschema.draft4_format_checker is )draft4_format_checkerz.Accessing jsonschema.draft3_format_checker is )draft3_format_checker)draft1234_format_checker)r   r   r   ru   assertIsr	   rU   FORMAT_CHECKERr   r   r   rv   Draft201909Validatorrw   r>   rx   Draft6Validatorry   Draft4Validatorrz   Draft3ValidatorrV   ImportErrorr{   )
r   r   r   ru   rv   rw   rx   ry   rz   r{   r   r   r   test_draftN_format_checker/  sr   $z+TestDeprecations.test_draftN_format_checkerc                 C   sX   d}|  t|}ddl}t|j W d   n1 sw   Y  | |jtj dS )zH
        As of v4.17.0, importing jsonschema.cli is deprecated.
        z5The jsonschema CLI is deprecated and will be removed r   N)	r   r   jsonschema.clir   reloadclir   r   r   )r   r   r   r   r   r   r   test_import_clit  s   z TestDeprecations.test_import_clic                 C   s*   t jtjddgddd}| d|j dS )zB
        As of v4.17.0, the jsonschema CLI is deprecated.
        z-mr   T)capture_outputchecks!   The jsonschema CLI is deprecated N)
subprocessrunsys
executablerb   stderr)r   processr   r   r   test_cli  s   
zTestDeprecations.test_clic              	      s   d}ddddiii dt jv rt jjdt jd  dt jd< t fdd	}td
|i}d}tjj	t
jd|d}|5 t| |i |dfd W d   n1 s^w   Y  W d   dS W d   dS 1 svw   Y  dS )zW
        Automatic retrieval of remote references is deprecated as of v4.18.0.
        zhttp://bar#/$defs/bazz$defsbazr<   integerrequestsNc                 3   sd     | tjj | jd |  \\}}| d |d tt	
 dV  d S )Nz
http://barz
user-agentz.python-jsonschema (deprecated $ref resolution)utf8)rG   urllibrequestRequestr   full_urlheader_itemslowerr   jsondumpsencode)r   headerr1   schemar   r   r   fake_urlopen  s   zFTestDeprecations.test_automatic_remote_retrieval.<locals>.fake_urlopenrP   z+Automatically retrieving remote references urlopen)new%   )FT)r   modulesrp   __setitem__r   r	   rU   r   patchobjectr   r   r   r   r   r?   )r   rS   r   rA   r   r   r   r   r   test_automatic_remote_retrieval  s$   

Pz0TestDeprecations.test_automatic_remote_retrievalN)rf   rg   rh   r   r    r"   r*   r,   r.   r2   r6   r;   rC   rF   rH   rJ   rO   r]   ra   rc   rn   rt   r   r   r   r   r   r   r   r   r
      s0    Er
   )
contextlibr   ior   unittestr   r   importlib.metadatar   r   r   r   urllib.requestr   referencing.exceptionsrW   r   r   r   r   r	   r
   r   r   r   r   <module>   s    