jsonargparse icon indicating copy to clipboard operation
jsonargparse copied to clipboard

How to specify an numpy ArrayLike argument in config YAML?

Open odedbd opened this issue 2 months ago • 3 comments

I am using the pytorch TCN package (https://github.com/paul-krug/pytorch-tcn) an would like to instantiate the TCN class using a jsonargparse CLI.

TCN accepts the num_channels and dilations arguments with type hint numpy.typing.ArrayLike. Naively, I tried setting the arguments in the config using a list of values. Unfortunately that doesn't pass the type check of the parser.

  Problem with given class_path 'pytorch_tcn.TCN':
    Parser key "num_channels":
      Does not validate against any of the Union subtypes
      Subtypes: [<class 'numpy._typing._array_like._Buffer'>, <class 'complex'>, <class 'bytes'>, <class 'str'>]
      Errors:
        - Does not implement protocol _Buffer
          Subclass types expect one of:
          - a class path (str)
          - a dict with class_path entry
          - a dict without class_path but with init_args entry (class path given previously)
        - Not of type complex: complex() first argument must be a string or a number, not 'list'
        - Not of type bytes: argument should be a bytes-like object or ASCII string, not 'list'
        - Expected a <class 'str'>
      Given value type: <class 'list'>

I tried several other things which also didn't work, finally the only solution I got to work was to subclass the TCN class, override the problematic arguments with my own typehint of List and call the super class. It is working, but is pretty hackish. I was wondering if I was missing some proper way to do this.

class WrapTCN(pytorch_tcn.TCN):
    """wraps a TCN module with ArrayLike args defined as List instead"""

    def __init__(self, num_inputs: int, num_channels: List, *args, dilations: Optional[List] = None, **kwargs):
        super(WrapTCN, self).__init__(num_inputs, num_channels, *args, dilations=dilations, **kwargs)

Many thanks for taking the time to read through my question.

odedbd avatar Oct 21 '25 07:10 odedbd

You can register numpy.typing.ArrayLike as a type, you just need to implement a serializer and deserializer. See in the docs registering-types.

mauvilsa avatar Oct 21 '25 09:10 mauvilsa

I actually tried this and it didn't waork for me. I assume I did something wrong. When I have some time I will try to reproduce that and see where it went wrong. Thanks!

odedbd avatar Oct 23 '25 12:10 odedbd

I had not noted what numpy.typing.ArrayLike actually is, even though it is right there in the error message. Registering types only works when the type is a single class, not a complex union. But I guess register_type could be extended so that it also works for a case like this. Changing this issue from question to feature request.

mauvilsa avatar Dec 09 '25 22:12 mauvilsa