From 79cf225ddc430a9a7b9835d4cb4cee1c2709076f Mon Sep 17 00:00:00 2001 From: collerek Date: Sat, 31 Oct 2020 18:42:13 +0100 Subject: [PATCH] fix scale and precision in decimal --- docs_src/fastapi/docs001.py | 2 +- docs_src/fields/docs001.py | 12 ++++++------ ormar/fields/foreign_key.py | 2 +- ormar/fields/many_to_many.py | 4 ++-- ormar/fields/model_fields.py | 12 ++++++------ tests/test_model_definition.py | 2 +- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/docs_src/fastapi/docs001.py b/docs_src/fastapi/docs001.py index 7987bef..efcc959 100644 --- a/docs_src/fastapi/docs001.py +++ b/docs_src/fastapi/docs001.py @@ -44,7 +44,7 @@ class Item(ormar.Model): id = ormar.Integer(primary_key=True) name = ormar.String(max_length=100) - category= ormar.ForeignKey(Category, nullable=True) + category = ormar.ForeignKey(Category, nullable=True) @app.get("/items/", response_model=List[Item]) diff --git a/docs_src/fields/docs001.py b/docs_src/fields/docs001.py index 304b92d..a757525 100644 --- a/docs_src/fields/docs001.py +++ b/docs_src/fields/docs001.py @@ -14,8 +14,8 @@ class Department(ormar.Model): database = database metadata = metadata - id = ormar.Integer(primary_key=True) - name = ormar.String(max_length=100) + id: int = ormar.Integer(primary_key=True) + name: str = ormar.String(max_length=100) class Course(ormar.Model): @@ -23,10 +23,10 @@ class Course(ormar.Model): database = database metadata = metadata - id = ormar.Integer(primary_key=True) - name = ormar.String(max_length=100) - completed= ormar.Boolean(default=False) - department= ormar.ForeignKey(Department) + id: int = ormar.Integer(primary_key=True) + name: str = ormar.String(max_length=100) + completed: bool = ormar.Boolean(default=False) + department: Optional[Department] = ormar.ForeignKey(Department) department = Department(name='Science') diff --git a/ormar/fields/foreign_key.py b/ormar/fields/foreign_key.py index c2f9911..0974746 100644 --- a/ormar/fields/foreign_key.py +++ b/ormar/fields/foreign_key.py @@ -37,7 +37,7 @@ def ForeignKey( # noqa CFQ002 virtual: bool = False, onupdate: str = None, ondelete: str = None, -) -> Type["ForeignKeyField"]: +) -> Any: fk_string = to.Meta.tablename + "." + to.get_column_alias(to.Meta.pkname) to_field = to.Meta.model_fields[to.Meta.pkname] __type__ = ( diff --git a/ormar/fields/many_to_many.py b/ormar/fields/many_to_many.py index e9fbb68..79fb7fa 100644 --- a/ormar/fields/many_to_many.py +++ b/ormar/fields/many_to_many.py @@ -17,7 +17,7 @@ def ManyToMany( unique: bool = False, virtual: bool = False, **kwargs: Any -) -> Type["ManyToManyField"]: +) -> Any: to_field = to.Meta.model_fields[to.Meta.pkname] related_name = kwargs.pop("related_name", None) nullable = kwargs.pop("nullable", True) @@ -50,7 +50,7 @@ def ManyToMany( class ManyToManyField(ForeignKeyField): through: Type["Model"] - if TYPE_CHECKING: # noqa: C901; pragma nocover + if TYPE_CHECKING: # noqa: C901; #pragma nocover @staticmethod async def add(item: "Model") -> None: diff --git a/ormar/fields/model_fields.py b/ormar/fields/model_fields.py index 780d2dd..29755a5 100644 --- a/ormar/fields/model_fields.py +++ b/ormar/fields/model_fields.py @@ -292,14 +292,14 @@ class Decimal(ModelFieldFactory, decimal.Decimal): kwargs["le"] = kwargs["maximum"] if kwargs.get("max_digits"): - kwargs["scale"] = kwargs["max_digits"] - elif kwargs.get("scale"): - kwargs["max_digits"] = kwargs["scale"] + kwargs["precision"] = kwargs["max_digits"] + elif kwargs.get("precision"): + kwargs["max_digits"] = kwargs["precision"] if kwargs.get("decimal_places"): - kwargs["precision"] = kwargs["decimal_places"] - elif kwargs.get("precision"): - kwargs["decimal_places"] = kwargs["precision"] + kwargs["scale"] = kwargs["decimal_places"] + elif kwargs.get("scale"): + kwargs["decimal_places"] = kwargs["scale"] return super().__new__(cls, **kwargs) diff --git a/tests/test_model_definition.py b/tests/test_model_definition.py index 840405d..66ced8a 100644 --- a/tests/test_model_definition.py +++ b/tests/test_model_definition.py @@ -31,7 +31,7 @@ class ExampleModel(Model): test_time = ormar.Time(default=datetime.time) test_json = ormar.JSON(default={}) test_bigint = ormar.BigInteger(default=0) - test_decimal = ormar.Decimal(scale=10, precision=2) + test_decimal = ormar.Decimal(scale=2, precision=10) test_decimal2 = ormar.Decimal(max_digits=10, decimal_places=2)