lebrice / SimpleParsing

Simple, Elegant, Typed Argument Parsing with argparse
MIT License
384 stars 46 forks source link

Is it possible to model a list of configs using inheritance? #296

Closed jonghwanhyeon closed 6 months ago

jonghwanhyeon commented 6 months ago

Let's assume that we have the following configs:

@dataclass
class PoolConfig(Serializable):
    input_size: int = field(cmd=False)

@dataclassq
class GlobalAveragePoolConfig(PoolConfig):
    ...

@dataclass
class SegmentalAveragePoolConfig(PoolConfig):
    ...

@dataclass
class SegmentalSelfAttentiveAveragePoolConfig(PoolConfig):
    num_heads: int = 8

@dataclass
class Config(Serializable):
    global_average_pool_config: GlobalAveragePoolConfig
    segmental_average_pool_config: SegmentalAveragePoolConfig
    segmental_self_attentive_average_pool_config: SegmentalSelfAttentiveAveragePoolConfig

Then, how can we model easily this several configs?

For example, If you parse Config class from the following arguments,

--config.global-average-pool
--config.segmental-self-attentive-average-pool
--config.segmental-self-attentive-average-pool.num-heads=16

it returns:

Config(global_average_pool_config=GlobalAveragePoolConfig(input_size=None), segmental_self_attentive_average_pool_config=SegmentalSelfAttentiveAveragePoolConfig(input_size=None, num_heads=8))

Is is possible to configure this settings?

lebrice commented 6 months ago

Hello there! I'm not sure I understand the question. You're asking if we can have a list[Dataclass] field? If so, not quite, at least for the moment. However, you can use the "reuse" feature of the ConflictResolution enum, which looks similar. (Check the examples folder if that's what you're looking for)