Skip to content
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

Fix return types in pydantic validation methods #10

Merged
merged 3 commits into from
Jan 9, 2024
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v0.2.1 (2024-01-09)

### Fix

- Fix return types in pydantic validation methods

## v0.2.0

### Features
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pysimdjson-schemaful"
version = "0.2.0"
version = "0.2.1"
description = "Schema-aware pysimdjson loader for efficient parsing of large excessive inputs."
authors = ["Tzoiker <[email protected]>"]
repository = "https://github.com/tzoiker/pysimdjson-schemaful"
Expand Down
2 changes: 1 addition & 1 deletion simdjson_schemaful/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.2.0"
__version__ = "0.2.1"
2 changes: 1 addition & 1 deletion simdjson_schemaful/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import simdjson
from simdjson import Parser

JsonType = Union[dict, list, str, int, float, bool]
JsonType = Union[Dict[Any, Any], List[Any], str, int, float, bool]
Schema = Dict[Any, Any]
_Dict = Dict[Any, Any]
_List = List[Any]
Expand Down
9 changes: 6 additions & 3 deletions simdjson_schemaful/pydantic/v1.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, Optional, Type, TypeVar, Union
from typing import TYPE_CHECKING, Any, Dict, Optional, Type, TypeVar, Union

import pydantic
from pydantic import schema_of
Expand All @@ -10,6 +10,9 @@
from simdjson_schemaful import loads
from simdjson_schemaful.parser import Schema

if TYPE_CHECKING:
Model = TypeVar("Model", bound="BaseModel")


class ModelMetaclass(pydantic.main.ModelMetaclass):
def __new__(cls, *args: Any, **kwargs: Any) -> type:
Expand All @@ -25,10 +28,10 @@ def __new__(cls, *args: Any, **kwargs: Any) -> type:
class BaseModel(pydantic.BaseModel, metaclass=ModelMetaclass):
@classmethod
def parse_raw_simdjson(
cls,
cls: Type["Model"],
b: Union[str, bytes],
parser: Optional[Parser] = None,
) -> "BaseModel":
) -> "Model":
try:
obj = loads(b, schema=_REGISTRY[cls], parser=parser)
except (ValueError, TypeError, UnicodeDecodeError) as e:
Expand Down
9 changes: 6 additions & 3 deletions simdjson_schemaful/pydantic/v2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from typing import Any, Dict, Optional, TypeVar, Union
from typing import TYPE_CHECKING, Any, Dict, Optional, Type, TypeVar, Union

import pydantic
from pydantic import ValidationError
Expand All @@ -9,6 +9,9 @@
from simdjson_schemaful import loads
from simdjson_schemaful.parser import Schema

if TYPE_CHECKING:
Model = TypeVar("Model", bound="BaseModel")


class ModelMetaclass(pydantic._internal._model_construction.ModelMetaclass):
def __new__(cls, *args: Any, **kwargs: Any) -> type:
Expand All @@ -24,10 +27,10 @@ def __new__(cls, *args: Any, **kwargs: Any) -> type:
class BaseModel(pydantic.BaseModel, metaclass=ModelMetaclass):
@classmethod
def model_validate_simdjson(
cls,
cls: Type["Model"],
json_data: Union[str, bytes, bytearray],
parser: Optional[Parser] = None,
) -> pydantic.BaseModel:
) -> "Model":
try:
obj = loads(json_data, schema=_REGISTRY[cls], parser=parser)
except (ValueError, TypeError, UnicodeDecodeError) as e:
Expand Down
Loading