omry / omegaconf

Flexible Python configuration system. The last one you will ever need.
BSD 3-Clause "New" or "Revised" License
1.97k stars 109 forks source link

How can I get the data type of an attribute at a depth>1? #1052

Open aksajja opened 1 year ago

aksajja commented 1 year ago

With the combination of the dataclasses and omegaconf packages we can create hierarchial dataclasses like the following -

class SPORT(Enum):
    TENNIS = 'tennis'
    CHESS = 'chess'

@dataclass
class PreTrainConfig:
    model: Any
    data_config: Any

@dataclass
class PreTrainArgs:
    pretrain_config: PreTrainConfig
    new_sport: Optional[SPORT] = field(
        default=SPORT.TENNIS
    )

@dataclass
class M5Config(ModelConfig):
    pretrain_args: PreTrainArgs
    override_this: Union[None, int] = field(
        default=None
    )
    def __post_init__(self):
        self.param1 = self.chg_param1()
    @staticmethod
    def chg_param1():
        return '245'

@dataclass
class M5(M5Config):
    sport: SPORT = field(
        default=SPORT.TENNIS
    )

In this case how will I be able to print the type of M5.pretrain_args.new_sport or M5['pretrain_args']['new_sport']? get_type_hints() does not get the job done.