Python: Typehints for argparse.Namespace objects

前端 未结 3 1968
悲哀的现实
悲哀的现实 2020-12-14 06:39

Is there a way to have Python static analyzers (e.g. in PyCharm, other IDEs) pick up on Typehints on argparse.Namespace objects? Example:

parse         


        
3条回答
  •  温柔的废话
    2020-12-14 07:14

    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.

    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.

提交回复
热议问题