HBNetwork / python-decouple

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

ENV when use Csv can't be optional #161

Closed mahdizojaji closed 1 year ago

mahdizojaji commented 1 year ago

I want to have an optional environment. When it's filled: separate data with a comma. and when the variable is not found, it should be an empty list.

I thought this code should work:

CORS_ALLOWED_ORIGINS = config(
    "CORS_ALLOWED_ORIGINS",
    cast=Csv(delimiter=","),
    default=[],
)

but I got this error:

Traceback (most recent call last):
  File "/project/manage.py", line 22, in <module>
    main()
  File "/project/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/env/lib/python3.10/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/env/lib/python3.10/site-packages/django/core/management/__init__.py", line 382, in execute
    settings.INSTALLED_APPS
  File "/env/lib/python3.10/site-packages/django/conf/__init__.py", line 102, in __getattr__
    self._setup(name)
  File "/env/lib/python3.10/site-packages/django/conf/__init__.py", line 89, in _setup
    self._wrapped = Settings(settings_module)
  File "/env/lib/python3.10/site-packages/django/conf/__init__.py", line 217, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/project/mycar/settings.py", line 130, in <module>
    CORS_ALLOWED_ORIGINS = config(
  File "/env/lib/python3.10/site-packages/decouple.py", line 248, in __call__
    return self.config(*args, **kwargs)
  File "/env/site-packages/decouple.py", line 107, in __call__
    return self.get(*args, **kwargs)
  File "/env/lib/python3.10/site-packages/decouple.py", line 101, in get
    return cast(value)
  File "/env/lib/python3.10/site-packages/decouple.py", line 286, in __call__
    return self.post_process(transform(s) for s in splitter)
  File "/env/lib/python3.10/site-packages/decouple.py", line 286, in <genexpr>
    return self.post_process(transform(s) for s in splitter)
  File "/usr/lib/python3.10/shlex.py", line 300, in __next__
    token = self.get_token()
  File "/usr/lib/python3.10/shlex.py", line 109, in get_token
    raw = self.read_token()
  File "/usr/lib/python3.10/shlex.py", line 140, in read_token
    nextchar = self.instream.read(1)
AttributeError: 'list' object has no attribute 'read'
lucasrcezimbra commented 1 year ago

You can pass default=None, and it will be cast to [] as mentioned in #149.

In [1]: from decouple import config, Csv

In [2]: config(
    ...:     "CORS_ALLOWED_ORIGINS",
    ...:     cast=Csv(delimiter=","),
    ...:     default=None,
    ...: )
Out[2]: []