chore(python): Add .pyi "stub" file for type checkers support #391

This commit is contained in:
ermakov-oleg 2022-10-28 22:35:30 +04:00 committed by GitHub
parent df7452e904
commit 002edce5b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 77 additions and 3 deletions

View File

@ -3,6 +3,8 @@ include build.rs
include pyproject.toml
include rust-toolchain
recursive-include src *
include jsonschema_rs/py.typed
recursive-include jsonschema_rs *.pyi
recursive-include jsonschema-lib *
recursive-exclude jsonschema-lib Cargo.lock
recursive-exclude jsonschema-lib/target *

View File

@ -0,0 +1 @@
from ._jsonschema_rs import *

View File

@ -0,0 +1,69 @@
from typing import Any, TypeVar
from collections.abc import Iterator
_SchemaT = TypeVar('_SchemaT', bool, dict[str, Any])
def is_valid(
schema: _SchemaT,
instance: Any,
draft: int | None = None,
with_meta_schemas: bool | None = None
) -> bool:
pass
def validate(
schema: _SchemaT,
instance: Any,
draft: int | None = None,
with_meta_schemas: bool | None = None
) -> None:
pass
def iter_errors(
schema: _SchemaT,
instance: Any,
draft: int | None = None,
with_meta_schemas: bool | None = None
) -> Iterator[ValidationError]:
pass
class JSONSchema:
def __init__(
self,
schema: _SchemaT,
draft: int | None = None,
with_meta_schemas: bool | None = None
) -> None:
pass
@classmethod
def from_str(
cls,
schema: str,
draft: int | None = None,
with_meta_schemas: bool | None = None
) -> 'JSONSchema':
pass
def is_valid(self, instance: Any) -> bool:
pass
def validate(self, instance: Any) -> None:
pass
def iter_errors(self, instance: Any) -> Iterator[ValidationError]:
pass
class ValidationError(ValueError):
message: str
schema_path: list[str | int]
instance_path: list[str | int]
Draft4: int
Draft6: int
Draft7: int

View File

View File

@ -21,6 +21,7 @@ def call_setup():
setup(
name="jsonschema_rs",
version="0.16.1",
packages=["jsonschema_rs"],
description="Fast JSON Schema validation for Python implemented in Rust",
long_description=open("README.rst", encoding="utf-8").read(),
long_description_content_type="text/x-rst",
@ -32,7 +33,8 @@ def call_setup():
python_requires=">=3.7",
url="https://github.com/Stranger6667/jsonschema-rs/tree/master/python",
license="MIT",
rust_extensions=[RustExtension("jsonschema_rs", binding=Binding.PyO3)],
rust_extensions=[RustExtension("jsonschema_rs._jsonschema_rs", binding=Binding.PyO3)],
include_package_data=True,
classifiers=[
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",

View File

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

View File

@ -65,7 +65,7 @@ def test_validate(func):
def test_from_str_error():
with pytest.raises(ValueError, match="Expected string, got int"):
JSONSchema.from_str(42)
JSONSchema.from_str(42) # type: ignore
@pytest.mark.parametrize(