{"record":{"id":"02a1bc8e524851ec","repo":"RustPython/RustPython","slug":"argument-with-mode-r","errorCode":null,"errorMessage":"argument \"-\" with mode %r","messagePattern":"argument \"-\" with mode %r","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Lib/argparse.py","lineNumber":1376,"sourceCode":"            \"FileType is deprecated. Simply open files after parsing arguments.\",\n            category=PendingDeprecationWarning,\n            stacklevel=2\n        )\n        self._mode = mode\n        self._bufsize = bufsize\n        self._encoding = encoding\n        self._errors = errors\n\n    def __call__(self, string):\n        # the special argument \"-\" means sys.std{in,out}\n        if string == '-':\n            if 'r' in self._mode:\n                return _sys.stdin.buffer if 'b' in self._mode else _sys.stdin\n            elif any(c in self._mode for c in 'wax'):\n                return _sys.stdout.buffer if 'b' in self._mode else _sys.stdout\n            else:\n                msg = _('argument \"-\" with mode %r') % self._mode\n                raise ValueError(msg)\n\n        # all other arguments are used as file names\n        try:\n            return open(string, self._mode, self._bufsize, self._encoding,\n                        self._errors)\n        except OSError as e:\n            args = {'filename': string, 'error': e}\n            message = _(\"can't open '%(filename)s': %(error)s\")\n            raise ArgumentTypeError(message % args)\n\n    def __repr__(self):\n        args = self._mode, self._bufsize\n        kwargs = [('encoding', self._encoding), ('errors', self._errors)]\n        args_str = ', '.join([repr(arg) for arg in args if arg != -1] +\n                             ['%s=%r' % (kw, arg) for kw, arg in kwargs\n                              if arg is not None])\n        return '%s(%s)' % (type(self).__name__, args_str)\n","sourceCodeStart":1358,"sourceCodeEnd":1394,"githubUrl":"https://github.com/RustPython/RustPython/blob/aaeab4f754b4f40efc0c8ab39cf7c4a3c35a8cfd/Lib/argparse.py#L1358-L1394","documentation":"FileType maps the special filename '-' to a standard stream: stdin when the mode contains 'r', stdout when it contains 'w', 'a', or 'x'. If the mode contains none of these characters, no stream can be selected, and __call__ raises this ValueError when '-' is passed on the command line for that argument.","triggerScenarios":"argparse.FileType(mode) where the mode has no r/w/a/x — e.g. type=argparse.FileType('b') or an empty mode — combined with the user passing '-'.","commonSituations":"Mode strings assembled from flags or configuration where the direction letter got dropped; passing a file extension or bare binary flag where a mode string is expected.","solutions":["Fix the mode to include a direction character, e.g. 'rb', 'wb', or 'ab'.","If the standard-stream shortcut is not wanted for this argument, reject '-' explicitly with a custom type function instead of FileType."],"exampleFix":"# before\nparser.add_argument('--log', type=argparse.FileType('b'))\n# after\nparser.add_argument('--log', type=argparse.FileType('ab'))","handlingStrategy":"validation","validationCode":"def make_file_type(mode, bufsize=-1, encoding=None, errors=None):\n    if 'r' not in mode and not any(c in mode for c in 'wax'):\n        raise ValueError('mode cannot map dash to a standard stream: ' + repr(mode))\n    return argparse.FileType(mode, bufsize, encoding, errors)","typeGuard":"def supports_dash(mode: str) -> bool:\n    return 'r' in mode or any(c in mode for c in 'wax')","tryCatchPattern":null,"preventionTips":["Define mode strings as named constants ('rb', 'wb') instead of building them dynamically.","Add a unit test that parses '-' for every FileType argument in the CLI."],"tags":["argparse","filetype","file-mode","stdin","stdout","valueerror"],"backgroundTag":"invalid-file-mode","analyzedSha":"aaeab4f754b4f40efc0c8ab39cf7c4a3c35a8cfd","analyzedAt":"2026-08-17T00:37:52.100Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}