Registry / devops / argparse-dataclass

argparse-dataclass

JSON →
library2.0.0pypypi✓ verified 21d ago

argparse-dataclass simplifies creating command-line interfaces by automatically generating `argparse` arguments from Python dataclasses. It uses type hints for argument type inference and supports features like default values, help messages, and argument groups. The current version is 2.0.0, and it generally follows an as-needed release cadence for bug fixes and feature enhancements.

pip install argparse-dataclass
INSTALL
IMPORT
SIG · ARGPARSE-DATACLASS
A
argparse-dataclass
devopspythonv2.0.0
Install
1.5s avg
Import
36ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.0.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.038s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.034s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ArgumentParser
from argparse_dataclass import ArgumentParser

Define your CLI arguments using a dataclass, then pass the dataclass type to ArgumentParser to automatically generate and parse arguments. This example demonstrates basic type inference and default values.

from dataclasses import dataclass from argparse_dataclass import ArgumentParser @dataclass class Config: input_file: str output_dir: str = 'output' verbose: bool = False port: int = 8080 def main(): parser = ArgumentParser(Config) config = parser.parse_args(['--input-file', 'data.txt', '--verbose']) print(f"Input: {config.input_file}, Output: {config.output_dir}, Verbose: {config.verbose}, Port: {config.port}") if __name__ == '__main__': main()
Debug
Known issues
breakingIn version 2.0.0, the `ArgumentParser` constructor now expects a dataclass *type* (e.g., `Config`) instead of a dataclass *instance* (e.g., `Config()`).
fix
Change `parser = ArgumentParser(Config())` to `parser = ArgumentParser(Config)`.
affects: 2.0.0 and above
breakingThe internal attribute `argument_settings` (used for configuring argument parsing behavior within a dataclass) was renamed to `_argument_settings` in version 2.0.0 to signify its internal, non-public nature.
fix
Update custom dataclasses to use `_argument_settings` instead of `argument_settings` for field-specific argument configuration.
affects: 2.0.0 and above
gotcha`argparse-dataclass` heavily relies on Python type hints to infer the correct `argparse` argument types (e.g., `str`, `int`, `bool`, `list`). Omitting or providing incorrect type hints can lead to unexpected parsing behavior or errors.
fix
Always provide explicit and correct type hints for all dataclass fields intended to become CLI arguments. For complex types, ensure they are supported or define custom parsers.
affects: All versions
gotchaBoolean dataclass fields automatically generate both `--field` (store `True`) and `--no-field` (store `False`) arguments by default. If you only expect a single flag without its negation, this might be unexpected.
fix
To get a simple store_true/store_false flag, configure the field using `_argument_settings(action='store_true')` or `_argument_settings(action='store_false')` explicitly, or set `boolean_flags=False` when initializing `ArgumentParser`.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'argparse_dataclass'
The 'argparse-dataclass' package is not installed in the Python environment.
fix
Install the package using pip: 'pip install argparse-dataclass'.
ImportError: cannot import name 'dataclass' from 'argparse_dataclass'
Incorrect import statement; 'dataclass' should be imported from Python's standard 'dataclasses' module, not 'argparse_dataclass'.
fix
Use the correct import: 'from dataclasses import dataclass'.
TypeError: 'NoneType' object is not iterable
Attempting to iterate over a 'None' value, possibly due to missing or incorrect command-line arguments.
fix
Ensure all required command-line arguments are provided and correctly parsed.
AttributeError: module 'argparse_dataclass' has no attribute 'ArgumentParser'
Incorrect usage; 'ArgumentParser' is part of Python's standard 'argparse' module, not 'argparse_dataclass'.
fix
Use the correct import: 'from argparse import ArgumentParser'.
ValueError: invalid literal for int() with base 10: 'abc'
A command-line argument expected to be an integer received a non-integer value.
fix
Ensure that the provided argument is a valid integer.
Upgrade
Version history
2.0.0latest on PyPI · released Jun 11, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
13 hits · last 30 days
node
10
OpenAI (training)
1
Resources
argparse-dataclass — pip install argparse-dataclass · libregistry