Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update fail_untyped flag to work with dataclasses whose arguments are classese with untyped arguments. #241

Closed
ioangatop opened this issue Feb 15, 2023 · 2 comments 路 Fixed by #242
Labels
enhancement New feature or request

Comments

@ioangatop
Copy link
Contributor

ioangatop commented Feb 15, 2023

馃殌 Feature request

The fail_untyped works with classes and nested classes, but id does not work with dataclasses which have class arguments with untyped arguments in their __init__.

Motivation

Dataclasses come handy when we build complex CLI frameworks as it organise the input fields without the need of typing class_path and init_args. The powerful feature here is to have objects as arguments that could be configured directly from the CLI. However, even though when these objects have arguments that are typed it works great, but the majority of open source doesn't, and thus limits us quite a bit.

Here is a snip to reproduce and to make sense of the above, quite complex 馃槄, description:

# main.py
from dataclasses import dataclass
from jsonargparse import CLI

class Database:
    def __init__(self, version) -> None:  # <-- HERE version is untyped; for `version: str` works!
        self._version = version

@dataclass
class MySQLConfig:
    db: Database
    host: str = "localhost"
    port: int = 8080

def my_app(cfg: MySQLConfig) -> None:
    print(f"{cfg.host}, {cfg.port}, {cfg.db._version}")

if __name__ == "__main__":
    CLI(my_app, fail_untyped=False)

Command:

python3 main.py --cfg.host "localhost" --cfg.port 8080 --cfg.db Database --cfg.db.version "0.0.1"

mind that when changing version to version: str, it works as expected.

Thank you for you time and for maintaining this great library!

@ioangatop ioangatop added the enhancement New feature or request label Feb 15, 2023
@mauvilsa
Copy link
Member

Thank you for the proposal. Looks good!

@ioangatop
Copy link
Contributor Author

@mauvilsa when you find the time, please check my PR #242 which fixes this!

mauvilsa pushed a commit that referenced this issue Feb 16, 2023
add_dataclass_arguments now supports the fail_untyped parameter #241.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants