@@ -1773,27 +1773,43 @@ def convert_arg_line_to_args(self, arg_line):
17731773# Type conversion tests
17741774# =====================
17751775
1776+ def FileType (* args , ** kwargs ):
1777+ with warnings .catch_warnings ():
1778+ warnings .filterwarnings ('ignore' , 'FileType is deprecated' ,
1779+ PendingDeprecationWarning , __name__ )
1780+ return argparse .FileType (* args , ** kwargs )
1781+
1782+
1783+ class TestFileTypeDeprecation (TestCase ):
1784+
1785+ def test (self ):
1786+ with self .assertWarns (PendingDeprecationWarning ) as cm :
1787+ argparse .FileType ()
1788+ self .assertIn ('FileType is deprecated' , str (cm .warning ))
1789+ self .assertEqual (cm .filename , __file__ )
1790+
1791+
17761792class TestFileTypeRepr (TestCase ):
17771793
17781794 def test_r (self ):
1779- type = argparse . FileType ('r' )
1795+ type = FileType ('r' )
17801796 self .assertEqual ("FileType('r')" , repr (type ))
17811797
17821798 def test_wb_1 (self ):
1783- type = argparse . FileType ('wb' , 1 )
1799+ type = FileType ('wb' , 1 )
17841800 self .assertEqual ("FileType('wb', 1)" , repr (type ))
17851801
17861802 def test_r_latin (self ):
1787- type = argparse . FileType ('r' , encoding = 'latin_1' )
1803+ type = FileType ('r' , encoding = 'latin_1' )
17881804 self .assertEqual ("FileType('r', encoding='latin_1')" , repr (type ))
17891805
17901806 def test_w_big5_ignore (self ):
1791- type = argparse . FileType ('w' , encoding = 'big5' , errors = 'ignore' )
1807+ type = FileType ('w' , encoding = 'big5' , errors = 'ignore' )
17921808 self .assertEqual ("FileType('w', encoding='big5', errors='ignore')" ,
17931809 repr (type ))
17941810
17951811 def test_r_1_replace (self ):
1796- type = argparse . FileType ('r' , 1 , errors = 'replace' )
1812+ type = FileType ('r' , 1 , errors = 'replace' )
17971813 self .assertEqual ("FileType('r', 1, errors='replace')" , repr (type ))
17981814
17991815
@@ -1847,7 +1863,6 @@ def __eq__(self, other):
18471863 text = text .decode ('ascii' )
18481864 return self .name == other .name == text
18491865
1850-
18511866class TestFileTypeR (TempDirMixin , ParserTestCase ):
18521867 """Test the FileType option/argument type for reading files"""
18531868
@@ -1860,8 +1875,8 @@ def setUp(self):
18601875 self .create_readonly_file ('readonly' )
18611876
18621877 argument_signatures = [
1863- Sig ('-x' , type = argparse . FileType ()),
1864- Sig ('spam' , type = argparse . FileType ('r' )),
1878+ Sig ('-x' , type = FileType ()),
1879+ Sig ('spam' , type = FileType ('r' )),
18651880 ]
18661881 failures = ['-x' , '' , 'non-existent-file.txt' ]
18671882 successes = [
@@ -1881,7 +1896,7 @@ def setUp(self):
18811896 file .close ()
18821897
18831898 argument_signatures = [
1884- Sig ('-c' , type = argparse . FileType ('r' ), default = 'no-file.txt' ),
1899+ Sig ('-c' , type = FileType ('r' ), default = 'no-file.txt' ),
18851900 ]
18861901 # should provoke no such file error
18871902 failures = ['' ]
@@ -1900,8 +1915,8 @@ def setUp(self):
19001915 file .write (file_name )
19011916
19021917 argument_signatures = [
1903- Sig ('-x' , type = argparse . FileType ('rb' )),
1904- Sig ('spam' , type = argparse . FileType ('rb' )),
1918+ Sig ('-x' , type = FileType ('rb' )),
1919+ Sig ('spam' , type = FileType ('rb' )),
19051920 ]
19061921 failures = ['-x' , '' ]
19071922 successes = [
@@ -1939,8 +1954,8 @@ def setUp(self):
19391954 self .create_writable_file ('writable' )
19401955
19411956 argument_signatures = [
1942- Sig ('-x' , type = argparse . FileType ('w' )),
1943- Sig ('spam' , type = argparse . FileType ('w' )),
1957+ Sig ('-x' , type = FileType ('w' )),
1958+ Sig ('spam' , type = FileType ('w' )),
19441959 ]
19451960 failures = ['-x' , '' , 'readonly' ]
19461961 successes = [
@@ -1962,8 +1977,8 @@ def setUp(self):
19621977 self .create_writable_file ('writable' )
19631978
19641979 argument_signatures = [
1965- Sig ('-x' , type = argparse . FileType ('x' )),
1966- Sig ('spam' , type = argparse . FileType ('x' )),
1980+ Sig ('-x' , type = FileType ('x' )),
1981+ Sig ('spam' , type = FileType ('x' )),
19671982 ]
19681983 failures = ['-x' , '' , 'readonly' , 'writable' ]
19691984 successes = [
@@ -1977,8 +1992,8 @@ class TestFileTypeWB(TempDirMixin, ParserTestCase):
19771992 """Test the FileType option/argument type for writing binary files"""
19781993
19791994 argument_signatures = [
1980- Sig ('-x' , type = argparse . FileType ('wb' )),
1981- Sig ('spam' , type = argparse . FileType ('wb' )),
1995+ Sig ('-x' , type = FileType ('wb' )),
1996+ Sig ('spam' , type = FileType ('wb' )),
19821997 ]
19831998 failures = ['-x' , '' ]
19841999 successes = [
@@ -1994,8 +2009,8 @@ class TestFileTypeXB(TestFileTypeX):
19942009 "Test the FileType option/argument type for writing new binary files only"
19952010
19962011 argument_signatures = [
1997- Sig ('-x' , type = argparse . FileType ('xb' )),
1998- Sig ('spam' , type = argparse . FileType ('xb' )),
2012+ Sig ('-x' , type = FileType ('xb' )),
2013+ Sig ('spam' , type = FileType ('xb' )),
19992014 ]
20002015 successes = [
20012016 ('-x foo bar' , NS (x = WFile ('foo' ), spam = WFile ('bar' ))),
@@ -2007,7 +2022,7 @@ class TestFileTypeOpenArgs(TestCase):
20072022 """Test that open (the builtin) is correctly called"""
20082023
20092024 def test_open_args (self ):
2010- FT = argparse . FileType
2025+ FT = FileType
20112026 cases = [
20122027 (FT ('rb' ), ('rb' , - 1 , None , None )),
20132028 (FT ('w' , 1 ), ('w' , 1 , None , None )),
@@ -2022,7 +2037,7 @@ def test_open_args(self):
20222037
20232038 def test_invalid_file_type (self ):
20242039 with self .assertRaises (ValueError ):
2025- argparse . FileType ('b' )('-test' )
2040+ FileType ('b' )('-test' )
20262041
20272042
20282043class TestFileTypeMissingInitialization (TestCase ):
0 commit comments