U
    ï¤ùdJ;  ã                   @   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-S ).ÚTestDeprecationsc              	   C   sN   d}|   t|¡}ddlm} W 5 Q R X |  |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__Ú
jsonschemaN)
ÚassertWarnsRegexÚDeprecationWarningr   r   ÚassertEqualÚ	importlibÚmetadataÚversionÚfilenameÚ__file__)ÚselfÚmessageÚwr   © r   úF/tmp/pip-unpacked-wheel-afshzn49/jsonschema/tests/test_deprecations.pyÚtest_version   s
    zTestDeprecations.test_versionc              	   C   sH   d}|   t|¡}ddlm} W 5 Q R X |  |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   s
    z*TestDeprecations.test_validators_ErrorTreec              	   C   sH   d}|   t|¡}ddlm} W 5 Q R X |  |tj¡ |  |jt¡ dS )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)   s
    z&TestDeprecations.test_import_ErrorTreec              	   C   sH   d}|   t|¡}ddlm} W 5 Q R X |  |tj¡ |  |jt¡ dS )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_FormatError6   s
    z(TestDeprecations.test_import_FormatErrorc              	   C   sH   d}|   t|¡}ddlm} W 5 Q R X |  |tj¡ |  |jt¡ dS )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_ValidatorC   s
    z&TestDeprecations.test_import_Validatorc              	   C   sB   d}|   t|¡}tj}W 5 Q R X |  |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   Z_VALIDATORSr   r   ©r   r   r   Úvaluer   r   r   Útest_validators_validatorsP   s
    z+TestDeprecations.test_validators_validatorsc              	   C   sB   d}|   t|¡}tj}W 5 Q R X |  |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	   Zmeta_schemasr   Z_META_SCHEMASr   r   r%   r   r   r   Útest_validators_meta_schemas]   s
    z-TestDeprecations.test_validators_meta_schemasc              
   C   sP   t j i ¡}d}|  t|¡}| d¡ W 5 Q R X W 5 Q R X |  |jt¡ dS )zC
        As of v4.0.0, RefResolver.in_scope is deprecated.
        z.jsonschema.RefResolver.in_scope is deprecated ÚfooN)	r	   Ú_RefResolverZfrom_schemar   r   Zin_scoper   r   r   )r   Úresolverr   r   r   r   r   Útest_RefResolver_in_scopej   s    z*TestDeprecations.test_RefResolver_in_scopec              	   C   sR   t  i ¡}d}|  t|¡}| dddi¡}W 5 Q R X |  |¡ |  |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_validZassertFalser   r   r   )r   Ú	validatorr   r   Úresultr   r   r   Ú%test_Validator_is_valid_two_argumentsw   s    

z6TestDeprecations.test_Validator_is_valid_two_argumentsc              	   C   sX   t  i ¡}d}|  t|¡}| dddi¡\}W 5 Q R X |  |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   Ziter_errorsr   r1   r   r   )r   r1   r   r   Úerrorr   r   r   Ú(test_Validator_iter_errors_two_arguments…   s    
z9TestDeprecations.test_Validator_iter_errors_two_argumentsc              	   C   sH   t  i ¡}d}|  t|¡}|  |jt j¡ W 5 Q R X |  |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+   r*   r   r   r   )r   r1   r   r   r   r   r   Útest_Validator_resolver“   s
    
z(TestDeprecations.test_Validator_resolverc              	   C   sl   d}|   t|¡}ddlm} W 5 Q R X |  |jt¡ |   t|¡}ddlm} W 5 Q R X |  |jt¡ dS )zA
        As of v4.18.0, RefResolver is fully deprecated.
        z$jsonschema.RefResolver is deprecatedr   )ÚRefResolverN)r   r   r   r8   r   r   r   r   )r   r   r   r8   r   r   r   Útest_RefResolverŸ   s    z!TestDeprecations.test_RefResolverc              	   C   sˆ   d}|   t|¡}ddlm} W 5 Q R X |  |tj¡ |  |jt¡ |   t|¡}ddl	m} W 5 Q R X |  |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   r;   r   r   Z_RefResolutionErrorr   r   Zjsonschema.exceptions)r   r   r   r;   r   r   r   Útest_RefResolutionError­   s    z(TestDeprecations.test_RefResolutionErrorc              	   C   s`   t  ddi¡}|  tjj¡}| d¡ W 5 Q R X 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é   )ÚrefúUnresolvable: urn:nothingN)
r	   ÚDraft202012ValidatorÚassertRaisesÚreferencingr   ÚUnresolvableÚvalidater   Ú	exceptionÚstr)r   r1   ÚeÚexpectedr   r   r   Ú#test_catching_Unresolvable_directlyÀ   s    þz4TestDeprecations.test_catching_Unresolvable_directlyc              	   C   s–   |   t¡ ddlm} W 5 Q R X t ddi¡}|  tjj	¡}| 
d¡ W 5 Q R X |  |¡}| 
d¡ W 5 Q R X |  |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   r:   r=   r>   r?   rA   N)ÚassertWarnsr   r   r;   r	   rB   rC   rD   r   rE   rF   r   rG   rH   ©r   r;   r1   ÚurI   r   r   r   Ú1test_catching_Unresolvable_via_RefResolutionErrorÒ   s    þzBTestDeprecations.test_catching_Unresolvable_via_RefResolutionErrorc              	   C   sœ   |   t¡ ddlm} W 5 Q R X t ddi¡}|  tjj	¡}| 
d¡ W 5 Q R X |  |¡}| 
d¡ W 5 Q R X |  |j|jh¡ |  |j|jh¡ dS )zS
        Ensure the wrapped referencing errors are hashable when possible.
        r   r:   r=   r>   r?   N)rL   r   r   r;   r	   rB   rC   rD   r   rE   rF   ÚassertInrG   rM   r   r   r   Ú(test_WrappedReferencingError_hashabilityè   s    z9TestDeprecations.test_WrappedReferencingError_hashabilityc              	   C   sp   d}|   t|¡}G dd„ dtjƒ}W 5 Q R X |  |jt¡ |   t|¡}G dd„ dtji dƒ}W 5 Q R X 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                   @   s   e Zd ZdS )z=TestDeprecations.test_Validator_subclassing.<locals>.SubclassN©Ú__name__Ú
__module__Ú__qualname__r   r   r   r   ÚSubclass  s   rV   c                   @   s   e Zd ZdS )zDTestDeprecations.test_Validator_subclassing.<locals>.AnotherSubclassNrR   r   r   r   r   ÚAnotherSubclass  s   rW   )Zmeta_schemaN)r   r   r	   rB   r   r   r   Úcreate)r   r   r   rV   rW   r   r   r   Útest_Validator_subclassingú   s    z+TestDeprecations.test_Validator_subclassingc              	   C   sJ   |   tjjdd¡ d}|  t|¡}t d¡ W 5 Q R X |  |jt	¡ dS )zd
        As of v4.14.0, FormatChecker.cls_checks is deprecated without
        replacement.
        ZboomNzFormatChecker.cls_checks )
Ú
addCleanupr   ZcheckersÚpopr   r   Z
cls_checksr   r   r   )r   r   r   r   r   r   Útest_FormatChecker_cls_checks  s
    z.TestDeprecations.test_FormatChecker_cls_checksc           
   	   C   sÊ  d}|   t|¡}ddlm} W 5 Q R X |  |tjj¡ |  |j	t
¡ d}|   t|¡}ddlm} W 5 Q R X |  |tjj¡ |  |j	t
¡ d}|   t|¡}ddlm} W 5 Q R X |  |tjj¡ |  |j	t
¡ d}|   t|¡}dd	lm} W 5 Q R X |  |tjj¡ |  |j	t
¡ d
}|   t|¡}ddlm} W 5 Q R X |  |tjj¡ |  |j	t
¡ d}|   t|¡}ddlm} W 5 Q R X |  |tjj¡ |  |j	t
¡ |  t¡ ddlm}	 W 5 Q R X 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_checkerz3Accessing 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_checkerN)r   r   r   r]   ZassertIsr	   rB   ZFORMAT_CHECKERr   r   r   r^   ZDraft201909Validatorr_   r/   r`   ZDraft6Validatorra   ZDraft4Validatorrb   ZDraft3ValidatorrC   ÚImportErrorrc   )
r   r   r   r]   r^   r_   r`   ra   rb   rc   r   r   r   Útest_draftN_format_checker  sd    þþþþþþz+TestDeprecations.test_draftN_format_checkerc              	   C   sD   d}|   t|¡}ddl}t |j¡ W 5 Q R X |  |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   Zjsonschema.clir   ÚreloadÚclir   r   r   )r   r   r   r   r   r   r   Útest_import_clid  s
    z TestDeprecations.test_import_clic                 C   s(   t jtjddgdd}|  d|j¡ dS )zB
        As of v4.17.0, the jsonschema CLI is deprecated.
        z-mr   T)Úcapture_outputs!   The jsonschema CLI is deprecated N)Ú
subprocessÚrunÚsysÚ
executablerP   Ústderr)r   Úprocessr   r   r   Útest_clip  s
    
þzTestDeprecations.test_clic              
      sÀ   d}ddddiii‰ dt jkr6ˆ 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}|: ˆ t|¡" ˆ | i ¡| d¡fd¡ W 5 Q R X W 5 Q R X dS )zW
        Automatic retrieval of remote references is deprecated as of v4.18.0.
        zhttp://bar#/$defs/bazz$defsZbazr-   ÚintegerÚrequestsNc                 3   sb   ˆ  | tjj¡ ˆ | jd¡ |  ¡ \\}}ˆ | ¡ d¡ ˆ |d¡ tt	 
ˆ ¡ d¡ƒV  d S )Nz
http://barz
user-agentz.python-jsonschema (deprecated $ref resolution)Úutf8)r6   ÚurllibÚrequestÚRequestr   Úfull_urlÚheader_itemsÚlowerr   ÚjsonÚdumpsÚencode)ru   Úheaderr&   ©Zschemar   r   r   Úfake_urlopenˆ  s     ÿzFTestDeprecations.test_automatic_remote_retrieval.<locals>.fake_urlopenr=   z+Automatically retrieving remote references Úurlopen)Únewé%   )FT)rl   ÚmodulesrZ   Ú__setitem__r   r	   rB   r   ÚpatchÚobjectrt   ru   r   r   r   r0   )r   r@   r   r1   r   r…   r   r~   r   Útest_automatic_remote_retrieval{  s&    
  ÿ
þz0TestDeprecations.test_automatic_remote_retrievalN)rS   rT   rU   r   r   r    r"   r$   r'   r(   r,   r3   r5   r7   r9   r<   rK   rO   rQ   rY   r\   re   rh   rp   r‡   r   r   r   r   r
      s,   Er
   )Ú
contextlibr   Úior   Zunittestr   r   Úimportlib.metadatar   rz   rj   rl   Úurllib.requestrt   Zreferencing.exceptionsrD   r   r   r   r   r	   r
   r   r   r   r   Ú<module>   s   