Skip to content

Commit 460f4fa

Browse files
authored
Merge pull request #1396 from sirosen/improve-protocol-init-signature
Fix `Validator` protocol init to match runtime
2 parents de60f18 + 1e58409 commit 460f4fa

File tree

5 files changed

+48
-5
lines changed

5 files changed

+48
-5
lines changed

jsonschema/protocols.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,11 @@ class Validator(Protocol):
108108
def __init__(
109109
self,
110110
schema: Mapping | bool,
111-
registry: referencing.jsonschema.SchemaRegistry,
111+
resolver: Any = None, # deprecated
112112
format_checker: jsonschema.FormatChecker | None = None,
113-
) -> None:
114-
...
113+
*,
114+
registry: referencing.jsonschema.SchemaRegistry = ...,
115+
) -> None: ...
115116

116117
@classmethod
117118
def check_schema(cls, schema: Mapping | bool) -> None:

jsonschema/tests/typing/__init__.py

Whitespace-only changes.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
This module acts as a test that type checkers will allow each validator
3+
class to be assigned to a variable of type `type[Validator]`
4+
5+
The assignation is only valid if type checkers recognize each Validator
6+
implementation as a valid implementer of the protocol.
7+
"""
8+
from jsonschema.protocols import Validator
9+
from jsonschema.validators import (
10+
Draft3Validator,
11+
Draft4Validator,
12+
Draft6Validator,
13+
Draft7Validator,
14+
Draft201909Validator,
15+
Draft202012Validator,
16+
)
17+
18+
my_validator: type[Validator]
19+
20+
my_validator = Draft3Validator
21+
my_validator = Draft4Validator
22+
my_validator = Draft6Validator
23+
my_validator = Draft7Validator
24+
my_validator = Draft201909Validator
25+
my_validator = Draft202012Validator
26+
27+
28+
# in order to confirm that none of the above were incorrectly typed as 'Any'
29+
# ensure that each of these assignments to a non-validator variable requires an
30+
# ignore
31+
none_var: None
32+
33+
none_var = Draft3Validator # type: ignore[assignment]
34+
none_var = Draft4Validator # type: ignore[assignment]
35+
none_var = Draft6Validator # type: ignore[assignment]
36+
none_var = Draft7Validator # type: ignore[assignment]
37+
none_var = Draft201909Validator # type: ignore[assignment]
38+
none_var = Draft202012Validator # type: ignore[assignment]

jsonschema/validators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def create(
147147
applicable_validators: _typing.ApplicableValidators = methodcaller(
148148
"items",
149149
),
150-
):
150+
) -> type[Validator]:
151151
"""
152152
Create a new validator class.
153153
@@ -511,7 +511,7 @@ def is_valid(self, instance, _schema=None):
511511
Validator.__name__ = Validator.__qualname__ = f"{safe}Validator"
512512
Validator = validates(version)(Validator) # type: ignore[misc]
513513

514-
return Validator
514+
return Validator # type: ignore[return-value]
515515

516516

517517
def extend(

noxfile.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
ROOT = Path(__file__).parent
88
PACKAGE = ROOT / "jsonschema"
9+
TYPING_TESTS= ROOT / "jsonschema" / "tests" / "typing"
910
BENCHMARKS = PACKAGE / "benchmarks"
1011
PYPROJECT = ROOT / "pyproject.toml"
1112
CHANGELOG = ROOT / "CHANGELOG.rst"
@@ -181,6 +182,9 @@ def typing(session):
181182
"""
182183
session.install("mypy", "types-requests", ROOT)
183184
session.run("mypy", "--config", PYPROJECT, PACKAGE)
185+
session.run(
186+
"mypy", "--config", PYPROJECT, "--warn-unused-ignores", TYPING_TESTS,
187+
)
184188

185189

186190
@session(tags=["docs"])

0 commit comments

Comments
 (0)