Skip to content

Commit

Permalink
Allow setting None when using default_value
Browse files Browse the repository at this point in the history
  • Loading branch information
Endogen committed Jan 22, 2025
1 parent 38745aa commit 6f92832
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/contracting/storage/orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,22 @@ def __init__(self, contract, name, driver: Driver):
class Variable(Datum):
def __init__(self, contract, name, driver: Driver = driver, t=None, default_value=None):
self._type = None

if isinstance(t, type):
self._type = t

self._default_value = default_value

super().__init__(contract, name, driver=driver)
# Set initial value to default
self._driver.set(self._key, default_value, True)

def set(self, value):
if self._type is not None and value is not None:
assert isinstance(value, self._type), (
f'Wrong type passed to variable! '
f'Expected {self._type}, got {type(value)}.'
)

self._driver.set(self._key, value, True)

def get(self):
value = self._driver.get(self._key)
if value is None:
return self._default_value
return value
return self._driver.get(self._key)

class Hash(Datum):
def __init__(self, contract, name, driver: Driver = driver, default_value=None):
Expand Down

0 comments on commit 6f92832

Please sign in to comment.