New submission from Marc Abramowitz:

My expectation was that any defaults I passed to ConfigParser when creating one 
would override values in the DEFAULT section of the config file. This is 
because I'd like the DEFAULT section to have the default values, but then I 
want to be able to override those with settings from environment variables.

However, this is not the way it works. The defaults in the file take precedence 
over the defaults passed to the constructor. I didn't see a mention of this in 
the docs, but I might've missed it.

Take this short program (`configparsertest.py`):

```
import configparser

cp = configparser.ConfigParser({'foo': 'dog'})
print(cp.defaults())
cp.read('app.ini')
print(cp.defaults())
```

and this config file (`app.ini`):

```
[DEFAULT]
foo = bar
```

I was expecting that I would see foo equal to dog twice, but what I get is:

```
$ python configparsertest.py
OrderedDict([('foo', 'dog')])
OrderedDict([('foo', 'bar')])
```

The reason that I want the programmatic default values to override the default 
values in the file is that I want the file to have low-precedence defaults that 
are used as a last resort, and I want to be able to override the defaults with 
the values from environment variables.

As a concrete example, imagine that I have a config file for the stdlib 
`logging` module that looks something like this:

```
[DEFAULT]
logging_logger_root_level = WARN
...
[logger_root]
level = %(logging_logger_root_level)s
handlers = console
```

The desired behavior is that normally the app would use the WARN level for 
logging, but I'd like to be able to do something like:

```
$ LOGGING_LOGGER_ROOT_LEVEL=DEBUG python my_app.py
```

to get DEBUG logging.

Maybe there is some other mechanism to accomplish this?

----------
components: Library (Lib)
messages: 262989
nosy: Marc.Abramowitz
priority: normal
severity: normal
status: open
title: ConfigParser: Values in DEFAULT section override defaults passed to 
constructor
type: behavior

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue26710>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to