python-jsonschema / python-jsonschema/jsonschema
Validator protocol type checking issues
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5k
- Forks
- 671
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 10
Description
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)?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by inspecting the jsonschema.protocols.Validator and Draft202012Validator definitions, then reproduce the reported assignment and isinstance checks with mypy. Compare the protocol members and runtime behavior shown in the issue; done means the typing diagnostics and protocol check behavior are either corrected or documented with a clear explanation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100