Skip to content

Commit 3af0ac4

Browse files
Fix numeric bug (piccolo-orm#236)
* fixed the bug discussed in piccolo-orm#232 . if precision and scale are not set for the Numeric Field, 'None' value will be returned. * added a unit test for testing the bug-fix * fixed lint issues * add type annotations for precision and scale Co-authored-by: Daniel Townsend <[email protected]>
1 parent 23a8c1f commit 3af0ac4

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

piccolo/columns/column_types.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -890,18 +890,18 @@ def column_type(self):
890890
return "NUMERIC"
891891

892892
@property
893-
def precision(self):
893+
def precision(self) -> t.Optional[int]:
894894
"""
895895
The total number of digits allowed.
896896
"""
897-
return self.digits[0]
897+
return self.digits[0] if self.digits is not None else None
898898

899899
@property
900-
def scale(self):
900+
def scale(self) -> t.Optional[int]:
901901
"""
902902
The number of digits after the decimal point.
903903
"""
904-
return self.digits[1]
904+
return self.digits[1] if self.digits is not None else None
905905

906906
def __init__(
907907
self,

tests/utils/test_pydantic.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ class Movie(Table):
4545

4646
pydantic_model(box_office=decimal.Decimal("1.0"))
4747

48+
def test_numeric_without_digits(self):
49+
class Movie(Table):
50+
box_office = Numeric()
51+
52+
try:
53+
create_pydantic_model(table=Movie)
54+
except TypeError:
55+
self.fail(
56+
"Creating numeric field without"
57+
" digits failed in pydantic model."
58+
)
59+
else:
60+
self.assertTrue(True)
61+
4862

4963
class TestSecretColumn(TestCase):
5064
def test_secret_param(self):
@@ -93,7 +107,6 @@ class TestColumnHelpText(TestCase):
93107
"""
94108

95109
def test_help_text_present(self):
96-
97110
help_text = "In millions of US dollars."
98111

99112
class Movie(Table):
@@ -115,7 +128,6 @@ class TestTableHelpText(TestCase):
115128
"""
116129

117130
def test_help_text_present(self):
118-
119131
help_text = "Movies which were released in cinemas."
120132

121133
class Movie(Table, help_text=help_text):

0 commit comments

Comments
 (0)