yahp icon indicating copy to clipboard operation
yahp copied to clipboard

dict values are always assumed to be JSON/dict

Open dlwh opened this issue 2 years ago • 0 comments

Playing around with a custom hparams with composer, I ran into this.

If I have an hparams with a dict[str, OtherHparams] member, the yaml parser will produce a dict[str, dict]. The values in this dict can be turned into OtherHparams via OtherHparams.create, but it would be better if the type being stored was actually the declared type.

Thanks!

from dataclasses import dataclass
import yahp as hp
from typing import List, Any, Dict, Optional
import os


@dataclass
class FooHparams(hp.Hparams):
    # name: str = hp.required("name of the dataset")
    urls: List[str] = hp.required("urls of the dataset. Supports braceexpand")
    json_text_key: str = hp.optional("key of the json text", default="text")

    def validate(self):
        pass

    def initialize_object(self):
        return self


@dataclass
class BarHparams(hp.Hparams):
    foo: Dict[str, FooHparams] = hp.required("dict")


hparams = BarHparams.create(os.path.join(os.path.dirname(__file__), 'bar.yaml'))

print(type(hparams.foo)) # dict, as expected
print(type(hparams.foo["aa"])) # prints dict rather than FooHparams
foo:
    aa:
        urls:
            - aa
    bb:
        urls:
            - qq

dlwh avatar Apr 07 '22 16:04 dlwh