Closed kazet closed 1 year ago
You can create a custom cast function.
MAX_RETRIES = decouple.config("MAX_RETRIES", cast=lambda v: int(v) if v else None, default=None)
You can also achieve this with something like:
class Optional:
def __init__(self, type):
self.type = type
def __call__(self, value):
try:
return self.type(value)
except:
return None
MAX_RETRIES = decouple.config("MAX_RETRIES", cast=Optional(int), default=None)
so that e.g. I could write
MAX_RETRIES = decouple.config("MAX_RETRIES", cast=Optional(int), default=None)
, which will be None ifMAX_RETRIES
is not set and int value if it is.Currently such configuration produces: