Skip to content

Commit 7c8d98d

Browse files
github-actions[bot]MichelleArkQMalcolm
authored
deprecation warnings for --models, --model, -m (#11729) (#11739)
(cherry picked from commit 6bbcce1) Co-authored-by: Michelle Ark <[email protected]> Co-authored-by: Quigley Malcolm <[email protected]>
1 parent fe876a3 commit 7c8d98d

File tree

9 files changed

+138
-5
lines changed

9 files changed

+138
-5
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: Features
2+
body: "11561"
3+
time: 2025-06-11T16:02:17.334525-04:00
4+
custom:
5+
Author: michelleark
6+
Issue: deprecate --models,--model, and -m flags

core/dbt/cli/flags.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from dbt.config.project import read_project_flags
1818
from dbt.config.utils import normalize_warn_error_options
1919
from dbt.contracts.project import ProjectFlags
20-
from dbt.deprecations import fire_buffered_deprecations, renamed_env_var
20+
from dbt.deprecations import fire_buffered_deprecations, renamed_env_var, warn
2121
from dbt.events import ALL_EVENT_NAMES
2222
from dbt_common import ui
2323
from dbt_common.clients import jinja
@@ -50,6 +50,8 @@
5050
}
5151

5252

53+
DEPRECATED_FLAGS_TO_WARNINGS = {("--models", "--model", "-m"): "model-param-usage-deprecation"}
54+
5355
WHICH_KEY = "which"
5456

5557

@@ -385,7 +387,7 @@ def _validate_event_time_configs(self) -> None:
385387
"Value for `--event-time-start` must be less than `--event-time-end`"
386388
)
387389

388-
def fire_deprecations(self):
390+
def fire_deprecations(self, ctx: Optional[Context] = None):
389391
"""Fires events for deprecated env_var usage."""
390392
[dep_fn() for dep_fn in self.deprecated_env_var_warnings]
391393
# It is necessary to remove this attr from the class so it does
@@ -394,12 +396,25 @@ def fire_deprecations(self):
394396

395397
fire_buffered_deprecations()
396398

399+
# Handle firing deprecations of CLI aliases separately using argv or dbtRunner args
400+
# because click doesn't make it possible to disambiguite which literal CLI option was used
401+
# and only preserves the 'canonical' representation.
402+
original_command_args = (
403+
ctx.obj["dbt_runner_command_args"]
404+
if (ctx and ctx.obj and "dbt_runner_command_args" in ctx.obj)
405+
else sys.argv
406+
)
407+
for deprecated_flags, warning in DEPRECATED_FLAGS_TO_WARNINGS.items():
408+
for deprecated_flag in deprecated_flags:
409+
if deprecated_flag in original_command_args:
410+
warn(warning)
411+
397412
@classmethod
398413
def from_dict(cls, command: CliCommand, args_dict: Dict[str, Any]) -> "Flags":
399414
command_arg_list = command_params(command, args_dict)
400415
ctx = args_to_context(command_arg_list)
401416
flags = cls(ctx=ctx)
402-
flags.fire_deprecations()
417+
flags.fire_deprecations(ctx=ctx)
403418
return flags
404419

405420
def set_common_global_flags(self):

core/dbt/cli/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def invoke(self, args: List[str], **kwargs) -> dbtRunnerResult:
5656
dbt_ctx.obj = {
5757
"manifest": self.manifest,
5858
"callbacks": self.callbacks,
59+
"dbt_runner_command_args": args,
5960
}
6061

6162
for key, value in kwargs.items():

core/dbt/cli/requires.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def wrapper(*args, **kwargs):
9696
fire_event(MainReportArgs(args=flags_dict_str))
9797

9898
# Deprecation warnings
99-
flags.fire_deprecations()
99+
flags.fire_deprecations(ctx)
100100

101101
if active_user is not None: # mypy appeasement, always true
102102
fire_event(MainTrackingUserState(user_state=active_user.state()))

core/dbt/deprecations.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ class PropertyMovedToConfigDeprecation(DBTDeprecation):
194194
_event = "PropertyMovedToConfigDeprecation"
195195

196196

197+
class ModelParamUsageDeprecation(DBTDeprecation):
198+
_name = "model-param-usage-deprecation"
199+
_event = "ModelParamUsageDeprecation"
200+
201+
197202
def renamed_env_var(old_name: str, new_name: str):
198203
class EnvironmentVariableRenamed(DBTDeprecation):
199204
_name = f"environment-variable-renamed:{old_name}"
@@ -272,6 +277,7 @@ def show_deprecations_summary() -> None:
272277
CustomKeyInObjectDeprecation(),
273278
CustomOutputPathInSourceFreshnessDeprecation(),
274279
PropertyMovedToConfigDeprecation(),
280+
ModelParamUsageDeprecation(),
275281
WEOInlcudeExcludeDeprecation(),
276282
]
277283

core/dbt/events/types.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,15 @@ def message(self) -> str:
700700
return line_wrap_message(deprecation_tag(description, self.__class__.__name__))
701701

702702

703+
class ModelParamUsageDeprecation(WarnLevel):
704+
def code(self) -> str:
705+
return "D032"
706+
707+
def message(self) -> str:
708+
description = "Usage of `--models`, `--model`, and `-m` is deprecated in favor of `--resource-type model --select`"
709+
return line_wrap_message(deprecation_tag(description))
710+
711+
703712
# =======================================================
704713
# I - Project parsing
705714
# =======================================================

core/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
# Minor versions for these are expected to be backwards-compatible
7474
"dbt-common>=1.22.0,<2.0",
7575
"dbt-adapters>=1.15.2,<2.0",
76-
"dbt-protos>=1.0.312,<2.0",
76+
"dbt-protos>=1.0.315,<2.0",
7777
# ----
7878
# Expect compatibility with all new versions of these packages, so lower bounds only.
7979
"packaging>20.9",

tests/functional/deprecations/test_deprecations.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import sys
23
from collections import defaultdict
34
from unittest import mock
45

@@ -7,6 +8,7 @@
78

89
import dbt_common
910
from dbt import deprecations
11+
from dbt.cli.main import dbtRunner
1012
from dbt.clients.registry import _get_cached
1113
from dbt.events.types import (
1214
CustomKeyInConfigDeprecation,
@@ -15,6 +17,7 @@
1517
DeprecationsSummary,
1618
DuplicateYAMLKeysDeprecation,
1719
GenericJSONSchemaValidationDeprecation,
20+
ModelParamUsageDeprecation,
1821
PackageRedirectDeprecation,
1922
WEOIncludeExcludeDeprecation,
2023
)
@@ -491,3 +494,95 @@ def test_weo_include_exclude_deprecation(
491494
assert "exclude" in event_catcher.caught_events[0].info.msg
492495
else:
493496
assert "exclude" not in event_catcher.caught_events[0].info.msg
497+
498+
499+
class TestModelsParamUsageDeprecation:
500+
501+
@mock.patch.object(sys, "argv", ["dbt", "ls", "--models", "some_model"])
502+
def test_models_usage(self, project):
503+
event_catcher = EventCatcher(ModelParamUsageDeprecation)
504+
505+
assert len(event_catcher.caught_events) == 0
506+
run_dbt(
507+
["ls", "--models", "some_model"],
508+
callbacks=[event_catcher.catch],
509+
)
510+
assert len(event_catcher.caught_events) == 1
511+
512+
513+
class TestModelsParamUsageRunnerDeprecation:
514+
515+
def test_models_usage(self, project):
516+
event_catcher = EventCatcher(ModelParamUsageDeprecation)
517+
518+
assert len(event_catcher.caught_events) == 0
519+
dbtRunner(callbacks=[event_catcher.catch]).invoke(["ls", "--models", "some_model"])
520+
assert len(event_catcher.caught_events) == 1
521+
522+
523+
class TestModelParamUsageDeprecation:
524+
@mock.patch.object(sys, "argv", ["dbt", "ls", "--model", "some_model"])
525+
def test_model_usage(self, project):
526+
event_catcher = EventCatcher(ModelParamUsageDeprecation)
527+
528+
assert len(event_catcher.caught_events) == 0
529+
run_dbt(
530+
["ls", "--model", "some_model"],
531+
callbacks=[event_catcher.catch],
532+
)
533+
assert len(event_catcher.caught_events) == 1
534+
535+
536+
class TestModelParamUsageRunnerDeprecation:
537+
538+
def test_model_usage(self, project):
539+
event_catcher = EventCatcher(ModelParamUsageDeprecation)
540+
541+
assert len(event_catcher.caught_events) == 0
542+
dbtRunner(callbacks=[event_catcher.catch]).invoke(["ls", "--model", "some_model"])
543+
assert len(event_catcher.caught_events) == 1
544+
545+
546+
class TestMParamUsageDeprecation:
547+
@mock.patch.object(sys, "argv", ["dbt", "ls", "-m", "some_model"])
548+
def test_m_usage(self, project):
549+
event_catcher = EventCatcher(ModelParamUsageDeprecation)
550+
551+
assert len(event_catcher.caught_events) == 0
552+
run_dbt(
553+
["ls", "-m", "some_model"],
554+
callbacks=[event_catcher.catch],
555+
)
556+
assert len(event_catcher.caught_events) == 1
557+
558+
559+
class TestMParamUsageRunnerDeprecation:
560+
def test_m_usage(self, project):
561+
event_catcher = EventCatcher(ModelParamUsageDeprecation)
562+
563+
assert len(event_catcher.caught_events) == 0
564+
dbtRunner(callbacks=[event_catcher.catch]).invoke(["ls", "-m", "some_model"])
565+
assert len(event_catcher.caught_events) == 1
566+
567+
568+
class TestSelectParamNoModelUsageDeprecation:
569+
570+
@mock.patch.object(sys, "argv", ["dbt", "ls", "--select", "some_model"])
571+
def test_select_usage(self, project):
572+
event_catcher = EventCatcher(ModelParamUsageDeprecation)
573+
574+
assert len(event_catcher.caught_events) == 0
575+
run_dbt(
576+
["ls", "--select", "some_model"],
577+
callbacks=[event_catcher.catch],
578+
)
579+
assert len(event_catcher.caught_events) == 0
580+
581+
582+
class TestSelectParamNoModelUsageRunnerDeprecation:
583+
def test_select_usage(self, project):
584+
event_catcher = EventCatcher(ModelParamUsageDeprecation)
585+
586+
assert len(event_catcher.caught_events) == 0
587+
dbtRunner(callbacks=[event_catcher.catch]).invoke(["ls", "--select", "some_model"])
588+
assert len(event_catcher.caught_events) == 0

tests/unit/test_events.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ def test_event_codes(self):
169169
core_types.CustomOutputPathInSourceFreshnessDeprecation(path=""),
170170
core_types.PropertyMovedToConfigDeprecation(key="", key_path="", file=""),
171171
core_types.WEOIncludeExcludeDeprecation(found_include=True, found_exclude=True),
172+
core_types.ModelParamUsageDeprecation(),
172173
# E - DB Adapter ======================
173174
adapter_types.AdapterEventDebug(),
174175
adapter_types.AdapterEventInfo(),

0 commit comments

Comments
 (0)