Skip to content

1.8.0

Choose a tag to compare

@mosquito mosquito released this 21 May 18:14
· 11 commits to master since this release
6831e77

Nested Argument Groups

Group can now contain other Group fields, with arbitrary nesting depth.

import argclass

class Credentials(argclass.Group):
    username: str = "admin"
    password: str = "secret"

class Endpoint(argclass.Group):
    host: str = "localhost"
    credentials: Credentials = Credentials()

class CLI(argclass.Parser):
    endpoint: Endpoint = Endpoint()

Names join the attribute path with the natural separator for each source:

Source Separator Example
CLI - --endpoint-credentials-username
ENV _ <PREFIX>ENDPOINT_CREDENTIALS_USERNAME
INI . [endpoint.credentials] section, username
TOML . [endpoint.credentials] table, username
JSON nested {"endpoint": {"credentials": {"username": …}}}

Group(prefix=…) overrides only the CLI/env segment for that level; config section names always follow the attribute path so they stay predictable.

Group fields with type annotations

A group attribute can now be declared with a type hint. argclass enforces these rules at class definition time:

Form Behaviour
g: G Auto-instantiated as G()
g: G = G() Uses the provided instance
g: G = ... Auto-instantiated as G()
g = G() (no annotation) Uses the provided instance
g: G | None = None Rejected (Group can't be None)
g: G = None Rejected
g: G | int, G1 | G2 Rejected (Group-specific error)
g: G = G2() (wrong class) Rejected

Rejected forms raise ArgumentDefinitionError with an actionable hint.

Cycle detection

Reusing the same Group instance across multiple attributes — directly or through a cycle of any length — raises ArgclassError at parse_args time, with the offending path included in the message:

ArgclassError: Group instance is referenced more than once in the parser tree
(current path: root.b.c.a). …

Other

  • AbstractDefaultsParser.get_value(section=...) now accepts dotted paths ("endpoint.credentials") for JSON/TOML descent; INI literal-section lookup still works.
  • Help output gets a dedicated argument_group per nested level, titled with the dotted attribute path.
  • Existing examples, README, and the python -m argclass groups interactive demo cover nested groups end-to-end.

Full Changelog: 1.7.0...1.8.0