2
2
3
3
import platform
4
4
import sys
5
+ from contextlib import contextmanager
5
6
6
7
import pytest
7
8
12
13
IS_PYPY = platform .python_implementation () == 'PyPy'
13
14
14
15
16
+ @contextmanager
17
+ def overwrite_file (path , content ):
18
+ current_content = path .read_bytes () if path .exists () else None
19
+ try :
20
+ path .write_text (content , encoding = 'utf-8' )
21
+ yield
22
+ finally :
23
+ if current_content is not None :
24
+ path .write_bytes (current_content )
25
+ else :
26
+ path .unlink ()
27
+
28
+
15
29
@pytest .mark .sphinx ('html' , testroot = 'ext-autodoc' )
16
30
def test_autoclass_content_class (app ):
17
31
app .config .autoclass_content = 'class'
@@ -947,7 +961,8 @@ def test_autodoc_typehints_none_for_overload(app):
947
961
948
962
949
963
@pytest .mark .sphinx ('text' , testroot = 'ext-autodoc' ,
950
- confoverrides = {'autodoc_typehints' : "description" })
964
+ confoverrides = {'autodoc_typehints' : "description" },
965
+ freshenv = True )
951
966
def test_autodoc_typehints_description (app ):
952
967
app .build ()
953
968
context = (app .outdir / 'index.txt' ).read_text (encoding = 'utf8' )
@@ -986,20 +1001,19 @@ def test_autodoc_typehints_description_no_undoc(app):
986
1001
# No :type: or :rtype: will be injected for `incr`, which does not have
987
1002
# a description for its parameters or its return. `tuple_args` does
988
1003
# describe them, so :type: and :rtype: will be added.
989
- (app .srcdir / 'index.rst' ).write_text (
990
- '.. autofunction:: target.typehints.incr\n '
991
- '\n '
992
- '.. autofunction:: target.typehints.decr\n '
993
- '\n '
994
- ' :returns: decremented number\n '
995
- '\n '
996
- '.. autofunction:: target.typehints.tuple_args\n '
997
- '\n '
998
- ' :param x: arg\n '
999
- ' :return: another tuple\n ' ,
1000
- encoding = 'utf8' ,
1001
- )
1002
- app .build ()
1004
+ with overwrite_file (app .srcdir / 'index.rst' ,
1005
+ '.. autofunction:: target.typehints.incr\n '
1006
+ '\n '
1007
+ '.. autofunction:: target.typehints.decr\n '
1008
+ '\n '
1009
+ ' :returns: decremented number\n '
1010
+ '\n '
1011
+ '.. autofunction:: target.typehints.tuple_args\n '
1012
+ '\n '
1013
+ ' :param x: arg\n '
1014
+ ' :return: another tuple\n ' ):
1015
+ app .build ()
1016
+ # Restore the original content of the file
1003
1017
context = (app .outdir / 'index.txt' ).read_text (encoding = 'utf8' )
1004
1018
assert ('target.typehints.incr(a, b=1)\n '
1005
1019
'\n '
@@ -1033,26 +1047,24 @@ def test_autodoc_typehints_description_no_undoc_doc_rtype(app):
1033
1047
# autodoc_typehints_description_target. `tuple_args` does describe both, so
1034
1048
# :type: and :rtype: will be added. `nothing` has no parameters but a return
1035
1049
# type of None, which will be added.
1036
- (app .srcdir / 'index.rst' ).write_text (
1037
- '.. autofunction:: target.typehints.incr\n '
1038
- '\n '
1039
- '.. autofunction:: target.typehints.decr\n '
1040
- '\n '
1041
- ' :returns: decremented number\n '
1042
- '\n '
1043
- '.. autofunction:: target.typehints.tuple_args\n '
1044
- '\n '
1045
- ' :param x: arg\n '
1046
- ' :return: another tuple\n '
1047
- '\n '
1048
- '.. autofunction:: target.typehints.Math.nothing\n '
1049
- '\n '
1050
- '.. autofunction:: target.typehints.Math.horse\n '
1051
- '\n '
1052
- ' :return: nothing\n ' ,
1053
- encoding = 'utf8' ,
1054
- )
1055
- app .build ()
1050
+ with overwrite_file (app .srcdir / 'index.rst' ,
1051
+ '.. autofunction:: target.typehints.incr\n '
1052
+ '\n '
1053
+ '.. autofunction:: target.typehints.decr\n '
1054
+ '\n '
1055
+ ' :returns: decremented number\n '
1056
+ '\n '
1057
+ '.. autofunction:: target.typehints.tuple_args\n '
1058
+ '\n '
1059
+ ' :param x: arg\n '
1060
+ ' :return: another tuple\n '
1061
+ '\n '
1062
+ '.. autofunction:: target.typehints.Math.nothing\n '
1063
+ '\n '
1064
+ '.. autofunction:: target.typehints.Math.horse\n '
1065
+ '\n '
1066
+ ' :return: nothing\n ' ):
1067
+ app .build ()
1056
1068
context = (app .outdir / 'index.txt' ).read_text (encoding = 'utf8' )
1057
1069
assert context == (
1058
1070
'target.typehints.incr(a, b=1)\n '
@@ -1094,12 +1106,10 @@ def test_autodoc_typehints_description_no_undoc_doc_rtype(app):
1094
1106
@pytest .mark .sphinx ('text' , testroot = 'ext-autodoc' ,
1095
1107
confoverrides = {'autodoc_typehints' : "description" })
1096
1108
def test_autodoc_typehints_description_with_documented_init (app ):
1097
- (app .srcdir / 'index.rst' ).write_text (
1098
- '.. autoclass:: target.typehints._ClassWithDocumentedInit\n '
1099
- ' :special-members: __init__\n ' ,
1100
- encoding = 'utf8' ,
1101
- )
1102
- app .build ()
1109
+ with overwrite_file (app .srcdir / 'index.rst' ,
1110
+ '.. autoclass:: target.typehints._ClassWithDocumentedInit\n '
1111
+ ' :special-members: __init__\n ' ):
1112
+ app .build ()
1103
1113
context = (app .outdir / 'index.txt' ).read_text (encoding = 'utf8' )
1104
1114
assert context == (
1105
1115
'class target.typehints._ClassWithDocumentedInit(x, *args, **kwargs)\n '
@@ -1133,12 +1143,10 @@ def test_autodoc_typehints_description_with_documented_init(app):
1133
1143
confoverrides = {'autodoc_typehints' : "description" ,
1134
1144
'autodoc_typehints_description_target' : 'documented' })
1135
1145
def test_autodoc_typehints_description_with_documented_init_no_undoc (app ):
1136
- (app .srcdir / 'index.rst' ).write_text (
1137
- '.. autoclass:: target.typehints._ClassWithDocumentedInit\n '
1138
- ' :special-members: __init__\n ' ,
1139
- encoding = 'utf8' ,
1140
- )
1141
- app .build ()
1146
+ with overwrite_file (app .srcdir / 'index.rst' ,
1147
+ '.. autoclass:: target.typehints._ClassWithDocumentedInit\n '
1148
+ ' :special-members: __init__\n ' ):
1149
+ app .build ()
1142
1150
context = (app .outdir / 'index.txt' ).read_text (encoding = 'utf8' )
1143
1151
assert context == (
1144
1152
'class target.typehints._ClassWithDocumentedInit(x, *args, **kwargs)\n '
@@ -1165,12 +1173,10 @@ def test_autodoc_typehints_description_with_documented_init_no_undoc_doc_rtype(a
1165
1173
# see test_autodoc_typehints_description_with_documented_init_no_undoc
1166
1174
# returnvalue_and_documented_params should not change class or method
1167
1175
# docstring.
1168
- (app .srcdir / 'index.rst' ).write_text (
1169
- '.. autoclass:: target.typehints._ClassWithDocumentedInit\n '
1170
- ' :special-members: __init__\n ' ,
1171
- encoding = 'utf8' ,
1172
- )
1173
- app .build ()
1176
+ with overwrite_file (app .srcdir / 'index.rst' ,
1177
+ '.. autoclass:: target.typehints._ClassWithDocumentedInit\n '
1178
+ ' :special-members: __init__\n ' ):
1179
+ app .build ()
1174
1180
context = (app .outdir / 'index.txt' ).read_text (encoding = 'utf8' )
1175
1181
assert context == (
1176
1182
'class target.typehints._ClassWithDocumentedInit(x, *args, **kwargs)\n '
@@ -1200,15 +1206,13 @@ def test_autodoc_typehints_description_for_invalid_node(app):
1200
1206
@pytest .mark .sphinx ('text' , testroot = 'ext-autodoc' ,
1201
1207
confoverrides = {'autodoc_typehints' : "both" })
1202
1208
def test_autodoc_typehints_both (app ):
1203
- (app .srcdir / 'index.rst' ).write_text (
1204
- '.. autofunction:: target.typehints.incr\n '
1205
- '\n '
1206
- '.. autofunction:: target.typehints.tuple_args\n '
1207
- '\n '
1208
- '.. autofunction:: target.overload.sum\n ' ,
1209
- encoding = 'utf8' ,
1210
- )
1211
- app .build ()
1209
+ with overwrite_file (app .srcdir / 'index.rst' ,
1210
+ '.. autofunction:: target.typehints.incr\n '
1211
+ '\n '
1212
+ '.. autofunction:: target.typehints.tuple_args\n '
1213
+ '\n '
1214
+ '.. autofunction:: target.overload.sum\n ' ):
1215
+ app .build ()
1212
1216
context = (app .outdir / 'index.txt' ).read_text (encoding = 'utf8' )
1213
1217
assert ('target.typehints.incr(a: int, b: int = 1) -> int\n '
1214
1218
'\n '
@@ -1387,8 +1391,9 @@ def test_autodoc_type_aliases(app):
1387
1391
confoverrides = {'autodoc_typehints' : "description" ,
1388
1392
'autodoc_type_aliases' : {'myint' : 'myint' }})
1389
1393
def test_autodoc_typehints_description_and_type_aliases (app ):
1390
- (app .srcdir / 'autodoc_type_aliases.rst' ).write_text ('.. autofunction:: target.autodoc_type_aliases.sum' , encoding = 'utf8' )
1391
- app .build ()
1394
+ with overwrite_file (app .srcdir / 'autodoc_type_aliases.rst' ,
1395
+ '.. autofunction:: target.autodoc_type_aliases.sum' ):
1396
+ app .build ()
1392
1397
context = (app .outdir / 'autodoc_type_aliases.txt' ).read_text (encoding = 'utf8' )
1393
1398
assert context == (
1394
1399
'target.autodoc_type_aliases.sum(x, y)\n '
0 commit comments