Skip to content
Prev Previous commit
Next Next commit
Emit a PendingDeprecationWarning.
  • Loading branch information
serhiy-storchaka committed Oct 10, 2024
commit 6c9660f6efa9206ad1b5322b0f0a5a6be418bae3
6 changes: 6 additions & 0 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,12 @@ class FileType(object):
"""

def __init__(self, mode='r', bufsize=-1, encoding=None, errors=None):
import warnings
warnings.warn(
"FileType is deprecated. Simply open files after parsing arguments.",
category=PendingDeprecationWarning,
stacklevel=2
)
self._mode = mode
self._bufsize = bufsize
self._encoding = encoding
Expand Down
58 changes: 37 additions & 21 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1744,27 +1744,44 @@ def convert_arg_line_to_args(self, arg_line):
# Type conversion tests
# =====================

def FileType(*args, **kwargs):
with warnings.catch_warnings():
warnings.filterwarnings('ignore', 'FileType is deprecated',
PendingDeprecationWarning, __name__)
return argparse.FileType(*args, **kwargs)


class TestFileTypeDeprecation(TestCase):

def test(self):
parser = argparse.ArgumentParser()
Comment thread
serhiy-storchaka marked this conversation as resolved.
Outdated
with self.assertWarns(PendingDeprecationWarning) as cm:
argparse.FileType()
self.assertIn('FileType is deprecated', str(cm.warning))
self.assertEqual(cm.filename, __file__)


class TestFileTypeRepr(TestCase):

def test_r(self):
type = argparse.FileType('r')
type = FileType('r')
self.assertEqual("FileType('r')", repr(type))

def test_wb_1(self):
type = argparse.FileType('wb', 1)
type = FileType('wb', 1)
self.assertEqual("FileType('wb', 1)", repr(type))

def test_r_latin(self):
type = argparse.FileType('r', encoding='latin_1')
type = FileType('r', encoding='latin_1')
self.assertEqual("FileType('r', encoding='latin_1')", repr(type))

def test_w_big5_ignore(self):
type = argparse.FileType('w', encoding='big5', errors='ignore')
type = FileType('w', encoding='big5', errors='ignore')
self.assertEqual("FileType('w', encoding='big5', errors='ignore')",
repr(type))

def test_r_1_replace(self):
type = argparse.FileType('r', 1, errors='replace')
type = FileType('r', 1, errors='replace')
self.assertEqual("FileType('r', 1, errors='replace')", repr(type))


Expand Down Expand Up @@ -1818,7 +1835,6 @@ def __eq__(self, other):
text = text.decode('ascii')
return self.name == other.name == text


class TestFileTypeR(TempDirMixin, ParserTestCase):
"""Test the FileType option/argument type for reading files"""

Expand All @@ -1831,8 +1847,8 @@ def setUp(self):
self.create_readonly_file('readonly')

argument_signatures = [
Sig('-x', type=argparse.FileType()),
Sig('spam', type=argparse.FileType('r')),
Sig('-x', type=FileType()),
Sig('spam', type=FileType('r')),
]
failures = ['-x', '', 'non-existent-file.txt']
successes = [
Expand All @@ -1852,7 +1868,7 @@ def setUp(self):
file.close()

argument_signatures = [
Sig('-c', type=argparse.FileType('r'), default='no-file.txt'),
Sig('-c', type=FileType('r'), default='no-file.txt'),
]
# should provoke no such file error
failures = ['']
Expand All @@ -1871,8 +1887,8 @@ def setUp(self):
file.write(file_name)

argument_signatures = [
Sig('-x', type=argparse.FileType('rb')),
Sig('spam', type=argparse.FileType('rb')),
Sig('-x', type=FileType('rb')),
Sig('spam', type=FileType('rb')),
]
failures = ['-x', '']
successes = [
Expand Down Expand Up @@ -1910,8 +1926,8 @@ def setUp(self):
self.create_writable_file('writable')

argument_signatures = [
Sig('-x', type=argparse.FileType('w')),
Sig('spam', type=argparse.FileType('w')),
Sig('-x', type=FileType('w')),
Sig('spam', type=FileType('w')),
]
failures = ['-x', '', 'readonly']
successes = [
Expand All @@ -1933,8 +1949,8 @@ def setUp(self):
self.create_writable_file('writable')

argument_signatures = [
Sig('-x', type=argparse.FileType('x')),
Sig('spam', type=argparse.FileType('x')),
Sig('-x', type=FileType('x')),
Sig('spam', type=FileType('x')),
]
failures = ['-x', '', 'readonly', 'writable']
successes = [
Expand All @@ -1948,8 +1964,8 @@ class TestFileTypeWB(TempDirMixin, ParserTestCase):
"""Test the FileType option/argument type for writing binary files"""

argument_signatures = [
Sig('-x', type=argparse.FileType('wb')),
Sig('spam', type=argparse.FileType('wb')),
Sig('-x', type=FileType('wb')),
Sig('spam', type=FileType('wb')),
]
failures = ['-x', '']
successes = [
Expand All @@ -1965,8 +1981,8 @@ class TestFileTypeXB(TestFileTypeX):
"Test the FileType option/argument type for writing new binary files only"

argument_signatures = [
Sig('-x', type=argparse.FileType('xb')),
Sig('spam', type=argparse.FileType('xb')),
Sig('-x', type=FileType('xb')),
Sig('spam', type=FileType('xb')),
]
successes = [
('-x foo bar', NS(x=WFile('foo'), spam=WFile('bar'))),
Expand All @@ -1978,7 +1994,7 @@ class TestFileTypeOpenArgs(TestCase):
"""Test that open (the builtin) is correctly called"""

def test_open_args(self):
FT = argparse.FileType
FT = FileType
cases = [
(FT('rb'), ('rb', -1, None, None)),
(FT('w', 1), ('w', 1, None, None)),
Expand All @@ -1993,7 +2009,7 @@ def test_open_args(self):

def test_invalid_file_type(self):
with self.assertRaises(ValueError):
argparse.FileType('b')('-test')
FileType('b')('-test')


class TestFileTypeMissingInitialization(TestCase):
Expand Down