Skip to content

Commit

Permalink
refactor: change internal structure
Browse files Browse the repository at this point in the history
  • Loading branch information
scjorge committed Jul 31, 2024
1 parent 41401ba commit 3c9141b
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 64 deletions.
33 changes: 7 additions & 26 deletions pydantic_br/__init__.py
Original file line number Diff line number Diff line change
@@ -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 *
14 changes: 7 additions & 7 deletions pydantic_br/fields/base_field.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion pydantic_br/fields/base_field_class.py
Original file line number Diff line number Diff line change
@@ -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"]

Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)


Expand All @@ -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])
4 changes: 0 additions & 4 deletions pydantic_br/fields/base_field_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from enum import Enum

try:
import pydantic # noqa
except ModuleNotFoundError:
raise ModuleNotFoundError("Are you sure you installed Pydantic?")


class PydanticVersion(Enum):
v1 = 1
v2 = 2


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
Expand Down
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 3 additions & 5 deletions tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pydantic import BaseModel
from pydantic import BaseModel, __version__
import pytest

from pydantic_br import (
Expand All @@ -18,8 +18,6 @@
CertidaoDigits,
)

from pydantic_br.get_versions import get_pydantic_version, PydanticVersion


@pytest.fixture(scope="session")
def model():
Expand All @@ -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()

0 comments on commit 3c9141b

Please sign in to comment.