Skip to content

Commit c05699e

Browse files
committed
small tweaks
1 parent 5a1018e commit c05699e

File tree

6 files changed

+27
-13
lines changed

6 files changed

+27
-13
lines changed

piccolo/apps/asgi/commands/new.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def new():
4848

4949
if not os.path.exists(output_dir_path):
5050
folder_name = output_dir_path.split("/")[-1]
51-
if folder_name.startswith("_") or folder_name.startswith("."):
51+
if folder_name.startswith(("_", ".")):
5252
continue
5353
os.mkdir(dir_path)
5454

piccolo/apps/migrations/auto/serialisation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def serialise_params(params: t.Dict[str, t.Any]) -> t.Dict[str, t.Any]:
3535
for key, value in params.items():
3636
# Convert enums into plain values
3737
if isinstance(value, Enum):
38-
params[key] = f"{value.__class__.__name__}.{value.name}"
38+
params[key] = str(value)
3939

4040
# Replace any Table class values into class names
4141
if isclass(value) and issubclass(value, Table):
@@ -99,7 +99,7 @@ def deserialise_params(
9999
default = params.get("default")
100100
if isinstance(default, str):
101101
if column_class_name == "Timestamp":
102-
if default.startswith("TimestampDefault"):
102+
if default.startswith(("TimestampDefault", "DatetimeDefault")):
103103
_, item_name = default.split(".")
104104
params["default"] = getattr(TimestampDefault, item_name)
105105
else:

piccolo/columns/base.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,10 @@ def __init__(
178178
params=kwargs,
179179
)
180180

181-
@staticmethod
182181
def _validate_default(
183-
default: t.Any, allowed_types: t.Iterable[t.Union[None, t.Type[t.Any]]]
182+
self,
183+
default: t.Any,
184+
allowed_types: t.Iterable[t.Union[None, t.Type[t.Any]]],
184185
) -> bool:
185186
"""
186187
Make sure that the default value is of the allowed types.
@@ -193,7 +194,7 @@ def _validate_default(
193194
return True
194195
else:
195196
raise ValueError(
196-
"The default isn't one of the permitted types - "
197+
f"The default {default} isn't one of the permitted types - "
197198
f"{allowed_types}"
198199
)
199200

piccolo/columns/column_types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ def __init__(
376376

377377

378378
class Date(Column):
379+
379380
value_type = date
380381

381382
def __init__(self, default: DateArg = DateDefault.now, **kwargs) -> None:
@@ -386,6 +387,7 @@ def __init__(self, default: DateArg = DateDefault.now, **kwargs) -> None:
386387

387388

388389
class Time(Column):
390+
389391
value_type = time
390392

391393
def __init__(self, default: TimeArg = TimeDefault.now, **kwargs) -> None:

piccolo/custom_types.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class UUIDDefault(Enum):
5858

5959

6060
UUIDArg = t.Union[UUIDDefault, uuid.UUID, None]
61-
TimestampArg = t.Union[datetime.datetime, TimestampDefault, None]
62-
DateArg = t.Union[datetime.date, DateDefault, None]
63-
TimeArg = t.Union[datetime.time, TimeDefault, None]
61+
TimestampArg = t.Union[
62+
datetime.datetime, TimestampDefault, DatetimeDefault, None,
63+
]
64+
DateArg = t.Union[datetime.date, datetime.timedelta, DateDefault, None]
65+
TimeArg = t.Union[datetime.time, datetime.timedelta, TimeDefault, None]

tests/columns/test_defaults.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525

2626

2727
class TestDefaults(TestCase):
28-
def test_defaults(self):
29-
"""
30-
Columns check the type of the default argument.
31-
"""
28+
"""
29+
Columns check the type of the default argument.
30+
"""
31+
32+
def test_int(self):
3233
for _type in (Integer, BigInt, SmallInt):
3334
_type(default=0)
3435
_type(default=None, null=True)
@@ -37,6 +38,7 @@ def test_defaults(self):
3738
with self.assertRaises(ValueError):
3839
_type(default=None, null=False)
3940

41+
def test_text(self):
4042
for _type in (Text, Varchar):
4143
_type(default="")
4244
_type(default=None, null=True)
@@ -45,20 +47,23 @@ def test_defaults(self):
4547
with self.assertRaises(ValueError):
4648
_type(default=None, null=False)
4749

50+
def test_real(self):
4851
Real(default=0.0)
4952
Real(default=None, null=True)
5053
with self.assertRaises(ValueError):
5154
Real(default="hello world")
5255
with self.assertRaises(ValueError):
5356
Real(default=None, null=False)
5457

58+
def test_numeric(self):
5559
Numeric(default=decimal.Decimal(1.0))
5660
Numeric(default=None, null=True)
5761
with self.assertRaises(ValueError):
5862
Numeric(default="hello world")
5963
with self.assertRaises(ValueError):
6064
Numeric(default=None, null=False)
6165

66+
def test_uuid(self):
6267
UUID(default=None, null=True)
6368
UUID(default=UUIDDefault.uuid4)
6469
UUID(default=uuid.uuid4())
@@ -67,6 +72,7 @@ def test_defaults(self):
6772
with self.assertRaises(ValueError):
6873
UUID(default=None, null=False)
6974

75+
def test_time(self):
7076
Time(default=None, null=True)
7177
Time(default=TimeDefault.now)
7278
Time(default=datetime.datetime.now().time())
@@ -75,6 +81,7 @@ def test_defaults(self):
7581
with self.assertRaises(ValueError):
7682
Time(default=None, null=False)
7783

84+
def test_date(self):
7885
Date(default=None, null=True)
7986
Date(default=DateDefault.now)
8087
Date(default=datetime.datetime.now().date())
@@ -83,6 +90,7 @@ def test_defaults(self):
8390
with self.assertRaises(ValueError):
8491
Date(default=None, null=False)
8592

93+
def test_timestamp(self):
8694
Timestamp(default=None, null=True)
8795
Timestamp(default=TimestampDefault.now)
8896
Timestamp(default=datetime.datetime.now())
@@ -91,6 +99,7 @@ def test_defaults(self):
9199
with self.assertRaises(ValueError):
92100
Timestamp(default=None, null=False)
93101

102+
def test_foreignkey(self):
94103
ForeignKey(references=Table(), default=None, null=True)
95104
ForeignKey(references=Table(), default=1)
96105
with self.assertRaises(ValueError):

0 commit comments

Comments
 (0)