diff --git a/conda/plugins/subcommands/doctor/cli.py b/conda/plugins/subcommands/doctor/cli.py index 37f4392d9d6..903e7608b34 100644 --- a/conda/plugins/subcommands/doctor/cli.py +++ b/conda/plugins/subcommands/doctor/cli.py @@ -4,10 +4,9 @@ import argparse -from ....base.context import context, locate_prefix_by_name -from ....cli.common import validate_prefix +from ....base.context import context from ....cli.conda_argparse import add_parser_prefix -from ....exceptions import CondaEnvException +from ....deprecations import deprecated from ... import CondaSubcommand, hookimpl @@ -28,25 +27,12 @@ def get_parsed_args(argv: list[str]) -> argparse.Namespace: return args +@deprecated( + "24.3", "24.9", addendum="Use `conda.base.context.context.target_prefix` instead." +) def get_prefix(args: argparse.Namespace) -> str: - """ - Determine the correct prefix to use provided the CLI arguments and the context object. - - When not specified via CLI options, the default is the currently active prefix - """ - if args.name: - return locate_prefix_by_name(args.name) - - if args.prefix: - return validate_prefix(args.prefix) - - if context.active_prefix: - return context.active_prefix - - raise CondaEnvException( - "No environment specified. Activate an environment or specify the " - "environment via `--name` or `--prefix`." - ) + context.__init__(argparse_args=args) + return context.target_prefix def execute(argv: list[str]) -> None: @@ -54,8 +40,8 @@ def execute(argv: list[str]) -> None: from .health_checks import display_health_checks args = get_parsed_args(argv) - prefix = get_prefix(args) - display_health_checks(prefix, verbose=args.verbose) + context.__init__(argparse_args=args) + display_health_checks(context.target_prefix, verbose=args.verbose) @hookimpl diff --git a/news/12725-deprecate-get_prefix b/news/12725-deprecate-get_prefix new file mode 100644 index 00000000000..871ef3c7f31 --- /dev/null +++ b/news/12725-deprecate-get_prefix @@ -0,0 +1,19 @@ +### Enhancements + +* + +### Bug fixes + +* + +### Deprecations + +* Mark `conda.plugins.subcommands.doctor.cli.get_prefix` as pending deprecation. Use `conda.base.context.context.target_prefix` instead. (#12725) + +### Docs + +* + +### Other + +* diff --git a/tests/plugins/subcommands/doctor/test_cli.py b/tests/plugins/subcommands/doctor/test_cli.py index 2482c39c787..cfdde38f9df 100644 --- a/tests/plugins/subcommands/doctor/test_cli.py +++ b/tests/plugins/subcommands/doctor/test_cli.py @@ -57,36 +57,3 @@ def test_conda_doctor_with_test_environment(): assert MISSING_FILES_SUCCESS_MESSAGE in out assert not err # no error message assert not code # successful exit code - - -def test_get_prefix_name(): - assert get_prefix(Namespace(name="base", prefix=None)) == context.root_prefix - - -def test_get_prefix_bad_name(): - with pytest.raises(EnvironmentNameNotFound): - get_prefix(Namespace(name="invalid", prefix=None)) - - -def test_get_prefix_prefix(): - with make_temp_env() as prefix: - assert get_prefix(Namespace(name=None, prefix=prefix)) == prefix - - -def test_get_prefix_bad_prefix(tmp_path: Path): - with pytest.raises(DirectoryNotACondaEnvironmentError): - assert get_prefix(Namespace(name=None, prefix=tmp_path)) - - -def test_get_prefix_active(): - with make_temp_env() as prefix, env_vars( - {"CONDA_PREFIX": prefix}, - stack_callback=conda_tests_ctxt_mgmt_def_pol, - ): - assert get_prefix(Namespace(name=None, prefix=None)) == prefix - - -def test_get_prefix_not_active(monkeypatch: MonkeyPatch): - monkeypatch.delenv("CONDA_PREFIX") - with pytest.raises(CondaEnvException): - get_prefix(Namespace(name=None, prefix=None))