question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Defaults override boolean values provided in config file.

See original GitHub issue

Hi, The following self-contained code demonstrates, that when parsing booleans, defaults override, whereas the same does not happen with int:

import pytest
import tempfile
import configargparse

@pytest.fixture
def example_config_file():
    cfg_str = '''correlation_thr = 0.3
basedir = /mnt/datafast
plot_curves = True
experiment_names = [RD1005, RD1006a, RD1007, RD1008, RD1009]
use_saved = 1
area_thr = 150
power_thr = 0.2
gene = TRP
recalculate_quantified = 1000
analyze = duration_s
include_all_days = False
take_best_days = 0
required_repetitions = 1'''
    with tempfile.NamedTemporaryFile('w+') as tf:
        tf.write(cfg_str)
        tf.seek(0)
        tf.flush()
        yield tf

def test_config_file(example_config_file):
    '''
    Tests whether values written to config file will get parsed correctly.
    Make sure you added --noconftest to "Additional arguments" in pycharm configuration or on command-line, so that confstest.py will not indirectly invoke visexpA.configuration.
    '''
    import sys
    parser = configargparse.get_arg_parser()
    parser.add_argument('--param_file', default = example_config_file.name, is_config_file=True)  # default value for temp file
    parser.add_argument('--area_thr', type=int, default=100)
    parser.add_argument('--take_best_days', default=1, type=int)
    parser.add_argument('--include_all_days', default=1, type=bool)
    parsed, unrecognized = parser.parse_known_args(config_file_contents=example_config_file.read())
    assert parsed.area_thr == 150
    assert parsed.take_best_days == False
    assert parsed.include_all_days == False
    parsed1, unrecognized1 = parser.parse_known_args()  # load via the provided default filename
    assert parsed1.take_best_days == False

Giving:

example_config_file = <tempfile._TemporaryFileWrapper object at 0x7fe218b7e4e0>

    def test_config_file(example_config_file):
        '''
        Tests whether values written to config file will get parsed correctly.
        Make sure you added --noconftest to "Additional arguments" in pycharm configuration or on command-line, so that confstest.py will not indirectly invoke visexpA.configuration.
        '''
        import sys
        parser = configargparse.get_arg_parser()
        parser.add_argument('--param_file', default = example_config_file.name, is_config_file=True)  # default value for temp file
        parser.add_argument('--area_thr', type=int, default=100)
        parser.add_argument('--take_best_days', default=1, type=int)
        parser.add_argument('--include_all_days', default=1, type=bool)
        parsed, unrecognized = parser.parse_known_args(config_file_contents=example_config_file.read())
        assert parsed.area_thr == 150
        assert parsed.take_best_days == False
>       assert parsed.include_all_days == False
E       assert True == False

test_configuration.py:40: AssertionError

The only difference between take_best_days and include_all_days is that the latter is defined as bool. I would be grateful for a clarification on why this happens and whether this is the desired behavior. Thanks.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
bw2commented, Apr 2, 2020

This looks like a bug. I don’t immediately see why this is happening, but a PR to fix it would be appreciated.

0reactions
hillierdanicommented, Apr 17, 2020

Thanks very much for going after this issue, I will check whether this solves my problem and post my test case here soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set a default value on a Boolean in a Code First model?
To set the values to true of the existing rows when you run Update-Database command, you could do this in your Configuration class:...
Read more >
How to modify SELinux settings with booleans - Red Hat
The semanage command is an SELinux policy management tool. You can use it to view available boolean options: $ sudo semanage boolean --list...
Read more >
Configuration providers - .NET - Microsoft Learn
The XML settings are overridden by settings in the Environment variables configuration provider and the Command-line configuration provider.
Read more >
configparser — Configuration file parser — Python 3.11.1 ...
Config parsers provide option value getters that perform type conversion. By default getint() , getfloat() , and getboolean() are implemented. Should other ...
Read more >
Using Variables - Ansible Documentation
If you define the same parameter in a variable and by another method, the variable overrides the other setting. This approach allows host-specific...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found