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

Set Flags from UserConfig #6266

Merged
merged 6 commits into from
Dec 2, 2022

Conversation

MichelleArk
Copy link
Contributor

@MichelleArk MichelleArk commented Nov 16, 2022

resolves ##6327

Description

  • Incorporates UserConfig (if available) to Flags, preferring UserConfig values to environment variables and defaults.
  • Adds some initial tests for cli.Flags

Another option considered was to implement resolvers for each user-config param in cli/resolvers.py and use them to set defaults in params.py. However, this approach would still need access to profiles_dir prior to Flags initialization, and be much less succinct.

Checklist

@cla-bot cla-bot bot added the cla:yes label Nov 16, 2022
@github-actions
Copy link
Contributor

Thank you for your pull request! We could not find a changelog entry for this change. For details on how to document a change, see the contributing guide.

@MichelleArk MichelleArk changed the title [Prototype] prefer user_config to default in Flags Set Flags from UserConfig Nov 29, 2022
@MichelleArk MichelleArk marked this pull request as ready for review November 30, 2022 00:21
@MichelleArk MichelleArk requested a review from a team as a code owner November 30, 2022 00:21

# Hard coded flags
object.__setattr__(self, "WHICH", ctx.info_name)
object.__setattr__(
self, "WHICH", ctx.protected_args[0] if ctx.protected_args else ctx.info_name
Copy link
Contributor

@iknox-fa iknox-fa Nov 30, 2022

Choose a reason for hiding this comment

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

I'm not sure I follow what this is doing... According to the docs protected_args is used for certain extra parsing scenarios and that doesn't sound like that we're looking for when using flags.WHICH?

(as far as I can tell WHICH is only used to identify the command invoked in tracking events)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here I was looking to get an accurate value for WHICH regardless of whether the context was based on the CLI group or a subcommand. I assume we'll want callers going through the CLI group context because it does a lot of setup (and even implements some CLI functionality, such as --version).

In either case the caller would be responsible for figuring out what to set name to when constructing a Context object programmatically, which is then used to set flags.VERSION:

>>> from dbt.cli.main import cli
>>> from dbt.cli.flags import Flags
# Caller would need to infer that "run" is the right name to set based on args.
>>> ctx = cli.make_context(cli.name, ["--partial-parse", "run"])
>>> flags = Flags(ctx)
>>> flags.WHICH
'cli'

Perhaps invoked_subcommand is what we should use here, if available. However invoked_subcommand is only available if the command is actually being invoked, as opposed to based on the provided args. So it would be None in the example above.

I went with protected_args because of the docs hinting that it is used to implement nested parsing, but it does feel fragile as well.

Copy link
Contributor Author

@MichelleArk MichelleArk Nov 30, 2022

Choose a reason for hiding this comment

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

Related note - Currently on feature/click-cli, flags.WHICH is 'dbt' at this point, not the subcommand name when called directly. So it's technically not accurate unless we recreate flags further along in the subcommand method, which I understand we're looking to move away from doing. Using invoked_subcommand when available should at least fix the issue from the CLI interface.

Copy link
Contributor

Choose a reason for hiding this comment

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

As far as I'm aware, the real function of flags.WHICH is to support conditional checks in user code for {{ if flags.WHICH = ... }}. That is, conditional behavior that only wants to run during certain commands, and not others.

links:

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh I'm glad we had this conversation! Yes invoked_subcommand is exactly what we should be using-- In my head that's what info_name did-- I probably C/P'd the wrong line in the original code (info_name is right above invoked_subcommand in the output of pp vars(flags)).

Copy link
Contributor Author

@MichelleArk MichelleArk Dec 1, 2022

Choose a reason for hiding this comment

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

Updated flags.WHICH to use invoked_subcommand if available, falling back on ctx.info_name. I think it'd still be worth looking into protected_args more but I'll punt that to a follow-up if that's alright; feels a bit out of scope for this work.

object.__setattr__(self, "MP_CONTEXT", get_context("spawn"))

# Support console DO NOT TRACK initiave
object.__setattr__(
self,
"ANONYMOUS_USAGE_STATS",
False
if os.getenv("DO_NOT_TRACK", "").lower() in (1, "t", "true", "y", "yes")
if os.getenv("DO_NOT_TRACK", "").lower() in ("1", "t", "true", "y", "yes")
Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch! env vars != ints. Ever.

Copy link
Contributor

@iknox-fa iknox-fa left a comment

Choose a reason for hiding this comment

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

LGTM other than my question about protected_args

Copy link
Contributor

@iknox-fa iknox-fa left a comment

Choose a reason for hiding this comment

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

LGTM!

@MichelleArk MichelleArk merged commit e91863d into feature/click-cli Dec 2, 2022
@MichelleArk MichelleArk deleted the arky/click-flags-user-config branch December 2, 2022 20:23
iknox-fa pushed a commit that referenced this pull request Dec 4, 2022
flags with user config, flags.WHICH from invoked_subcommand if available
This was referenced Feb 8, 2023
@aranke aranke mentioned this pull request Feb 9, 2023
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants