Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pilo/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class CreatedCountMixin(object):

_created_count = 0

def __init__(self):
def __init__(self):
CreatedCountMixin._created_count += 1
self._count = CreatedCountMixin._created_count

Expand Down Expand Up @@ -389,7 +389,7 @@ def is_attached(self):
return self.parent is not None

def clone(self):
other = copy.copy(self)
other = copy.deepcopy(self)
other.parent = None
return other

Expand Down
25 changes: 25 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,28 @@ def send_to_zoo(self):
]:
obj = Animal.type.cast(desc)(desc)
self.assertIsInstance(obj, cls)

def test_override_hook(self):
"""
Test that Sub-classes can independently provide hooks on the fields by
cloning.
"""

class Item(pilo.Form):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not that its any better (still tedious) but u could do ~

class Item(pilo.Form):

    price = pilo.fields.Integer()

    @price.compute
    def price(self):
        return self.compute_price()

    def compute_price(self):
        return type(self).price._compute()


class Heirloom(Item):

    has_sentimental_value = pilo.fields.Boolean()

    def compute_price(self):
        return 1000000 if self.has_sentimental_value else 0

price = pilo.fields.Integer()

class Heirloom(Item):

has_sentimental_value = pilo.fields.Boolean()

price = Item.price.clone()

@price.compute
def get_price(self):
return 1000000 if self.has_sentimental_value else 0

item = Item(price=100)
self.assertEqual(item.price, 100)

heirloom = Heirloom(has_sentimental_value=False)
self.assertEqual(heirloom.price, 0)