Skip to content

Commit

Permalink
add ProjectFlags.project_only_flags from #9366
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelleArk committed Apr 23, 2024
1 parent 455d817 commit 9a1ae15
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
9 changes: 8 additions & 1 deletion core/dbt/cli/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ def _assign_params(
# Add entire invocation command to flags
object.__setattr__(self, "INVOCATION_COMMAND", "dbt " + " ".join(sys.argv[1:]))

# Overwrite default assignments with user config if available.
if project_flags:
# Overwrite default assignments with project flags if available.
param_assigned_from_default_copy = params_assigned_from_default.copy()
for param_assigned_from_default in params_assigned_from_default:
project_flags_param_value = getattr(
Expand All @@ -228,6 +228,13 @@ def _assign_params(
param_assigned_from_default_copy.remove(param_assigned_from_default)
params_assigned_from_default = param_assigned_from_default_copy

# Add project-level flags that are not available as CLI options / env vars
for (
project_level_flag_name,
project_level_flag_value,
) in project_flags.project_only_flags.items():
object.__setattr__(self, project_level_flag_name.upper(), project_level_flag_value)

Check warning on line 236 in core/dbt/cli/flags.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/cli/flags.py#L236

Added line #L236 was not covered by tests

# Set hard coded flags.
object.__setattr__(self, "WHICH", invoked_subcommand_name or ctx.info_name)
object.__setattr__(self, "MP_CONTEXT", get_context("spawn"))
Expand Down
4 changes: 4 additions & 0 deletions core/dbt/contracts/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ class ProjectFlags(ExtensibleDbtClassMixin, Replaceable):
warn_error_options: Optional[Dict[str, Union[str, List[str]]]] = None
write_json: Optional[bool] = None

@property
def project_only_flags(self) -> Dict[str, Any]:
return {}


@dataclass
class ProfileConfig(dbtClassMixin, Replaceable):
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/test_cli_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,14 @@ def test_global_flag_at_child_context(self):

assert flags_a.USE_COLORS == flags_b.USE_COLORS

def test_set_project_only_flags(self, project_flags, run_context):
flags = Flags(run_context, project_flags)

for project_only_flag, project_only_flag_value in project_flags.project_only_flags.items():
assert getattr(flags, project_only_flag) == project_only_flag_value
# sanity check: ensure project_only_flag is not part of the click context
assert project_only_flag not in run_context.params

def _create_flags_from_dict(self, cmd, d):
write_file("", "profiles.yml")
result = Flags.from_dict(cmd, d)
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import dbt.parser.manifest
from dbt import tracking
from dbt.contracts.files import SourceFile, FileHash, FilePath
from dbt.contracts.project import ProjectFlags
from dbt.contracts.graph.manifest import MacroManifest, ManifestStateCheck
from dbt.graph import NodeSelector, parse_difference
from dbt.events.functions import setup_event_logger
Expand Down Expand Up @@ -130,7 +131,7 @@ def get_config(self, extra_cfg=None):
cfg.update(extra_cfg)

config = config_from_parts_or_dicts(project=cfg, profile=self.profile)
dbt.flags.set_from_args(Namespace(), config)
dbt.flags.set_from_args(Namespace(), ProjectFlags())
setup_event_logger(dbt.flags.get_flags())
object.__setattr__(dbt.flags.get_flags(), "PARTIAL_PARSE", False)
return config
Expand Down

0 comments on commit 9a1ae15

Please sign in to comment.