Skip to content

Variable precedence #873

Closed
Closed

Description

@rgreinho

Playing with click's ability to read environment variables and provide default values, I noticed that the order in which the variables are looked up seems wrong.

Here is what I expected (first variable found wins):

  1. Retrieve the value from the CLI
  2. Retrieve the value from the environment
  3. Retrieve the default value

But here is what happened:

  1. Retrieve the value from the CLI
  2. Retrieve the default value
  3. Retrieve the value from the environment

This seems wrong because the fact that I set default values should not prevent the environment lookup to happen.

How to reproduce

Create the precedence.py file:

import click

@click.command()
@click.option('--name', help="Specify a name")
def prec(name):
    """Greet the world."""
    click.echo(f'Hey {name}!')


if __name__ == '__main__':
    prec(auto_envvar_prefix='CLICK', default_map={'name': 'Stranger'})

Then run:

$ export CLICK_NAME=Tiger
$ python precedence.py --name Joe
Hey Joe!
$ python precedence.py
Hey Stranger!

Then remove the default_map argument from the command call and you get:

$ export CLICK_NAME=Tiger
$ python precedence.py --name Joe
Hey Joe!
$ python precedence.py
Hey Tiger!

Versions

Python: 3.6.3
Click: 6.7

Activity

  1. added a commit that references this issue on Oct 14, 2017
    8cd9676
  2. chaoflow commented on Apr 25, 2018

    @chaoflow
    Contributor

    I also see this as an issue. Quoting the documentation: "By default, the default value for a parameter is pulled from the default flag that is provided when it’s defined, but that’s not the only place defaults can be loaded from."

    Currently, a default value provided via default_map takes precedence over environment variables, whereas environment variables take precedence over default values provided to click.option().

  3. added 2 commits that reference this issue on May 14, 2018
    51a0ad3
    535559a
  4. added this to the 7.0 milestone on Sep 10, 2018
  5. added 2 commits that reference this issue on Oct 8, 2018
  6. added a commit that references this issue on Oct 29, 2018
  7. locked as resolved and limited conversation to collaborators on Nov 13, 2020
  8. added
    parsing
    on Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    parsing

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions