##// END OF EJS Templates
fix docstring for Python 3.13
Matthias Bussonnier -
Show More
@@ -1,166 +1,169 b''
1 """Tests for the decorators we've created for IPython.
1 """Tests for the decorators we've created for IPython.
2 """
2 """
3
3
4 # Module imports
4 # Module imports
5 # Std lib
5 # Std lib
6 import inspect
6 import inspect
7 import sys
7 import sys
8
8
9 # Our own
9 # Our own
10 from IPython.testing import decorators as dec
10 from IPython.testing import decorators as dec
11 from IPython.testing.skipdoctest import skip_doctest
11 from IPython.testing.skipdoctest import skip_doctest
12 from IPython.utils.text import dedent
12
13
13 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
14 # Utilities
15 # Utilities
15
16
16 # Note: copied from OInspect, kept here so the testing stuff doesn't create
17 # Note: copied from OInspect, kept here so the testing stuff doesn't create
17 # circular dependencies and is easier to reuse.
18 # circular dependencies and is easier to reuse.
18 def getargspec(obj):
19 def getargspec(obj):
19 """Get the names and default values of a function's arguments.
20 """Get the names and default values of a function's arguments.
20
21
21 A tuple of four things is returned: (args, varargs, varkw, defaults).
22 A tuple of four things is returned: (args, varargs, varkw, defaults).
22 'args' is a list of the argument names (it may contain nested lists).
23 'args' is a list of the argument names (it may contain nested lists).
23 'varargs' and 'varkw' are the names of the * and ** arguments or None.
24 'varargs' and 'varkw' are the names of the * and ** arguments or None.
24 'defaults' is an n-tuple of the default values of the last n arguments.
25 'defaults' is an n-tuple of the default values of the last n arguments.
25
26
26 Modified version of inspect.getargspec from the Python Standard
27 Modified version of inspect.getargspec from the Python Standard
27 Library."""
28 Library."""
28
29
29 if inspect.isfunction(obj):
30 if inspect.isfunction(obj):
30 func_obj = obj
31 func_obj = obj
31 elif inspect.ismethod(obj):
32 elif inspect.ismethod(obj):
32 func_obj = obj.__func__
33 func_obj = obj.__func__
33 else:
34 else:
34 raise TypeError('arg is not a Python function')
35 raise TypeError('arg is not a Python function')
35 args, varargs, varkw = inspect.getargs(func_obj.__code__)
36 args, varargs, varkw = inspect.getargs(func_obj.__code__)
36 return args, varargs, varkw, func_obj.__defaults__
37 return args, varargs, varkw, func_obj.__defaults__
37
38
38 #-----------------------------------------------------------------------------
39 #-----------------------------------------------------------------------------
39 # Testing functions
40 # Testing functions
40
41
41 @dec.as_unittest
42 @dec.as_unittest
42 def trivial():
43 def trivial():
43 """A trivial test"""
44 """A trivial test"""
44 pass
45 pass
45
46
46
47
47 @dec.skip()
48 @dec.skip()
48 def test_deliberately_broken():
49 def test_deliberately_broken():
49 """A deliberately broken test - we want to skip this one."""
50 """A deliberately broken test - we want to skip this one."""
50 1/0
51 1/0
51
52
52 @dec.skip('Testing the skip decorator')
53 @dec.skip('Testing the skip decorator')
53 def test_deliberately_broken2():
54 def test_deliberately_broken2():
54 """Another deliberately broken test - we want to skip this one."""
55 """Another deliberately broken test - we want to skip this one."""
55 1/0
56 1/0
56
57
57
58
58 # Verify that we can correctly skip the doctest for a function at will, but
59 # Verify that we can correctly skip the doctest for a function at will, but
59 # that the docstring itself is NOT destroyed by the decorator.
60 # that the docstring itself is NOT destroyed by the decorator.
60 @skip_doctest
61 @skip_doctest
61 def doctest_bad(x,y=1,**k):
62 def doctest_bad(x,y=1,**k):
62 """A function whose doctest we need to skip.
63 """A function whose doctest we need to skip.
63
64
64 >>> 1+1
65 >>> 1+1
65 3
66 3
66 """
67 """
67 print('x:',x)
68 print('x:',x)
68 print('y:',y)
69 print('y:',y)
69 print('k:',k)
70 print('k:',k)
70
71
71
72
72 def call_doctest_bad():
73 def call_doctest_bad():
73 """Check that we can still call the decorated functions.
74 """Check that we can still call the decorated functions.
74
75
75 >>> doctest_bad(3,y=4)
76 >>> doctest_bad(3,y=4)
76 x: 3
77 x: 3
77 y: 4
78 y: 4
78 k: {}
79 k: {}
79 """
80 """
80 pass
81 pass
81
82
82
83
83 def test_skip_dt_decorator():
84 def test_skip_dt_decorator():
84 """Doctest-skipping decorator should preserve the docstring.
85 """Doctest-skipping decorator should preserve the docstring.
85 """
86 """
86 # Careful: 'check' must be a *verbatim* copy of the doctest_bad docstring!
87 # Careful: 'check' must be a *verbatim* copy of the doctest_bad docstring!
87 check = """A function whose doctest we need to skip.
88 check = dedent(
89 """A function whose doctest we need to skip.
88
90
89 >>> 1+1
91 >>> 1+1
90 3
92 3
91 """
93 """
94 )
92 # Fetch the docstring from doctest_bad after decoration.
95 # Fetch the docstring from doctest_bad after decoration.
93 val = doctest_bad.__doc__
96 val = doctest_bad.__doc__
94
97
95 assert check == val, "doctest_bad docstrings don't match"
98 assert check == val, "doctest_bad docstrings don't match"
96
99
97
100
98 # Doctest skipping should work for class methods too
101 # Doctest skipping should work for class methods too
99 class FooClass(object):
102 class FooClass(object):
100 """FooClass
103 """FooClass
101
104
102 Example:
105 Example:
103
106
104 >>> 1+1
107 >>> 1+1
105 2
108 2
106 """
109 """
107
110
108 @skip_doctest
111 @skip_doctest
109 def __init__(self,x):
112 def __init__(self,x):
110 """Make a FooClass.
113 """Make a FooClass.
111
114
112 Example:
115 Example:
113
116
114 >>> f = FooClass(3)
117 >>> f = FooClass(3)
115 junk
118 junk
116 """
119 """
117 print('Making a FooClass.')
120 print('Making a FooClass.')
118 self.x = x
121 self.x = x
119
122
120 @skip_doctest
123 @skip_doctest
121 def bar(self,y):
124 def bar(self,y):
122 """Example:
125 """Example:
123
126
124 >>> ff = FooClass(3)
127 >>> ff = FooClass(3)
125 >>> ff.bar(0)
128 >>> ff.bar(0)
126 boom!
129 boom!
127 >>> 1/0
130 >>> 1/0
128 bam!
131 bam!
129 """
132 """
130 return 1/y
133 return 1/y
131
134
132 def baz(self,y):
135 def baz(self,y):
133 """Example:
136 """Example:
134
137
135 >>> ff2 = FooClass(3)
138 >>> ff2 = FooClass(3)
136 Making a FooClass.
139 Making a FooClass.
137 >>> ff2.baz(3)
140 >>> ff2.baz(3)
138 True
141 True
139 """
142 """
140 return self.x==y
143 return self.x==y
141
144
142
145
143 def test_skip_dt_decorator2():
146 def test_skip_dt_decorator2():
144 """Doctest-skipping decorator should preserve function signature.
147 """Doctest-skipping decorator should preserve function signature.
145 """
148 """
146 # Hardcoded correct answer
149 # Hardcoded correct answer
147 dtargs = (['x', 'y'], None, 'k', (1,))
150 dtargs = (['x', 'y'], None, 'k', (1,))
148 # Introspect out the value
151 # Introspect out the value
149 dtargsr = getargspec(doctest_bad)
152 dtargsr = getargspec(doctest_bad)
150 assert dtargsr==dtargs, \
153 assert dtargsr==dtargs, \
151 "Incorrectly reconstructed args for doctest_bad: %s" % (dtargsr,)
154 "Incorrectly reconstructed args for doctest_bad: %s" % (dtargsr,)
152
155
153
156
154 @dec.skip_linux
157 @dec.skip_linux
155 def test_linux():
158 def test_linux():
156 assert sys.platform.startswith("linux") is False, "This test can't run under linux"
159 assert sys.platform.startswith("linux") is False, "This test can't run under linux"
157
160
158
161
159 @dec.skip_win32
162 @dec.skip_win32
160 def test_win32():
163 def test_win32():
161 assert sys.platform != "win32", "This test can't run under windows"
164 assert sys.platform != "win32", "This test can't run under windows"
162
165
163
166
164 @dec.skip_osx
167 @dec.skip_osx
165 def test_osx():
168 def test_osx():
166 assert sys.platform != "darwin", "This test can't run under osx"
169 assert sys.platform != "darwin", "This test can't run under osx"
General Comments 0
You need to be logged in to leave comments. Login now