Skip to content

Commit

Permalink
Allow specifying a non-relative module name in config_path (#2565)
Browse files Browse the repository at this point in the history
When constructing the config search path, allow callers to directly pass
in a non-relative module name starting with `pkg://`.  This allows
callers to bypass the relative name lookup, and always get consistent
behavior regardless of the current environment variables at runtime.

This addresses issue #2564.

Co-authored-by: Adam Simpkins <[email protected]>
Co-authored-by: Jasha <[email protected]>
  • Loading branch information
3 people committed Feb 22, 2023
1 parent 4910f6b commit f66646f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
10 changes: 7 additions & 3 deletions hydra/_internal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,13 @@ def compute_search_path_dir(
calling_module: Optional[str],
config_path: Optional[str],
) -> Optional[str]:
if config_path is not None and os.path.isabs(config_path):
search_path_dir = config_path
elif calling_file is not None:
if config_path is not None:
if os.path.isabs(config_path):
return config_path
if config_path.startswith("pkg://"):
return config_path

if calling_file is not None:
abs_base_dir = realpath(dirname(calling_file))

if config_path is not None:
Expand Down
6 changes: 5 additions & 1 deletion hydra/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ def main(
version_base: Optional[str] = _UNSPECIFIED_,
) -> Callable[[TaskFunction], Any]:
"""
:param config_path: The config path, a directory relative to the declaring python file.
:param config_path: The config path, a directory where Hydra will search for
config files. This path is added to Hydra's searchpath.
Relative paths are interpreted relative to the declaring python
file. Alternatively, you can use the prefix `pkg://` to specify
a python package to add to the searchpath.
If config_path is None no directory is added to the Config search path.
:param config_name: The name of the config (usually the file name without the .yaml extension)
"""
Expand Down
1 change: 1 addition & 0 deletions news/2564.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow config_path to specify a non-relative module path, by starting with `pkg://`
1 change: 1 addition & 0 deletions tests/test_config_search_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ def test_prepend(
"../conf",
realpath(os.path.join(os.getcwd(), "../conf")),
),
(None, "package.module", "pkg://some/conf", "pkg://some/conf"),
],
)
def test_compute_search_path_dir(
Expand Down

0 comments on commit f66646f

Please sign in to comment.