Skip to content

Commit

Permalink
Merge pull request #10 from tzoiker/fix/typing
Browse files Browse the repository at this point in the history
Fix return types in pydantic validation methods
  • Loading branch information
tzoiker authored Jan 9, 2024
2 parents 0a22a12 + cce8829 commit 86af17a
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 9 deletions.
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

0 comments on commit 86af17a

Please sign in to comment.