Python: Typehints for argparse.Namespace objects

Typed argument parser was made for exactly this purpose. It wraps argparse. Your example is implemented as:

from tap import Tap


class ArgumentParser(Tap):
    somearg: str


parsed = ArgumentParser().parse_args(['--somearg', 'someval'])
the_arg = parsed.somearg

Here’s a picture of it in action.
enter image description here

It’s on PyPI and can be installed with: pip install typed-argument-parser

Full disclosure: I’m one of the creators of this library.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)