U
    kuf=  ã                   @   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   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-k94ccfep/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   sl   t jddgd}t  ¡ }t j|gd}d}|  t|¡}||d< W 5 Q R X |  |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   ZValidationErrorr   r   r   r   r   r   )r   ÚeÚtreeZsubtreer   r   r   r   r   Útest_ErrorTree_setitem6   s    z'TestDeprecations.test_ErrorTree_setitemc              	   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_FormatErrorF   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_ValidatorS   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_validators`   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_schemasm   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 r!   N)	r	   Ú_RefResolverZfrom_schemar   r   Zin_scoper   r   r   )r   Úresolverr   r   r   r   r   Útest_RefResolver_in_scopez   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_arguments‡   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!   r2   r3   N)	r	   r4   r   r   Ziter_errorsr   r6   r   r   )r   r6   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	   r4   r   r   ÚassertIsInstancer0   r/   r   r   r   )r   r6   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   r=   r   r   r   r   )r   r   r   r=   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   r6   r$   Ú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?   rB   rC   rD   rF   N)ÚassertWarnsr   r   r@   r	   rG   rH   rI   r   rJ   rK   r   rL   rM   ©r   r@   r6   Úur$   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?   rB   rC   rD   N)rP   r   r   r@   r	   rG   rH   rI   r   rJ   rK   ÚassertInrL   rQ   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   rZ   c                   @   s   e Zd ZdS )zDTestDeprecations.test_Validator_subclassing.<locals>.AnotherSubclassNrV   r   r   r   r   ÚAnotherSubclass  s   r[   )Zmeta_schemaN)r   r   r	   rG   r   r   r   Úcreate)r   r   r   rZ   r[   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   ra   ZassertIsr	   rG   ZFORMAT_CHECKERr   r   r   rb   ZDraft201909Validatorrc   r4   rd   ZDraft6Validatorre   ZDraft4Validatorrf   ZDraft3ValidatorrH   ÚImportErrorrg   )
r   r   r   ra   rb   rc   rd   re   rf   rg   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_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Ú
executablerT   Ústderr)r   Úprocessr   r   r   Útest_cli€  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bazr2   Ú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)r;   ÚurllibÚrequestÚRequestr   Úfull_urlÚheader_itemsÚlowerr   ÚjsonÚdumpsÚencode)rz   Úheaderr,   ©Zschemar   r   r   Úfake_urlopen™  s     ÿzFTestDeprecations.test_automatic_remote_retrieval.<locals>.fake_urlopenrB   z+Automatically retrieving remote references Úurlopen)Únewé%   )FT)rq   Úmodulesr^   Ú__setitem__r   r	   rG   r   ÚpatchÚobjectry   rz   r   r   r   r5   )r   rE   r„   r6   r   rŠ   r   rƒ   r   Útest_automatic_remote_retrievalŒ  s&    
  ÿ
þz0TestDeprecations.test_automatic_remote_retrievalN)rW   rX   rY   r   r   r    r&   r(   r*   r-   r.   r1   r8   r:   r<   r>   rA   rO   rS   rU   r]   r`   ri   rl   ru   rŒ   r   r   r   r   r
      s.   Er
   )Ú
contextlibr   Úior   Zunittestr   r   Úimportlib.metadatar   r   ro   rq   Úurllib.requestry   Zreferencing.exceptionsrI   r   r   r   r   r	   r
   r   r   r   r   Ú<module>   s   