From 50fe1e0bb0db74adc831251ef9874891ff9bca7a Mon Sep 17 00:00:00 2001 From: Kamil Rybacki Date: Tue, 16 Jul 2024 11:28:17 +0200 Subject: [PATCH] Refactor consts.py to use NullModule class for blocked modules --- phaistos/consts.pyi | 6 +++++- phaistos/exceptions.pyi | 2 ++ phaistos/schema.pyi | 7 ++----- phaistos/transpiler.pyi | 2 -- phaistos/typings.pyi | 3 --- 5 files changed, 9 insertions(+), 11 deletions(-) diff --git a/phaistos/consts.pyi b/phaistos/consts.pyi index 0510c74..ddcb944 100644 --- a/phaistos/consts.pyi +++ b/phaistos/consts.pyi @@ -3,7 +3,11 @@ from types import ModuleType class NullModule(ModuleType): LOCK: ClassVar[bool] - def __getattr__(self, *args, **kwargs): ... + def __getattr__(self, *args, **kwargs): + """ + This method is called when the module is accessed, + to prevent the module from being accessed. + """ ALLOWED_COLLECTION_TYPES: set COLLECTION_TYPE_REGEX: str diff --git a/phaistos/exceptions.pyi b/phaistos/exceptions.pyi index 2b5670b..16d180f 100644 --- a/phaistos/exceptions.pyi +++ b/phaistos/exceptions.pyi @@ -1,4 +1,5 @@ import datetime +import dataclasses class ForbiddenModuleUseInValidator(ImportError): ... @@ -6,6 +7,7 @@ class SchemaLoadingException(Exception): ... class IncorrectFieldTypeError(ValueError): ... +@dataclasses.dataclass(kw_only=True) class FieldValidationErrorInfo: name: str message: str diff --git a/phaistos/schema.pyi b/phaistos/schema.pyi index af10cf8..762e28f 100644 --- a/phaistos/schema.pyi +++ b/phaistos/schema.pyi @@ -1,4 +1,3 @@ -import dataclasses from typing import Any, ClassVar import pydantic._internal._decorators @@ -19,12 +18,12 @@ class TranspiledSchema(pydantic.main.BaseModel): def compile(cls, model_data: TranspiledModelData) -> type[TranspiledSchema]: ... @classmethod def _rename_schema(cls, schema: type[TranspiledSchema], name: str) -> None: ... - def __init__(self, **data: Any) -> None: + def __init__(self, **data) -> None: """ A modified version of the Pydantic BaseModel __init__ method that passed the context to the validator. """ - def model_post_init(self: pydantic.main.BaseModel, __context: Any) -> None: + def model_post_init(self: pydantic.main.BaseModel, __context) -> None: """This function is meant to behave like a BaseModel method to initialise private attributes. It takes context as an argument since that's what pydantic-core passes when calling it. @@ -54,5 +53,3 @@ class SchemaInstancesFactory: ValidationResults: The validation results, including the schema, errors, and data. """ def build(self, data: dict[str, Any]) -> TranspiledSchema | None: ... - def __init__(self) -> None: ... - def __eq__(self, other) -> bool: ... diff --git a/phaistos/transpiler.pyi b/phaistos/transpiler.pyi index fb7aec6..7e51929 100644 --- a/phaistos/transpiler.pyi +++ b/phaistos/transpiler.pyi @@ -56,5 +56,3 @@ class Transpiler: def supress_logging(cls) -> None: ... @classmethod def enable_logging(cls) -> None: ... - def __init__(self) -> None: ... - def __eq__(self, other) -> bool: ... diff --git a/phaistos/typings.pyi b/phaistos/typings.pyi index 2d4b617..d675d91 100644 --- a/phaistos/typings.pyi +++ b/phaistos/typings.pyi @@ -110,6 +110,3 @@ class ValidationResults: schema: dict errors: list[FieldValidationErrorInfo] data: dict - __dataclass_params__: typing.ClassVar[dataclasses._DataclassParams] - __dataclass_fields__: typing.ClassVar[dict] - __match_args__: typing.ClassVar[tuple]