diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2024-10-23 07:50:29 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-23 07:50:29 (GMT) |
commit | 834ba5aaf21ac7fd123534dae8f9e478ee526aaa (patch) | |
tree | 3e1351b3c1b061fdfd0d382614db89489cccdd14 /Doc/library | |
parent | c75ff2ef8eb71d91b1f92db9c2bc7ff18c582ab1 (diff) | |
download | cpython-834ba5aaf21ac7fd123534dae8f9e478ee526aaa.zip cpython-834ba5aaf21ac7fd123534dae8f9e478ee526aaa.tar.gz cpython-834ba5aaf21ac7fd123534dae8f9e478ee526aaa.tar.bz2 |
gh-58032: Deprecate the argparse.FileType type converter (GH-124664)
Diffstat (limited to 'Doc/library')
-rw-r--r-- | Doc/library/argparse.rst | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index ef0db3e..65663d4 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -865,16 +865,14 @@ See also :ref:`specifying-ambiguous-arguments`. The supported values are: output files:: >>> parser = argparse.ArgumentParser() - >>> parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), - ... default=sys.stdin) - >>> parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), - ... default=sys.stdout) + >>> parser.add_argument('infile', nargs='?') + >>> parser.add_argument('outfile', nargs='?') >>> parser.parse_args(['input.txt', 'output.txt']) - Namespace(infile=<_io.TextIOWrapper name='input.txt' encoding='UTF-8'>, - outfile=<_io.TextIOWrapper name='output.txt' encoding='UTF-8'>) + Namespace(infile='input.txt', outfile='output.txt') + >>> parser.parse_args(['input.txt']) + Namespace(infile='input.txt', outfile=None) >>> parser.parse_args([]) - Namespace(infile=<_io.TextIOWrapper name='<stdin>' encoding='UTF-8'>, - outfile=<_io.TextIOWrapper name='<stdout>' encoding='UTF-8'>) + Namespace(infile=None, outfile=None) .. index:: single: * (asterisk); in argparse module @@ -1033,7 +1031,6 @@ Common built-in types and functions can be used as type converters: parser.add_argument('distance', type=float) parser.add_argument('street', type=ascii) parser.add_argument('code_point', type=ord) - parser.add_argument('dest_file', type=argparse.FileType('w', encoding='latin-1')) parser.add_argument('datapath', type=pathlib.Path) User defined functions can be used as well: @@ -1827,9 +1824,19 @@ FileType objects >>> parser.parse_args(['-']) Namespace(infile=<_io.TextIOWrapper name='<stdin>' encoding='UTF-8'>) + .. note:: + + If one argument uses *FileType* and then a subsequent argument fails, + an error is reported but the file is not automatically closed. + This can also clobber the output files. + In this case, it would be better to wait until after the parser has + run and then use the :keyword:`with`-statement to manage the files. + .. versionchanged:: 3.4 Added the *encodings* and *errors* parameters. + .. deprecated:: 3.14 + Argument groups ^^^^^^^^^^^^^^^ |