Skip to content

Validator protocol type checking issues #1358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
t1m013y opened this issue May 10, 2025 · 0 comments
Open

Validator protocol type checking issues #1358

t1m013y opened this issue May 10, 2025 · 0 comments

Comments

@t1m013y
Copy link

t1m013y commented May 10, 2025

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)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant