Skip to content

Commit 002edce

Browse files
authored
chore(python): Add .pyi "stub" file for type checkers support #391
1 parent df7452e commit 002edce

File tree

7 files changed

+77
-3
lines changed

7 files changed

+77
-3
lines changed

bindings/python/MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ include build.rs
33
include pyproject.toml
44
include rust-toolchain
55
recursive-include src *
6+
include jsonschema_rs/py.typed
7+
recursive-include jsonschema_rs *.pyi
68
recursive-include jsonschema-lib *
79
recursive-exclude jsonschema-lib Cargo.lock
810
recursive-exclude jsonschema-lib/target *
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from ._jsonschema_rs import *
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from typing import Any, TypeVar
2+
from collections.abc import Iterator
3+
4+
_SchemaT = TypeVar('_SchemaT', bool, dict[str, Any])
5+
6+
7+
def is_valid(
8+
schema: _SchemaT,
9+
instance: Any,
10+
draft: int | None = None,
11+
with_meta_schemas: bool | None = None
12+
) -> bool:
13+
pass
14+
15+
def validate(
16+
schema: _SchemaT,
17+
instance: Any,
18+
draft: int | None = None,
19+
with_meta_schemas: bool | None = None
20+
) -> None:
21+
pass
22+
23+
def iter_errors(
24+
schema: _SchemaT,
25+
instance: Any,
26+
draft: int | None = None,
27+
with_meta_schemas: bool | None = None
28+
) -> Iterator[ValidationError]:
29+
pass
30+
31+
32+
class JSONSchema:
33+
34+
def __init__(
35+
self,
36+
schema: _SchemaT,
37+
draft: int | None = None,
38+
with_meta_schemas: bool | None = None
39+
) -> None:
40+
pass
41+
42+
@classmethod
43+
def from_str(
44+
cls,
45+
schema: str,
46+
draft: int | None = None,
47+
with_meta_schemas: bool | None = None
48+
) -> 'JSONSchema':
49+
pass
50+
51+
def is_valid(self, instance: Any) -> bool:
52+
pass
53+
54+
def validate(self, instance: Any) -> None:
55+
pass
56+
57+
def iter_errors(self, instance: Any) -> Iterator[ValidationError]:
58+
pass
59+
60+
61+
class ValidationError(ValueError):
62+
message: str
63+
schema_path: list[str | int]
64+
instance_path: list[str | int]
65+
66+
67+
Draft4: int
68+
Draft6: int
69+
Draft7: int

bindings/python/jsonschema_rs/py.typed

Whitespace-only changes.

bindings/python/setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def call_setup():
2121
setup(
2222
name="jsonschema_rs",
2323
version="0.16.1",
24+
packages=["jsonschema_rs"],
2425
description="Fast JSON Schema validation for Python implemented in Rust",
2526
long_description=open("README.rst", encoding="utf-8").read(),
2627
long_description_content_type="text/x-rst",
@@ -32,7 +33,8 @@ def call_setup():
3233
python_requires=">=3.7",
3334
url="https://github.com/Stranger6667/jsonschema-rs/tree/master/python",
3435
license="MIT",
35-
rust_extensions=[RustExtension("jsonschema_rs", binding=Binding.PyO3)],
36+
rust_extensions=[RustExtension("jsonschema_rs._jsonschema_rs", binding=Binding.PyO3)],
37+
include_package_data=True,
3638
classifiers=[
3739
"Development Status :: 3 - Alpha",
3840
"Intended Audience :: Developers",

bindings/python/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ mod build {
450450

451451
/// JSON Schema validation for Python written in Rust.
452452
#[pymodule]
453-
fn jsonschema_rs(py: Python<'_>, module: &PyModule) -> PyResult<()> {
453+
fn _jsonschema_rs(py: Python<'_>, module: &PyModule) -> PyResult<()> {
454454
// To provide proper signatures for PyCharm, all the functions have their signatures as the
455455
// first line in docstrings. The idea is taken from NumPy.
456456
types::init();

bindings/python/tests-py/test_jsonschema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def test_validate(func):
6565

6666
def test_from_str_error():
6767
with pytest.raises(ValueError, match="Expected string, got int"):
68-
JSONSchema.from_str(42)
68+
JSONSchema.from_str(42) # type: ignore
6969

7070

7171
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)