question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow argument dependencies

See original GitHub issue

Is your feature request related to a problem? Please describe.

I am wondering if its possible to tie some argument values together. For example, when I train an Encoder-Decoder architecture, the encoder.output_dim should be the same like decoder.input_dim.

Describe the solution you’d like

I would like to specify only one of them in the CLI and have the other be set automatically. And if I accidentally specify both, I would like to be yelled at for doing something wrong.

How it could work:

dim: int = TiedParam(default=25)

@dataclass
class Encoder:
    output_dim: int = dim

@dataclass
class Decoder:
    input_dim: int = dim

Let me know what you think (or forgive my ignorance if its already possible!! 😃) Thanks!

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mauvilsacommented, Jun 12, 2022

@janvainer what you propose already exists in jsonargparse, see argument-linking. You might want to try that out.

1reaction
lebricecommented, May 28, 2022

Hey there @janvainer, thanks for posting this!

Hmm that’s interesting. From what is currently implemented, you have two options to take a look at, none of which do exactly what you want… 😞

This isn’t directly related to simple-parsing, it’s just a little feature for dataclasses that I’ve made into a shareable code snippet using GitHub Gist. Here’s the idea:

@dataclass
class Bob(HasConditionalFields):
    name: str = "Bob Jones"
    age: int = 26

    gamer_name: str = conditional_field(
        lambda name, age: f"xXx_{name}_{2022-age}_xXx",
    )

    email: str = conditional_field(
        lambda gamer_name: f"{gamer_name}@gmail.com"
    )


bob = Bob()
print(bob)
# Bob(name='Bob Jones', age=26, gamer_name='xXx_Bob Jones_1995_xXx',
#     email='Bob Jones@gmail.com', gamer_email='xXx_Bob Jones_1995_xXx@gmail.com')

The problem with this though is that I don’t think it works if the fields are on different dataclasses… You might be able to tweak the conditional_field function to achieve it though. If you do, please do let me know!

  • Option 2: Using passing a dest to the field function:
from argparse import Namespace
from dataclasses import dataclass
from simple_parsing import ArgumentParser, field
from simple_parsing.wrappers.field_wrapper import ArgumentGenerationMode


@dataclass
class Encoder:
    output_dim: int = field(default=128, dest="model.decoder.input_dim")


@dataclass
class Decoder:
    input_dim: int = field(default=128, cmd=False)


@dataclass
class ModelConfig:
    encoder: Encoder = field(default_factory=Encoder)
    decoder: Decoder = field(default_factory=Decoder)


def test_repro_issue_143():
    parser = ArgumentParser(argument_generation_mode=ArgumentGenerationMode.NESTED)
    parser.add_arguments(ModelConfig, dest="model")
    args = parser.parse_args("--model.encoder.output_dim 256".split())

    # Doesn't exactly work at the moment:
    assert args == Namespace(
        model=ModelConfig(encoder=Encoder(output_dim=128), decoder=Decoder(input_dim=128)),
        **{"model.decoder.input_dim": 256}
    )

This doesn’t really do what you want, and doesn’t really work as intended, in its current state. (I wrote this waaaaay back, and haven’t used it or tested it since…)

Hope this is somewhat helpful. I’d be really curious to hear some more ideas on how we could design this! The TiedField idea seems interesting. Perhaps we could also make use of type annotations for this somehow?

Let me know what you think! 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

8. Dependencies with Arguments - FastapiTutorial
Pass arguments to a class based dependency in FastAPI. ... We want to allow our website users to search for jobs by providing...
Read more >
Declaring gradle dependencies with arguments - Stack Overflow
I would like for a module to be able to accept an argument from a module that specifies it as a dependency. Can...
Read more >
Adding dependencies to pattern parameters - IBM
Pattern parameter dependencies are created between only two parameters. The Pattern Authoring view allows the entry of multiple supplier or client ...
Read more >
Dependency injection is passing an argument - ploeh blog
This is the application of a design pattern called constructor injection. It captures the dependencies in class fields, making them available ...
Read more >
8 Avoid dependencies between arguments
Avoid creating dependencies between details arguments so that only certain combinations are permitted. Dependencies between arguments makes functions harder ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found