Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Typehints for argparse.Namespace objects

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

parser = argparse.ArgumentParser() parser.add_argument('--somearg') parsed = parser.parse_args(['--somearg','someval'])  # type: argparse.Namespace the_arg = parsed.somearg  # <- Pycharm complains that parsed object has no attribute 'somearg' 

If I remove the type declaration in the inline comment, PyCharm doesn't complain, but it also doesn't pick up on invalid attributes. For example:

parser = argparse.ArgumentParser() parser.add_argument('--somearg') parsed = parser.parse_args(['--somearg','someval'])  # no typehint the_arg = parsed.somaerg   # <- typo in attribute, but no complaint in PyCharm.  Raises AttributeError when executed. 

Any ideas?


Update

Inspired by Austin's answer below, the simplest solution I could find is one using namedtuples:

from collections import namedtuple ArgNamespace = namedtuple('ArgNamespace', ['some_arg', 'another_arg'])  parser = argparse.ArgumentParser() parser.add_argument('--some-arg') parser.add_argument('--another-arg') parsed = parser.parse_args(['--some-arg', 'val1', '--another-arg', 'val2'])  # type: ArgNamespace  x = parsed.some_arg  # good... y = parsed.another_arg  # still good... z = parsed.aint_no_arg  # Flagged by PyCharm! 

While this is satisfactory, I still don't like having to repeat the argument names. If the argument list grows considerably, it will be tedious updating both locations. What would be ideal is somehow extracting the arguments from the parser object like the following:

parser = argparse.ArgumentParser() parser.add_argument('--some-arg') parser.add_argument('--another-arg') MagicNamespace = parser.magically_extract_namespace() parsed = parser.parse_args(['--some-arg', 'val1', '--another-arg', 'val2'])  # type: MagicNamespace 

I haven't been able to find anything in the argparse module that could make this possible, and I'm still unsure if any static analysis tool could be clever enough to get those values and not bring the IDE to a grinding halt.

Still searching...


Update 2

Per hpaulj's comment, the closest thing I could find to the method described above that would "magically" extract the attributes of the parsed object is something that would extract the dest attribute from each of the parser's _actions.:

parser = argparse.ArgumentParser() parser.add_argument('--some-arg') parser.add_argument('--another-arg') MagicNamespace = namedtuple('MagicNamespace', [act.dest for act in parser._actions]) parsed = parser.parse_args(['--some-arg', 'val1', '--another-arg', 'val2'])  # type: MagicNamespace 

But this still does not cause attribute errors to get flagged in static analysis. This is true also true if I pass namespace=MagicNamespace in the parser.parse_args call.

like image 494
Billy Avatar asked Feb 16 '17 16:02

Billy


2 Answers

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.

like image 124
Jesse Avatar answered Sep 29 '22 21:09

Jesse


Consider defining an extension class to argparse.Namespace that provides the type hints you want:

class MyProgramArgs(argparse.Namespace):     def __init__():         self.somearg = 'defaultval' # type: str 

Then use namespace= to pass that to parse_args:

def process_argv():     parser = argparse.ArgumentParser()     parser.add_argument('--somearg')     nsp = MyProgramArgs()     parsed = parser.parse_args(['--somearg','someval'], namespace=nsp)  # type: MyProgramArgs     the_arg = parsed.somearg  # <- Pycharm should not complain 
like image 31
aghast Avatar answered Sep 29 '22 21:09

aghast