From 0715a67b53de5c0750500f067b0975f2b860eaf9 Mon Sep 17 00:00:00 2001 From: Arseny Boykov <36469655+Bobronium@users.noreply.github.com> Date: Tue, 16 Jul 2024 21:02:32 +0400 Subject: [PATCH 1/6] Allow for subclasses in defaults allowed types --- piccolo/columns/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/piccolo/columns/base.py b/piccolo/columns/base.py index 9d3e2b1cc..bdadbef9a 100644 --- a/piccolo/columns/base.py +++ b/piccolo/columns/base.py @@ -527,7 +527,7 @@ def _validate_default( elif ( default is None and None in allowed_types - or type(default) in allowed_types + or isinstance(default, tuple(allowed_types)) ): self._validated = True return True @@ -540,7 +540,7 @@ def _validate_default( self._validated = True return True elif ( - isinstance(default, Enum) and type(default.value) in allowed_types + isinstance(default, Enum) and isinstance(default.value, tuple(allowed_types)) ): self._validated = True return True From 4299e7c87728fb5d98d9429b00ecad148ee7c6fc Mon Sep 17 00:00:00 2001 From: Arseny Boykov <36469655+Bobronium@users.noreply.github.com> Date: Wed, 17 Jul 2024 13:21:20 +0400 Subject: [PATCH 2/6] Add tests for custom defaults types --- tests/columns/test_defaults.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tests/columns/test_defaults.py b/tests/columns/test_defaults.py index 77df731bf..935524f27 100644 --- a/tests/columns/test_defaults.py +++ b/tests/columns/test_defaults.py @@ -2,7 +2,6 @@ import decimal import uuid from unittest import TestCase - from piccolo.columns.column_types import ( UUID, UUID4, @@ -25,11 +24,16 @@ from piccolo.table import Table +def get_custom_default(base): + class CustomDefault(base): + pass + return CustomDefault() + + class TestDefaults(TestCase): """ Columns check the type of the default argument. """ - def test_int(self): for _type in (Integer, BigInt, SmallInt): _type(default=0) @@ -66,6 +70,8 @@ def test_uuid(self): UUID(default=None, null=True) UUID(default=UUID4()) UUID(default=uuid.uuid4()) + UUID(default=get_custom_default(UUID4)) + with self.assertRaises(ValueError): UUID(default="hello world") @@ -73,6 +79,8 @@ def test_time(self): Time(default=None, null=True) Time(default=TimeNow()) Time(default=datetime.datetime.now().time()) + Time(default=get_custom_default(TimeNow)) + with self.assertRaises(ValueError): Time(default="hello world") # type: ignore @@ -80,6 +88,8 @@ def test_date(self): Date(default=None, null=True) Date(default=DateNow()) Date(default=datetime.datetime.now().date()) + Date(default=get_custom_default(DateNow)) + with self.assertRaises(ValueError): Date(default="hello world") # type: ignore @@ -87,13 +97,14 @@ def test_timestamp(self): Timestamp(default=None, null=True) Timestamp(default=TimestampNow()) Timestamp(default=datetime.datetime.now()) + Timestamp(default=get_custom_default(TimestampNow)) + with self.assertRaises(ValueError): Timestamp(default="hello world") # type: ignore def test_foreignkey(self): class MyTable(Table): pass - ForeignKey(references=MyTable, default=None, null=True) ForeignKey(references=MyTable, default=1) with self.assertRaises(ValueError): From a6135242fc03d2d3889adb89c9b6beca2b7a1c2c Mon Sep 17 00:00:00 2001 From: Arseny Boykov <36469655+Bobronium@users.noreply.github.com> Date: Wed, 17 Jul 2024 13:22:38 +0400 Subject: [PATCH 3/6] Fix style --- tests/columns/test_defaults.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/columns/test_defaults.py b/tests/columns/test_defaults.py index 935524f27..3509cfd42 100644 --- a/tests/columns/test_defaults.py +++ b/tests/columns/test_defaults.py @@ -71,7 +71,6 @@ def test_uuid(self): UUID(default=UUID4()) UUID(default=uuid.uuid4()) UUID(default=get_custom_default(UUID4)) - with self.assertRaises(ValueError): UUID(default="hello world") @@ -80,7 +79,6 @@ def test_time(self): Time(default=TimeNow()) Time(default=datetime.datetime.now().time()) Time(default=get_custom_default(TimeNow)) - with self.assertRaises(ValueError): Time(default="hello world") # type: ignore @@ -89,7 +87,6 @@ def test_date(self): Date(default=DateNow()) Date(default=datetime.datetime.now().date()) Date(default=get_custom_default(DateNow)) - with self.assertRaises(ValueError): Date(default="hello world") # type: ignore @@ -98,13 +95,13 @@ def test_timestamp(self): Timestamp(default=TimestampNow()) Timestamp(default=datetime.datetime.now()) Timestamp(default=get_custom_default(TimestampNow)) - with self.assertRaises(ValueError): Timestamp(default="hello world") # type: ignore def test_foreignkey(self): class MyTable(Table): pass + ForeignKey(references=MyTable, default=None, null=True) ForeignKey(references=MyTable, default=1) with self.assertRaises(ValueError): From 53612461bd683a02b11c2ed6ec7b23c3ed6c3432 Mon Sep 17 00:00:00 2001 From: Arseny Boykov <36469655+Bobronium@users.noreply.github.com> Date: Wed, 17 Jul 2024 14:59:06 +0400 Subject: [PATCH 4/6] Fix type error --- piccolo/columns/base.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/piccolo/columns/base.py b/piccolo/columns/base.py index bdadbef9a..7e00516af 100644 --- a/piccolo/columns/base.py +++ b/piccolo/columns/base.py @@ -527,7 +527,7 @@ def _validate_default( elif ( default is None and None in allowed_types - or isinstance(default, tuple(allowed_types)) + or isinstance(default, tuple(t for t in allowed_types if t is not None)) ): self._validated = True return True @@ -539,8 +539,8 @@ def _validate_default( ): self._validated = True return True - elif ( - isinstance(default, Enum) and isinstance(default.value, tuple(allowed_types)) + elif isinstance(default, Enum) and isinstance( + default.value, tuple(t for t in allowed_types if t is not None) ): self._validated = True return True From f544f9a227d9e9a7980ea38d009152f59e58f1d3 Mon Sep 17 00:00:00 2001 From: Arseny Boykov <36469655+Bobronium@users.noreply.github.com> Date: Sat, 20 Jul 2024 15:47:40 +0000 Subject: [PATCH 5/6] Reformat base.py --- piccolo/columns/base.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/piccolo/columns/base.py b/piccolo/columns/base.py index 7e00516af..b9ed2bd18 100644 --- a/piccolo/columns/base.py +++ b/piccolo/columns/base.py @@ -527,7 +527,9 @@ def _validate_default( elif ( default is None and None in allowed_types - or isinstance(default, tuple(t for t in allowed_types if t is not None)) + or isinstance( + default, tuple(t for t in allowed_types if isinstance(t, type)) + ) ): self._validated = True return True @@ -540,7 +542,8 @@ def _validate_default( self._validated = True return True elif isinstance(default, Enum) and isinstance( - default.value, tuple(t for t in allowed_types if t is not None) + default.value, + tuple(t for t in allowed_types if isinstance(t, type)), ): self._validated = True return True From f4cc2e9651bc7deef7c214d5dbcfac516daaa45f Mon Sep 17 00:00:00 2001 From: Arseny Boykov <36469655+Bobronium@users.noreply.github.com> Date: Sat, 20 Jul 2024 15:48:11 +0000 Subject: [PATCH 6/6] Reformat test_defaults --- tests/columns/test_defaults.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/columns/test_defaults.py b/tests/columns/test_defaults.py index 3509cfd42..c6e32243f 100644 --- a/tests/columns/test_defaults.py +++ b/tests/columns/test_defaults.py @@ -2,6 +2,7 @@ import decimal import uuid from unittest import TestCase + from piccolo.columns.column_types import ( UUID, UUID4, @@ -27,6 +28,7 @@ def get_custom_default(base): class CustomDefault(base): pass + return CustomDefault() @@ -34,6 +36,7 @@ class TestDefaults(TestCase): """ Columns check the type of the default argument. """ + def test_int(self): for _type in (Integer, BigInt, SmallInt): _type(default=0)