HBNetwork / python-decouple

Strict separation of config from code.
MIT License
2.79k stars 192 forks source link

Prevent default value of being cast #101

Closed pedro-vale closed 4 years ago

pedro-vale commented 4 years ago

The goal behind this PR is to prevent default value to be casted.

Got scenario where I cast value I read from environment variable as int but in case variable is not present in want it to have default value as None. Currently this can't be done since the return value of get method is casted (https://github.com/henriquebastos/python-decouple/blob/master/decouple.py#L77).

As the value passed in default is something that is either the same type you define as cast or something of a known type passed (and expected) by user, maybe is not needed to cast it.

Also adjusted tests and tested locally.

henriquebastos commented 4 years ago

Thank you but this PR must be rejected.

By design and consistency all default values must be strings and must be casted.

If you want default to be None enforce it on your cast function.

Example: config(default=None, cast=lambda s: int(v) if s.isdigit() else s).

pedro-vale commented 4 years ago

Thanks for response! :D

I see. I didn't know default values must be passed as strings. I followed what I saw on documentation and also on followin tests: tests/test_ini.py and tests/test_env.py, where there are non-string default values being passed such as: 0, 1, True, False and None.