diff --git a/pydantic_br/__init__.py b/pydantic_br/__init__.py index e851c88..daf0cae 100644 --- a/pydantic_br/__init__.py +++ b/pydantic_br/__init__.py @@ -1,28 +1,9 @@ -from typing import TYPE_CHECKING - -from .field_errors import * - __version__ = "1.0.1" -if TYPE_CHECKING: - CPF = str - CNH = str - CPFMask = str - CPFDigits = str - CNPJ = str - CNPJDigits = str - CNPJMask = str - TE = str - PIS = str - PISMask = str - PISDigits = str - Certidao = str - CertidaoMask = str - CertidaoDigits = str -else: - from .fields.certidao_field import * - from .fields.cnh_field import * - from .fields.cnpj_field import * - from .fields.cpf_field import * - from .fields.pis_field import * - from .fields.te_field import * +from .fields.base_field_errors import * +from .fields.certidao_field import * +from .fields.cnh_field import * +from .fields.cnpj_field import * +from .fields.cpf_field import * +from .fields.pis_field import * +from .fields.te_field import * diff --git a/pydantic_br/fields/base_field.py b/pydantic_br/fields/base_field.py index 9e74a19..9634f3b 100644 --- a/pydantic_br/fields/base_field.py +++ b/pydantic_br/fields/base_field.py @@ -1,14 +1,14 @@ -from ..get_versions import get_pydantic_version +from ..tools.get_versions import get_pydantic_version pydantic_version = get_pydantic_version() if pydantic_version.value == 1: - from .base_field_v1 import BaseDigitsv1 as BaseDigits # noqa - from .base_field_v1 import BaseMaskv1 as BaseMask # noqa - from .base_field_v1 import Basev1 as Base # noqa + from .base_field_v1 import BaseDigitsv1 as BaseDigits # type: ignore # noqa + from .base_field_v1 import BaseMaskv1 as BaseMask # type: ignore # noqa + from .base_field_v1 import Basev1 as Base # type: ignore # noqa if pydantic_version.value == 2: - from .base_field_v2 import BaseDigitsV2 as BaseDigits # noqa - from .base_field_v2 import BaseMaskV2 as BaseMask # noqa - from .base_field_v2 import BaseV2 as Base # noqa + from .base_field_v2 import BaseDigitsV2 as BaseDigits # type: ignore # noqa + from .base_field_v2 import BaseMaskV2 as BaseMask # type: ignore # noqa + from .base_field_v2 import BaseV2 as Base # type: ignore # noqa diff --git a/pydantic_br/fields/base_field_class.py b/pydantic_br/fields/base_field_class.py index 26d5124..0ed229a 100644 --- a/pydantic_br/fields/base_field_class.py +++ b/pydantic_br/fields/base_field_class.py @@ -1,6 +1,6 @@ from typing import Any, Callable, Generator -from ..field_errors import FieldTypes, raise_field +from .base_field_errors import FieldTypes, raise_field __all__ = ["BaseFieldClass"] diff --git a/pydantic_br/field_errors.py b/pydantic_br/fields/base_field_errors.py similarity index 71% rename from pydantic_br/field_errors.py rename to pydantic_br/fields/base_field_errors.py index deaf057..564963c 100644 --- a/pydantic_br/field_errors.py +++ b/pydantic_br/fields/base_field_errors.py @@ -1,19 +1,15 @@ from enum import Enum -from .get_versions import get_pydantic_version +from ..tools.get_versions import get_pydantic_version __all__ = [ "FieldTypeError", "FieldMaskError", "FieldDigitError", "FieldInvalidError", - "raise_field", ] -pydantic_version = get_pydantic_version() - - class FieldTypes(Enum): type = "type" mask = "mask" @@ -45,21 +41,20 @@ class FieldInvalidError: message_template = msg_template -def raise_error(code: str, msg_template: str): # type: ignore +def raise_error(code: str, msg_template: str): + pydantic_version = get_pydantic_version() + if pydantic_version.value == 1: from pydantic import PydanticTypeError - PydanticTypeError.code = code - PydanticTypeError.msg_template = msg_template - PydanticTypeError.message_template = msg_template - raise PydanticTypeError() + PydanticTypeError.code = code # type: ignore + PydanticTypeError.msg_template = msg_template # type: ignore + PydanticTypeError.message_template = msg_template # type: ignore + raise PydanticTypeError() # type: ignore if pydantic_version.value == 2: from pydantic_core import PydanticCustomError - PydanticCustomError.code = code - PydanticCustomError.msg_template = msg_template - PydanticCustomError.message_template = msg_template raise PydanticCustomError(code, msg_template) @@ -71,4 +66,4 @@ def raise_field(context: FieldTypes): FieldTypes.invalid: (FieldInvalidError.code, FieldInvalidError.msg_template), } - raise_error(*field_types.get(context)) + raise_error(*field_types[context]) diff --git a/pydantic_br/fields/base_field_v2.py b/pydantic_br/fields/base_field_v2.py index 6d8477b..3e109d2 100644 --- a/pydantic_br/fields/base_field_v2.py +++ b/pydantic_br/fields/base_field_v2.py @@ -2,12 +2,8 @@ from typing import Any, Dict, Mapping -from ..get_versions import get_pydantic_version from .base_field_class import BaseFieldClass -pydantic_version = get_pydantic_version() - - try: from pydantic_core import core_schema # type: ignore except ModuleNotFoundError: diff --git a/pydantic_br/get_versions.py b/pydantic_br/tools/get_versions.py similarity index 71% rename from pydantic_br/get_versions.py rename to pydantic_br/tools/get_versions.py index 9da57c0..11da63e 100644 --- a/pydantic_br/get_versions.py +++ b/pydantic_br/tools/get_versions.py @@ -1,5 +1,10 @@ from enum import Enum +try: + import pydantic # noqa +except ModuleNotFoundError: + raise ModuleNotFoundError("Are you sure you installed Pydantic?") + class PydanticVersion(Enum): v1 = 1 @@ -7,11 +12,6 @@ class PydanticVersion(Enum): def get_pydantic_version() -> PydanticVersion: - try: - import pydantic # noqa - except ModuleNotFoundError: - raise ModuleNotFoundError("Are you sure you installed Pydantic") - if hasattr(pydantic, "__version__"): if pydantic.__version__.startswith("1"): return PydanticVersion.v1 diff --git a/pyproject.toml b/pyproject.toml index 8ac53f5..3096c46 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -57,8 +57,6 @@ profile = "black" [tool.flake8] profile = "black" -[tool.mypy] -exclude = ['pydantic_br/field_errors.py', 'pydantic_br/fields/base_field.py'] [tool.taskipy.tasks] lint = "black pydantic_br && isort pydantic_br && mypy pydantic_br && flake8 pydantic_br" diff --git a/tests/test_base.py b/tests/test_base.py index 20c9d6e..3634e9e 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -1,4 +1,4 @@ -from pydantic import BaseModel +from pydantic import BaseModel, __version__ import pytest from pydantic_br import ( @@ -18,8 +18,6 @@ CertidaoDigits, ) -from pydantic_br.get_versions import get_pydantic_version, PydanticVersion - @pytest.fixture(scope="session") def model(): @@ -44,10 +42,10 @@ class GeneralModel(BaseModel): def test_model_schemas(model: BaseModel): - if get_pydantic_version() == PydanticVersion.v2: + if __version__.startswith("1"): model.model_dump() model.model_dump_json() model.model_json_schema() - if get_pydantic_version() == PydanticVersion.v1: + if __version__.startswith("2"): model.dict() model.schema()