jupyter / nbformat

Reference implementation of the Jupyter Notebook format
http://nbformat.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
261 stars 153 forks source link

DeprecationWarning with jsonschema 4.0.0 (validator.py:315) #232

Open slayoo opened 2 years ago

slayoo commented 2 years ago

Since 4.0.0 release of jsonschema (https://github.com/Julian/jsonschema/releases/tag/v4.0.0), the following error started popping out in our CI logs:

/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/nbformat/v4/nbbase.py:100: in output_from_msg
    return new_output(output_type=msg_type,
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/nbformat/v4/nbbase.py:67: in new_output
    validate(output, output_type)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/nbformat/v4/nbbase.py:39: in validate
    return validate(node, ref=ref, version=nbformat)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/nbformat/validator.py:268: in validate
    for error in iter_validate(nbdict, ref=ref, version=version,
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/nbformat/validator.py:315: in iter_validate
    for error in errors:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = Draft4Validator(schema={'$schema': 'http://json-...ft-04/schema#', 'additionalProperties': False, 'definitions': {'cel...utput_type', 'data', 'metadata'], ...}, ...}, 'description': 'Jupyter Note... JSON schema.', ...}, format_checker=None)
instance = {'output_type': 'display_data', 'metadata': {'needs_background': 'light'}, 'data': {'text/plain': '<Figure size 432x28...5t9nMOz+3R8Gt9MVaN9e+2lkAQEWlyzVK6ERGRChToRUSanAK9iEiTU6AXEWlyCvQiIk1OgV5EpMkp0IuINLn/BGTFCzE4DS3BAAAAAElFTkSuQmCC\n'}}
_schema = {'$ref': '#/definitions/display_data'}

    def iter_errors(self, instance, _schema=None):
        if _schema is not None:
>           warnings.warn(
                (
                    "Passing a schema to Validator.iter_errors "
                    "is deprecated and will be removed in a future "
                    "release. Call validator.evolve(schema=new_schema)."
                    "iter_errors(...) instead."
                ),
                DeprecationWarning,
            )
E           DeprecationWarning: Passing a schema to Validator.iter_errors is deprecated and will be removed in a future release. Call validator.evolve(schema=new_schema).iter_errors(...) instead.

HTH, Would be great release a fix, Thanks, Sylwester

bollwyvl commented 2 years ago

Note that jsonschema 4.x was released yesterday, and has already had some bugfix releases.

In the near term, you could:

westurner commented 2 years ago

Would a PR that does this in a minor release of nbformat fix this DeprecationWarning?:

DeprecationWarning: Passing a schema to Validator.iter_errors is deprecated and will be removed in a future release. Call validator.evolve(schema=new_schema).iter_errors(...) instead

Does the fix need to be backwards-compatible or can this one call be updated along with the version constraint?

goanpeca commented 2 years ago

Hi @westurner it could be backwards, by using a try/except if there is an attributerror (assuming the evolve call is not on older versions)

try:
     iterator = validator.evolve(schema=new_schema).iter_errors(...)
except AttributeError:
      iterator = Validator.iter_errors(...)
bollwyvl commented 2 years ago

We should likely never be creating a new instances of validators, even if the warning suggests it, as this is very expensive (see https://www.peterbe.com/plog/jsonschema-validate-10x-faster-in-python).

Likely we need to

westurner commented 2 years ago

If there's no risk of memory leak from keeping the configured validator in scope, IMHO that sounds like a justified PERF optimization that justifies a couple more lines of changes.

The-Compiler commented 2 years ago

FWIW if anyone else is seeing this as a warning in pytest (e.g. via testbook), adding this to pytest's config will ignore the warning:

[pytest]
filterwarnings =
    ignore:Passing a schema to Validator\.iter_errors is deprecated and will be removed in a future release\.:DeprecationWarning:jsonschema\.validators