Easiest way to override PosixPath in hydra

113 views Asked by At

Consider the following yaml file for hydra config:

a:  
    b: !!python/object/apply:pathlib.PosixPath
    - /my/path/to/dir

How would I override a.b so that is stays PosixPath after providing a new path?

Running

python my_app.py ++a.b=/a/new/path

overrides a.b but it's obiously a string. Looking for a solution that not only works but preferably does not require a user to re-enter constructor information.

1

There are 1 answers

0
Proko On

One way I found is this:

a:  
    b: 
      filepath: ???
      _target_: pathlib.Path 
      _args_: 
        - ${a.b.filepath}

Then overriding is via

python ++a.b.filepath=/a/new/path

Here, I'm using instantiate API, the _target_ is a target class of object (pathlib is natively supported), and since constructor signature is pathlib.Path(*pathsegments) we need a list, if we would provide just a string, each letter would be a single segment.

For those wondering why filepath is required here:

  1. If we would supply value directly to a.b the instantiation is skipped (so it's still str, the same case as in the question)

  2. It might be skipped and _args_ might be used directly but it is not verbose for end-user (python ++a.b._args_=/a/new/path)