Skip to content

Fix Validator protocol init to match runtime #1396

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions jsonschema/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,11 @@ class Validator(Protocol):
def __init__(
self,
schema: Mapping | bool,
registry: referencing.jsonschema.SchemaRegistry,
resolver: Any = None, # deprecated
format_checker: jsonschema.FormatChecker | None = None,
) -> None:
...
*,
registry: referencing.jsonschema.SchemaRegistry = ...,
) -> None: ...

@classmethod
def check_schema(cls, schema: Mapping | bool) -> None:
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
This module acts as a test that type checkers will allow each validator
class to be assigned to a variable of type `type[Validator]`

The assignation is only valid if type checkers recognize each Validator
implementation as a valid implementer of the protocol.
"""
from jsonschema.protocols import Validator
from jsonschema.validators import (
Draft3Validator,
Draft4Validator,
Draft6Validator,
Draft7Validator,
Draft201909Validator,
Draft202012Validator,
)

my_validator: type[Validator]

my_validator = Draft3Validator
my_validator = Draft4Validator
my_validator = Draft6Validator
my_validator = Draft7Validator
my_validator = Draft201909Validator
my_validator = Draft202012Validator


# in order to confirm that none of the above were incorrectly typed as 'Any'
# ensure that each of these assignments to a non-validator variable requires an
# ignore
none_var: None

none_var = Draft3Validator # type: ignore[assignment]
none_var = Draft4Validator # type: ignore[assignment]
none_var = Draft6Validator # type: ignore[assignment]
none_var = Draft7Validator # type: ignore[assignment]
none_var = Draft201909Validator # type: ignore[assignment]
none_var = Draft202012Validator # type: ignore[assignment]
4 changes: 2 additions & 2 deletions jsonschema/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def create(
applicable_validators: _typing.ApplicableValidators = methodcaller(
"items",
),
):
) -> type[Validator]:
"""
Create a new validator class.
Expand Down Expand Up @@ -511,7 +511,7 @@ def is_valid(self, instance, _schema=None):
Validator.__name__ = Validator.__qualname__ = f"{safe}Validator"
Validator = validates(version)(Validator) # type: ignore[misc]

return Validator
return Validator # type: ignore[return-value]


def extend(
Expand Down
4 changes: 4 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

ROOT = Path(__file__).parent
PACKAGE = ROOT / "jsonschema"
TYPING_TESTS= ROOT / "jsonschema" / "tests" / "typing"
BENCHMARKS = PACKAGE / "benchmarks"
PYPROJECT = ROOT / "pyproject.toml"
CHANGELOG = ROOT / "CHANGELOG.rst"
Expand Down Expand Up @@ -181,6 +182,9 @@ def typing(session):
"""
session.install("mypy", "types-requests", ROOT)
session.run("mypy", "--config", PYPROJECT, PACKAGE)
session.run(
"mypy", "--config", PYPROJECT, "--warn-unused-ignores", TYPING_TESTS,
)


@session(tags=["docs"])
Expand Down
Loading