Skip to content

Commit

Permalink
support absolute --config-path (facebookresearch#2368)
Browse files Browse the repository at this point in the history
This was actually already supported as
os.path.join("/blah", "/abs/config"), just returns "/abs/config",
but we've made this explicit in code and docs now.
  • Loading branch information
pixelb committed Oct 3, 2022
1 parent 0570bb7 commit 6583abf
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
6 changes: 4 additions & 2 deletions hydra/_internal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ def compute_search_path_dir(
calling_module: Optional[str],
config_path: Optional[str],
) -> Optional[str]:
if calling_file is not None:
if config_path is not None and os.path.isabs(config_path):
search_path_dir = config_path
elif calling_file is not None:
abs_base_dir = realpath(dirname(calling_file))

if config_path is not None:
Expand Down Expand Up @@ -554,7 +556,7 @@ def __repr__(self) -> str:
"--config-path",
"-cp",
help="""Overrides the config_path specified in hydra.main().
The config_path is relative to the Python file declaring @hydra.main()""",
The config_path is absolute or relative to the Python file declaring @hydra.main()""",
)

parser.add_argument(
Expand Down
1 change: 1 addition & 0 deletions news/2368.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
support specifying an absolute path with `--config-path`
3 changes: 2 additions & 1 deletion tests/test_config_search_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def test_prepend(
("foo/bar.py", None, None, None),
("foo/bar.py", None, "conf", realpath("foo/conf")),
("foo/bar.py", None, "../conf", realpath("conf")),
("foo/bar.py", None, realpath("conf"), realpath("conf")),
("c:/foo/bar.py", None, "conf", realpath("c:/foo/conf")),
("c:/foo/bar.py", None, "../conf", realpath("c:/conf")),
(None, "module", None, "pkg://"),
Expand All @@ -169,7 +170,7 @@ def test_prepend(
"foo",
"package1.rename_package_to.module",
"../conf",
os.path.realpath(os.path.join(os.getcwd(), "../conf")),
realpath(os.path.join(os.getcwd(), "../conf")),
),
],
)
Expand Down
15 changes: 11 additions & 4 deletions tests/test_hydra.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ def test_sweep_complex_defaults(
eval "$(python {script} -sc uninstall=bash)"
--config-path,-cp : Overrides the config_path specified in hydra.main().
The config_path is relative to the Python file declaring @hydra.main()
The config_path is absolute or relative to the Python file declaring @hydra.main()
--config-name,-cn : Overrides the config_name specified in hydra.main()
--config-dir,-cd : Adds an additional config dir to the config search path
--experimental-rerun : Rerun a job from a previous config pickle
Expand Down Expand Up @@ -819,7 +819,7 @@ def test_sweep_complex_defaults(
eval "$(python {script} -sc uninstall=bash)"
--config-path,-cp : Overrides the config_path specified in hydra.main().
The config_path is relative to the Python file declaring @hydra.main()
The config_path is absolute or relative to the Python file declaring @hydra.main()
--config-name,-cn : Overrides the config_name specified in hydra.main()
--config-dir,-cd : Adds an additional config dir to the config search path
--experimental-rerun : Rerun a job from a previous config pickle
Expand Down Expand Up @@ -1033,7 +1033,14 @@ def test_override_with_invalid_group_choice(
assert re.search(msg, str(e.value)) is not None


@mark.parametrize("config_path", ["dir1", "dir2"])
@mark.parametrize(
"config_path",
[
"dir1",
"dir2",
os.path.abspath("tests/test_apps/app_with_multiple_config_dirs/dir2"),
],
)
@mark.parametrize("config_name", ["cfg1", "cfg2"])
def test_config_name_and_path_overrides(
tmpdir: Path, config_path: str, config_name: str
Expand All @@ -1048,7 +1055,7 @@ def test_config_name_and_path_overrides(
result, _err = run_python_script(cmd)
# normalize newlines on Windows to make testing easier
result = result.replace("\r\n", "\n")
assert result == f"{config_path}_{config_name}: true"
assert result == f"{os.path.basename(config_path)}_{config_name}: true"


@mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion website/docs/advanced/hydra-command-line-flags.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Running Hydra applications:
- **--run,-r**: Run is the default mode and is not normally needed.
- **--multirun,-m**: Run multiple jobs with the configured launcher and sweeper. See [Multi-run](/tutorials/basic/running_your_app/2_multirun.md).
<br/><br/>
- **--config-path,-cp**: Overrides the `config_path` specified in `hydra.main()`. The `config_path` is relative to the Python file declaring `@hydra.main()`.
- **--config-path,-cp**: Overrides the `config_path` specified in `hydra.main()`. The `config_path` is absolute or relative to the Python file declaring `@hydra.main()`.
- **--config-name,-cn**: Overrides the `config_name` specified in `hydra.main()`.
- **--config-dir,-cd**: Adds an additional config directory to the [config search path](search_path.md).
This is useful for installed apps that want to allow their users to provide additional configs.
Expand Down

0 comments on commit 6583abf

Please sign in to comment.