Skip to content

Validator protocol type checking issues #1358

Open

Description

@t1m013y

In the following code PyCharm finds 1 warning and 1 error:

import jsonschema

v: jsonschema.protocols.Validator = jsonschema.Draft202012Validator({})
#                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#                                   Expected type 'Validator', got 'Draft202012Validator' instead
print(isinstance(jsonschema.Draft202012Validator({}), jsonschema.protocols.Validator))
#                                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#       Only @runtime_checkable protocols can be used with instance and class checks

mypy finds them too:

test004.py:3: error: Incompatible types in assignment (expression has type "Draft202012Validator", variable has type "Validator")  [assignment]
test004.py:3: note: Following member(s) of "Draft202012Validator" have conflicts:
test004.py:3: note:     Expected:
test004.py:3: note:         def evolve(self, **kwargs: Any) -> Validator
test004.py:3: note:     Got:
test004.py:3: note:         def evolve(self, **changes: Any) -> _Validator
test004.py:3: note:     schema: expected "dict[Any, Any] | bool", got "bool | Mapping[str, Any]"
test004.py:6: error: Only @runtime_checkable protocols can be used with instance and class checks  [misc]
Found 2 errors in 1 file (checked 1 source file)

But the code works and prints "True".

Why doesn't Validator protocol work as expected? Is this jsonschema library issue? Or am I doing something wrong here (and if yes, how to do it correctly)?

Activity

  1. vkuehn commented on Aug 18, 2025

    @vkuehn

    I have kind of the same issue. It looks like upgrading the library does cause the issue.

    After Upgrading Draft7Validator Signature still looks like in the old version where there was no registry

    Draft7Validator
    class Draft7Validator(
    schema: _Schema,
    resolver: Any | None = ...,
    format_checker: Any | None = ...
    )

    For a fresh installed version the signature looks like

    class Draft7Validator(
    schema: Schema,
    resolver: Any | None = None,
    format_checker: FormatChecker | None = None,
    *,
    registry: SchemaRegistry = ...,
    _resolver: Any | None = None
    )

    let me know if I should open a separate issue

  2. vkuehn commented on Aug 18, 2025

    @vkuehn

    @sirosen thanks for the upgrade !

    checked with 4.25.1 and still I can't resolve the new signature in VSCode or PyCharm. Any hint how to update the caches etc. for it is greatly appreciated.

    very odd theses editors just don't pickup the new functionality.

  3. sirosen commented on Aug 18, 2025

    @sirosen
    Member

    You need to wait for the change to percolate out, which has hardly even started yet. I'll probably work on backporting to typeshed during my train ride home today. The timeline is:

    • I backport to typeshed
    • typeshed maintainers accept the tweak
    • typeshed releases new packages
    • editors and type checkers pick up on the new typeshed packages
    • if you are installing types-jsonschema yourself, update that too
    • then you see the fix

    So please don't expect same-day resolution. Getting the change applied here is only one part of the coordinated ecosystem of packages which are implicated in your editor showing you a type hint.

  4. vkuehn commented on Aug 19, 2025

    @vkuehn

    thanks.

    @t1m013y sorry kind of hijacking your issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions