Description
in an attempt to make an attrs-decorated class programmatically from the output of the docopt argument parser, I discovered a situation where it is possible to create an attrs class whose attributes appear to have names that contain invalid characters.
In the code below, input_args
, the instance of the class InputArgs
, contains an attribute ostensibly named print-config
, and I have no idea how to access the value of that attribute later, since it gets tokenized to input_args.print - config
, which attempts to subtract an unintialized variable from a nonexistent attribute. The problem, I think, is that print-config
is a perfectly valid string key in a dict, but it's an invalid attribute name.
I'm happy enough to sanitize things on my end, but I feel like this is a small example of a general issue that I don't understand enough of.
import attr
def from_docopt_dict(docopt_dict):
# sanitize the keys: can't start with any instances of '-', like how every key in this dict starts.
for key in docopt_dict:
clean = key.lstrip('-')
docopt_dict[clean] = docopt_dict.pop(key)
# make a class out of it
retval = attr.make_class(name='InputArgs', attrs=docopt_dict)()
return retval
if __name__ == "__main__":
docopt_dict= {'--config': None,
'--help': False,
'--input': 'C:\\\\Users\\\\Foo\\\\Documents',
'--output': None,
'--print-config': False,
'--verbose': False,
'--version': False}
input_args = from_docopt_dict(docopt_dict)
print("dict:")
print(docopt_dict)
print(input_args)