Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global CLI flag should override env var flag #10423

Merged
merged 3 commits into from
Jul 10, 2024
Merged

Global CLI flag should override env var flag #10423

merged 3 commits into from
Jul 10, 2024

Conversation

gshank
Copy link
Contributor

@gshank gshank commented Jul 9, 2024

resolves #10304

Problem

When both an environment variable and a command line flag are provided, the cli flag should override the environment variable. This wasn't happening for "global" flags, which can occur both before and after the sub command, when the flag came after the command.

Solution

The click context contains a "child" context for command options and a "parent" context for flags which occur before the command. If there was no "cli" setting for a flag, it would use the env var. When processing the parent context flags we add code to check if the source of this parameter is an env var, and do not overwrite the value of the param if it has already been set by the user.

Checklist

  • I have read the contributing guide and understand what's expected of me
  • I have run this code in development and it appears to resolve the stated issue
  • This PR includes tests, or tests are not required/relevant for this PR
  • This PR has no interface changes (e.g. macros, cli, logs, json artifacts, config files, adapter interface, etc) or this PR has already received feedback and approval from Product or DX
  • This PR includes type annotations for new and modified functions

@gshank gshank requested a review from a team as a code owner July 9, 2024 23:09
@cla-bot cla-bot bot added the cla:yes label Jul 9, 2024
Copy link

codecov bot commented Jul 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.81%. Comparing base (88b8b10) to head (d56f22e).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #10423      +/-   ##
==========================================
- Coverage   88.81%   88.81%   -0.01%     
==========================================
  Files         180      180              
  Lines       22558    22562       +4     
==========================================
+ Hits        20036    20038       +2     
- Misses       2522     2524       +2     
Flag Coverage Δ
integration 86.16% <100.00%> (-0.02%) ⬇️
unit 62.10% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
Unit Tests 62.10% <100.00%> (+<0.01%) ⬆️
Integration Tests 86.16% <100.00%> (-0.02%) ⬇️

Copy link
Contributor

@ChenyuLInx ChenyuLInx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@@ -380,6 +380,22 @@ def test_global_flag_at_child_context(self):

assert flags_a.USE_COLORS == flags_b.USE_COLORS

def test_global_flag_with_env_var(self, monkeypatch):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this test

@gshank gshank merged commit 601fee0 into main Jul 10, 2024
66 checks passed
@gshank gshank deleted the cli_flag_precedence branch July 10, 2024 19:25
github-actions bot pushed a commit that referenced this pull request Jul 10, 2024
gshank added a commit that referenced this pull request Jul 12, 2024
(cherry picked from commit 601fee0)

Co-authored-by: Gerda Shank <gerda@dbtlabs.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] Environment variables take precedence over CLI flags that appear after the subcommand
2 participants