Set flags.INVOCATION_COMMAND
for programmatic invocations on a best-effort basis
#11779
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Resolves #10313
Problem
For programmatic invocations, the
flags.INVOCATION_COMMAND
within the invocation has whatever args were passed to the python script rather than the equivalent CLI command for dbt.There is currently no testing in CI related to
flags.INVOCATION_COMMAND
, and we consider this an informal feature rather than a fully-supported one.Solution
Set a global variable named
DBT_EQUIVALENT_COMMAND
withindbtRunner.invoke
and read from it withinFlags.__init__
.Testing
Since this is still merely best-effort, no new functional tests are included in this PR.
All the dbt functional tests will hit these code paths though, so we can see that they don't generate any exceptions.
Behavior for boolean flags
This code results in the following behavior:
Actual behavior in the comments:
In an ideal world, the code above would provide convert into the opposite CLI flag whenever the boolean value is false. But I don't think it is necessary to do here since users can do it on their end.
Assumptions
Implementation decisions
Went with a environment variable rather than a global variable with thread-safety in mind:
Caveats
If
flags.INVOCATION_COMMAND
is accessed without actually running aclick
-based CLI command or without usinginvoke
, this solution still won't have the equivalent CLI command. For example, some unit testing in the dbt-core repo will accessflags
withoutinvoke
or running a CLI command.Checklist