@@ -1773,27 +1773,43 @@ def convert_arg_line_to_args(self, arg_line):
1773
1773
# Type conversion tests
1774
1774
# =====================
1775
1775
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
+
1776
1792
class TestFileTypeRepr (TestCase ):
1777
1793
1778
1794
def test_r (self ):
1779
- type = argparse . FileType ('r' )
1795
+ type = FileType ('r' )
1780
1796
self .assertEqual ("FileType('r')" , repr (type ))
1781
1797
1782
1798
def test_wb_1 (self ):
1783
- type = argparse . FileType ('wb' , 1 )
1799
+ type = FileType ('wb' , 1 )
1784
1800
self .assertEqual ("FileType('wb', 1)" , repr (type ))
1785
1801
1786
1802
def test_r_latin (self ):
1787
- type = argparse . FileType ('r' , encoding = 'latin_1' )
1803
+ type = FileType ('r' , encoding = 'latin_1' )
1788
1804
self .assertEqual ("FileType('r', encoding='latin_1')" , repr (type ))
1789
1805
1790
1806
def test_w_big5_ignore (self ):
1791
- type = argparse . FileType ('w' , encoding = 'big5' , errors = 'ignore' )
1807
+ type = FileType ('w' , encoding = 'big5' , errors = 'ignore' )
1792
1808
self .assertEqual ("FileType('w', encoding='big5', errors='ignore')" ,
1793
1809
repr (type ))
1794
1810
1795
1811
def test_r_1_replace (self ):
1796
- type = argparse . FileType ('r' , 1 , errors = 'replace' )
1812
+ type = FileType ('r' , 1 , errors = 'replace' )
1797
1813
self .assertEqual ("FileType('r', 1, errors='replace')" , repr (type ))
1798
1814
1799
1815
@@ -1847,7 +1863,6 @@ def __eq__(self, other):
1847
1863
text = text .decode ('ascii' )
1848
1864
return self .name == other .name == text
1849
1865
1850
-
1851
1866
class TestFileTypeR (TempDirMixin , ParserTestCase ):
1852
1867
"""Test the FileType option/argument type for reading files"""
1853
1868
@@ -1860,8 +1875,8 @@ def setUp(self):
1860
1875
self .create_readonly_file ('readonly' )
1861
1876
1862
1877
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' )),
1865
1880
]
1866
1881
failures = ['-x' , '' , 'non-existent-file.txt' ]
1867
1882
successes = [
@@ -1881,7 +1896,7 @@ def setUp(self):
1881
1896
file .close ()
1882
1897
1883
1898
argument_signatures = [
1884
- Sig ('-c' , type = argparse . FileType ('r' ), default = 'no-file.txt' ),
1899
+ Sig ('-c' , type = FileType ('r' ), default = 'no-file.txt' ),
1885
1900
]
1886
1901
# should provoke no such file error
1887
1902
failures = ['' ]
@@ -1900,8 +1915,8 @@ def setUp(self):
1900
1915
file .write (file_name )
1901
1916
1902
1917
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' )),
1905
1920
]
1906
1921
failures = ['-x' , '' ]
1907
1922
successes = [
@@ -1939,8 +1954,8 @@ def setUp(self):
1939
1954
self .create_writable_file ('writable' )
1940
1955
1941
1956
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' )),
1944
1959
]
1945
1960
failures = ['-x' , '' , 'readonly' ]
1946
1961
successes = [
@@ -1962,8 +1977,8 @@ def setUp(self):
1962
1977
self .create_writable_file ('writable' )
1963
1978
1964
1979
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' )),
1967
1982
]
1968
1983
failures = ['-x' , '' , 'readonly' , 'writable' ]
1969
1984
successes = [
@@ -1977,8 +1992,8 @@ class TestFileTypeWB(TempDirMixin, ParserTestCase):
1977
1992
"""Test the FileType option/argument type for writing binary files"""
1978
1993
1979
1994
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' )),
1982
1997
]
1983
1998
failures = ['-x' , '' ]
1984
1999
successes = [
@@ -1994,8 +2009,8 @@ class TestFileTypeXB(TestFileTypeX):
1994
2009
"Test the FileType option/argument type for writing new binary files only"
1995
2010
1996
2011
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' )),
1999
2014
]
2000
2015
successes = [
2001
2016
('-x foo bar' , NS (x = WFile ('foo' ), spam = WFile ('bar' ))),
@@ -2007,7 +2022,7 @@ class TestFileTypeOpenArgs(TestCase):
2007
2022
"""Test that open (the builtin) is correctly called"""
2008
2023
2009
2024
def test_open_args (self ):
2010
- FT = argparse . FileType
2025
+ FT = FileType
2011
2026
cases = [
2012
2027
(FT ('rb' ), ('rb' , - 1 , None , None )),
2013
2028
(FT ('w' , 1 ), ('w' , 1 , None , None )),
@@ -2022,7 +2037,7 @@ def test_open_args(self):
2022
2037
2023
2038
def test_invalid_file_type (self ):
2024
2039
with self .assertRaises (ValueError ):
2025
- argparse . FileType ('b' )('-test' )
2040
+ FileType ('b' )('-test' )
2026
2041
2027
2042
2028
2043
class TestFileTypeMissingInitialization (TestCase ):
0 commit comments