{"record":{"id":"f2bcf6cdd9986f89","repo":"RustPython/RustPython","slug":"required-is-an-invalid-argument-for-positionals","errorCode":null,"errorMessage":"'required' is an invalid argument for positionals","messagePattern":"'required' is an invalid argument for positionals","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Lib/argparse.py","lineNumber":1649,"sourceCode":"                cont = self\n            else:\n                cont = title_group_map[group._container.title]\n            mutex_group = cont.add_mutually_exclusive_group(\n                required=group.required)\n\n            # map the actions to their new mutex group\n            for action in group._group_actions:\n                group_map[action] = mutex_group\n\n        # add all actions to this container or their group\n        for action in container._actions:\n            group_map.get(action, self)._add_action(action)\n\n    def _get_positional_kwargs(self, dest, **kwargs):\n        # make sure required is not specified\n        if 'required' in kwargs:\n            msg = \"'required' is an invalid argument for positionals\"\n            raise TypeError(msg)\n\n        # mark positional arguments as required if at least one is\n        # always required\n        nargs = kwargs.get('nargs')\n        if nargs == 0:\n            raise ValueError('nargs for positionals must be != 0')\n        if nargs not in [OPTIONAL, ZERO_OR_MORE, REMAINDER, SUPPRESS]:\n            kwargs['required'] = True\n\n        # return the keyword arguments with no option strings\n        return dict(kwargs, dest=dest, option_strings=[])\n\n    def _get_optional_kwargs(self, *args, **kwargs):\n        # determine short and long option strings\n        option_strings = []\n        long_option_strings = []\n        for option_string in args:\n            # error on strings that don't start with an appropriate prefix","sourceCodeStart":1631,"sourceCodeEnd":1667,"githubUrl":"https://github.com/RustPython/RustPython/blob/aaeab4f754b4f40efc0c8ab39cf7c4a3c35a8cfd/Lib/argparse.py#L1631-L1667","documentation":"Positional arguments are inherently required whenever they consume a value: argparse derives required from nargs ('?', '*', '...', SUPPRESS make them non-required) and rejects an explicit required= keyword for positionals with this TypeError in _get_positional_kwargs.","triggerScenarios":"parser.add_argument('outfile', required=True); equally required=False on a positional that consumes a value.","commonSituations":"Making a positional optional during CLI evolution by adding required=False instead of nargs='?'; copy-paste from an option definition where required= is legal.","solutions":["Delete required= and let nargs decide; a plain positional is required by default.","To make it optional, use nargs='?' together with default= (e.g. default='out.txt').","To keep explicit required semantics, use an option string: add_argument('-o', '--out', required=True)."],"exampleFix":"# before\nparser.add_argument('outfile', required=True)\n# after\nparser.add_argument('outfile', nargs='?', default='out.txt')","handlingStrategy":"validation","validationCode":"def add_positional(parser, name, **kwargs):\n    if 'required' in kwargs:\n        optional = kwargs.pop('required') is False\n        if optional:\n            kwargs.setdefault('nargs', '?')\n    return parser.add_argument(name, **kwargs)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Remember the rule: positionals are always required; optionality comes from nargs='?' plus default.","Route positional definitions through a wrapper that translates or rejects required=."],"tags":["argparse","positional-arguments","required","typeerror"],"backgroundTag":"invalid-keyword-for-positional","analyzedSha":"aaeab4f754b4f40efc0c8ab39cf7c4a3c35a8cfd","analyzedAt":"2026-08-17T00:37:52.100Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}