-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_docstrings.py
More file actions
579 lines (530 loc) · 19.9 KB
/
test_docstrings.py
File metadata and controls
579 lines (530 loc) · 19.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
import logging
from textwrap import dedent
import lark
import pytest
from docstub._analysis import PyImport
from docstub._docstrings import (
Annotation,
DocstringAnnotations,
DoctypeTransformer,
)
class Test_Annotation:
def test_str(self):
annotation = Annotation(
value="Path",
imports=frozenset({PyImport(import_="Path", from_="pathlib")}),
)
assert str(annotation) == annotation.value
def test_as_return_tuple(self):
path_anno = Annotation(
value="Path",
imports=frozenset({PyImport(import_="Path", from_="pathlib")}),
)
sequence_anno = Annotation(
value="Sequence",
imports=frozenset({PyImport(import_="Sequence", from_="collections.abc")}),
)
return_annotation = Annotation.many_as_tuple([path_anno, sequence_anno])
assert return_annotation.value == "tuple[Path, Sequence]"
assert return_annotation.imports == path_anno.imports | sequence_anno.imports
def test_unexpected_value(self):
with pytest.raises(ValueError, match=r"unexpected '~' in annotation value"):
Annotation(value="~.foo")
class Test_DoctypeTransformer:
@pytest.mark.parametrize(
"doctype",
[
"((float))",
"(float,)",
"(, )",
"...",
"(..., ...)",
"{}",
"{:}",
"{a:}",
"{:b}",
"{'a',}",
"a or (b or c)",
",, optional",
],
)
def test_edge_case_errors(self, doctype):
transformer = DoctypeTransformer()
with pytest.raises(lark.exceptions.UnexpectedInput):
transformer.doctype_to_annotation(doctype)
@pytest.mark.parametrize("doctype", DoctypeTransformer.blacklisted_qualnames)
def test_reserved_keywords(self, doctype):
assert DoctypeTransformer.blacklisted_qualnames
transformer = DoctypeTransformer()
with pytest.raises(lark.exceptions.VisitError):
transformer.doctype_to_annotation(doctype)
@pytest.mark.parametrize(
("doctype", "expected"),
[
("int or float", "int | float"),
("int or float or str", "int | float | str"),
],
)
def test_natlang_union(self, doctype, expected):
transformer = DoctypeTransformer()
annotation, _ = transformer.doctype_to_annotation(doctype)
assert annotation.value == expected
@pytest.mark.parametrize(
("doctype", "expected"),
[
# Conventional
("list[float]", "list[float]"),
("dict[str, Union[int, str]]", "dict[str, Union[int, str]]"),
("tuple[int, ...]", "tuple[int, ...]"),
("Sequence[int | float]", "Sequence[int | float]"),
# Natural language variant with "of" and optional plural "(s)"
("list of int", "list[int]"),
("list of int(s)", "list[int]"),
# Natural tuple variant
("tuple of (float, int, str)", "tuple[float, int, str]"),
("tuple of (float, ...)", "tuple[float, ...]"),
# Natural dict variant
("dict of {str: int}", "dict[str, int]"),
("dict of {str: int | float}", "dict[str, int | float]"),
("dict of {str: int or float}", "dict[str, int | float]"),
("dict[list of str]", "dict[list[str]]"),
],
)
def test_subscription(self, doctype, expected):
transformer = DoctypeTransformer()
annotation, _ = transformer.doctype_to_annotation(doctype)
assert annotation.value == expected
@pytest.mark.parametrize(
("doctype", "expected"),
[
# Natural language variant with "of" and optional plural "(s)"
("list of int", "list[int]"),
("list of int(s)", "list[int]"),
("list of (int or float)", "list[int | float]"),
("list of (list of int)", "list[list[int]]"),
# Natural tuple variant
("tuple of (float, int, str)", "tuple[float, int, str]"),
("tuple of (float, ...)", "tuple[float, ...]"),
# Natural dict variant
("dict of {str: int}", "dict[str, int]"),
("dict of {str: int | float}", "dict[str, int | float]"),
("dict of {str: int or float}", "dict[str, int | float]"),
# Nesting is possible but probably rarely a good idea
("list of (list of int(s))", "list[list[int]]"),
("tuple of (tuple of (float, ...), ...)", "tuple[tuple[float, ...], ...]"),
("dict of {str: dict of {str: float}}", "dict[str, dict[str, float]]"),
("dict of {str: list of (list of int(s))}", "dict[str, list[list[int]]]"),
],
)
def test_natlang_container(self, doctype, expected):
transformer = DoctypeTransformer()
annotation, _ = transformer.doctype_to_annotation(doctype)
assert annotation.value == expected
@pytest.mark.parametrize(
"doctype",
[
"list of int (s)",
"list of ((float))",
"list of (float,)",
"list of (, )",
"list of ...",
"list of (..., ...)",
"dict of {}",
"dict of {:}",
"dict of {a:}",
"dict of {:b}",
],
)
def test_subscription_error(self, doctype):
transformer = DoctypeTransformer()
with pytest.raises(lark.exceptions.UnexpectedInput):
transformer.doctype_to_annotation(doctype)
@pytest.mark.parametrize(
"doctype",
[
"Callable[[int], str]",
"some_func[[int], str]",
"Callable[[int, float, byte], list[str]]",
"Callable[..., str]",
"Callable[[], str]",
"Callback[...]",
"Callable[Concatenate[int, float], str]",
"Callable[Concatenate[int, ...], str]",
"Callable[P, str]",
],
)
def test_callable(self, doctype):
transformer = DoctypeTransformer()
annotation, _ = transformer.doctype_to_annotation(doctype)
assert annotation.value == doctype
@pytest.mark.parametrize(
"doctype",
[
"Callable[[...], int]",
"Callable[[..., str], int]",
"Callable[[float, str], int, byte]",
],
)
def test_callable_error(self, doctype):
transformer = DoctypeTransformer()
with pytest.raises(lark.exceptions.UnexpectedInput):
transformer.doctype_to_annotation(doctype)
@pytest.mark.parametrize(
("doctype", "expected"),
[
("{0}", "Literal[0]"),
("{-1, 1}", "Literal[-1, 1]"),
("{None}", "Literal[None]"),
("{True, False}", "Literal[True, False]"),
("""{'a', "bar"}""", """Literal['a', "bar"]"""),
# Enum
("{SomeEnum.FIRST}", "Literal[SomeEnum_FIRST]"),
("{`SomeEnum.FIRST`, 1}", "Literal[SomeEnum_FIRST, 1]"),
("{:ref:`SomeEnum.FIRST`, 2}", "Literal[SomeEnum_FIRST, 2]"),
("{:py:ref:`SomeEnum.FIRST`, 3}", "Literal[SomeEnum_FIRST, 3]"),
# Nesting
("dict[{'a', 'b'}, int]", "dict[Literal['a', 'b'], int]"),
# These aren't officially valid as an argument to `Literal` (yet)
# https://typing.python.org/en/latest/spec/literal.html
# TODO figure out how docstub should deal with these
("{-2., 1.}", "Literal[-2., 1.]"),
pytest.param(
"{-inf, inf, nan}",
"Literal[, 1.]",
marks=pytest.mark.xfail(reason="unsure how to support"),
),
],
)
def test_literals(self, doctype, expected):
transformer = DoctypeTransformer()
annotation, _ = transformer.doctype_to_annotation(doctype)
assert annotation.value == expected
def test_single_natlang_literal_warning(self, caplog):
transformer = DoctypeTransformer()
annotation, _ = transformer.doctype_to_annotation("{True}")
assert annotation.value == "Literal[True]"
assert caplog.messages == ["Natural language literal with one item: `{True}`"]
assert caplog.records[0].levelno == logging.WARNING
assert (
caplog.records[0].details
== "Consider using `Literal[True]` to improve readability"
)
@pytest.mark.parametrize(
("doctype", "expected"),
[
("int", "int"),
("int | None", "int | None"),
("tuple of (int, float)", "tuple[int, float]"),
("{'a', 'b'}", "Literal['a', 'b']"),
],
)
@pytest.mark.parametrize(
"optional_info",
[
"",
", optional",
", default -1",
", default: -1",
", default = 1",
", in range (0, 1), optional",
", optional, in range [0, 1]",
", see parameter `image`, optional",
],
)
def test_optional_info(self, doctype, expected, optional_info):
doctype_with_optional = doctype + optional_info
transformer = DoctypeTransformer()
annotation, _ = transformer.doctype_to_annotation(doctype_with_optional)
assert annotation.value == expected
@pytest.mark.parametrize(
("doctype", "expected"),
[
("`Generator`", "Generator"),
(":class:`Generator`", "Generator"),
(":py:class:`Generator`", "Generator"),
(":py:class:`Generator`[int]", "Generator[int]"),
(":py:ref:`~.Foo`[int]", "_Foo[int]"),
("list[:py:class:`Generator`]", "list[Generator]"),
],
)
def test_rst_role(self, doctype, expected):
transformer = DoctypeTransformer()
annotation, _ = transformer.doctype_to_annotation(doctype)
assert annotation.value == expected
# fmt: off
@pytest.mark.parametrize(
("fmt", "expected_fmt"),
[
("{name} of shape {shape} and dtype {dtype}", "{name}[{dtype}]"),
("{name} of dtype {dtype} and shape {shape}", "{name}[{dtype}]"),
("{name} of {dtype}", "{name}[{dtype}]"),
],
)
@pytest.mark.parametrize("name", ["array", "ndarray", "array-like", "array_like"])
@pytest.mark.parametrize("dtype", ["int", "np.int8"])
@pytest.mark.parametrize("shape",
["(2, 3)", "(N, m)", "3D", "2-D", "(N, ...)", "([P,] M, N)"]
)
def test_natlang_array(self, fmt, expected_fmt, name, dtype, shape):
def escape(name: str) -> str:
return name.replace("-", "_").replace(".", "_")
doctype = fmt.format(name=name, dtype=dtype, shape=shape)
expected = expected_fmt.format(
name=escape(name), dtype=escape(dtype), shape=shape
)
transformer = DoctypeTransformer()
annotation, _ = transformer.doctype_to_annotation(doctype)
assert annotation.value == expected
# fmt: on
@pytest.mark.parametrize(
("doctype", "expected"),
[
("ndarray of dtype (int or float)", "ndarray[int | float]"),
],
)
def test_natlang_array_specific(self, doctype, expected):
transformer = DoctypeTransformer()
annotation, _ = transformer.doctype_to_annotation(doctype)
assert annotation.value == expected
@pytest.mark.parametrize("shape", ["(-1, 3)", "(1.0, 2)", "-3D", "-2-D"])
def test_natlang_array_invalid_shape(self, shape):
doctype = f"array of shape {shape}"
transformer = DoctypeTransformer()
with pytest.raises(lark.exceptions.UnexpectedInput):
_ = transformer.doctype_to_annotation(doctype)
def test_unknown_name(self):
# Simple unknown name is aliased to typing.Any
transformer = DoctypeTransformer()
annotation, unknown_names = transformer.doctype_to_annotation("a")
assert annotation.value == "a"
assert annotation.imports == {
PyImport(import_="Incomplete", from_="_typeshed", as_="a")
}
assert unknown_names == [("a", 0, 1)]
def test_unknown_qualname(self):
# Unknown qualified name is escaped and aliased to typing.Any as well
transformer = DoctypeTransformer()
annotation, unknown_names = transformer.doctype_to_annotation("a.b")
assert annotation.value == "a_b"
assert annotation.imports == {
PyImport(import_="Incomplete", from_="_typeshed", as_="a_b")
}
assert unknown_names == [("a.b", 0, 3)]
def test_multiple_unknown_names(self):
# Multiple names are aliased to typing.Any
transformer = DoctypeTransformer()
annotation, unknown_names = transformer.doctype_to_annotation("a.b of c")
assert annotation.value == "a_b[c]"
assert annotation.imports == {
PyImport(import_="Incomplete", from_="_typeshed", as_="a_b"),
PyImport(import_="Incomplete", from_="_typeshed", as_="c"),
}
assert unknown_names == [("a.b", 0, 3), ("c", 7, 8)]
class Test_DocstringAnnotations:
def test_empty_docstring(self):
docstring = dedent("""No sections in this docstring.""")
transformer = DoctypeTransformer()
annotations = DocstringAnnotations(docstring, transformer=transformer)
assert annotations.attributes == {}
assert annotations.parameters == {}
assert annotations.returns is None
@pytest.mark.parametrize(
("doctype", "expected"),
[
("bool", "bool"),
("str, extra information", "str"),
("list of int, optional", "list[int]"),
],
)
def test_parameters(self, doctype, expected):
docstring = dedent(
f"""
Parameters
----------
a : {doctype}
b :
"""
)
transformer = DoctypeTransformer()
annotations = DocstringAnnotations(docstring, transformer=transformer)
assert len(annotations.parameters) == 1
assert annotations.parameters["a"].value == expected
assert "b" not in annotations.parameters
@pytest.mark.parametrize(
("doctypes", "expected"),
[
(["bool", "int | None"], "tuple[bool, int | None]"),
(["tuple of int", "tuple[int, ...]"], "tuple[tuple[int], tuple[int, ...]]"),
],
)
def test_returns(self, doctypes, expected):
docstring = dedent(
"""
Returns
-------
a : {}
b : {}
"""
).format(*doctypes)
transformer = DoctypeTransformer()
annotations = DocstringAnnotations(docstring, transformer=transformer)
assert annotations.returns is not None
assert annotations.returns.value == expected
def test_yields(self, caplog):
docstring = dedent(
"""
Yields
------
a : int
b : str
"""
)
transformer = DoctypeTransformer()
annotations = DocstringAnnotations(docstring, transformer=transformer)
assert annotations.returns is not None
assert annotations.returns.value == "Generator[tuple[int, str]]"
assert annotations.returns.imports == {
PyImport(from_="collections.abc", import_="Generator")
}
def test_receives(self, caplog):
docstring = dedent(
"""
Yields
------
a : int
b : str
Receives
--------
c : float
d : bytes
"""
)
transformer = DoctypeTransformer()
annotations = DocstringAnnotations(docstring, transformer=transformer)
assert annotations.returns is not None
assert (
annotations.returns.value
== "Generator[tuple[int, str], tuple[float, bytes]]"
)
assert annotations.returns.imports == {
PyImport(from_="collections.abc", import_="Generator")
}
def test_full_generator(self, caplog):
docstring = dedent(
"""
Yields
------
a : int
b : str
Receives
--------
c : float
d : bytes
Returns
-------
e : bool
"""
)
transformer = DoctypeTransformer()
annotations = DocstringAnnotations(docstring, transformer=transformer)
assert annotations.returns is not None
assert annotations.returns.value == (
"Generator[tuple[int, str], tuple[float, bytes], bool]"
)
assert annotations.returns.imports == {
PyImport(from_="collections.abc", import_="Generator")
}
def test_yields_and_returns(self, caplog):
docstring = dedent(
"""
Yields
------
a : int
b : str
Returns
-------
e : bool
"""
)
transformer = DoctypeTransformer()
annotations = DocstringAnnotations(docstring, transformer=transformer)
assert annotations.returns is not None
assert annotations.returns.value == ("Generator[tuple[int, str], None, bool]")
assert annotations.returns.imports == {
PyImport(from_="collections.abc", import_="Generator")
}
def test_duplicate_parameters(self, caplog):
docstring = dedent(
"""
Parameters
----------
a : int
a : str
"""
)
transformer = DoctypeTransformer()
annotations = DocstringAnnotations(docstring, transformer=transformer)
assert len(annotations.parameters) == 1
assert annotations.parameters["a"].value == "int"
def test_duplicate_returns(self, caplog):
docstring = dedent(
"""
Returns
-------
a : int
a : str
"""
)
transformer = DoctypeTransformer()
annotations = DocstringAnnotations(docstring, transformer=transformer)
assert annotations.returns is not None
assert annotations.returns is not None
assert annotations.returns.value == "int"
def test_args_kwargs(self):
docstring = dedent(
"""
Parameters
----------
*args : int
**kwargs : str
"""
)
transformer = DoctypeTransformer()
annotations = DocstringAnnotations(docstring, transformer=transformer)
assert "args" in annotations.parameters
assert "*args" not in annotations.parameters
assert "kwargs" in annotations.parameters
assert "**kargs" not in annotations.parameters
def test_missing_whitespace(self, caplog):
"""Check for warning if a whitespace is missing between parameter and colon.
In this case, NumPyDoc parses the entire thing as the parameter name.
"""
docstring = dedent(
"""
Parameters
----------
a: int
"""
)
transformer = DoctypeTransformer()
annotations = DocstringAnnotations(docstring, transformer=transformer)
assert annotations.parameters["a"].value == "int"
assert len(caplog.records) == 1
assert "Possibly missing whitespace" in caplog.text
def test_combined_numpydoc_params(self):
docstring = dedent(
"""
Parameters
----------
a, b, c : bool
d, e :
"""
)
transformer = DoctypeTransformer()
annotations = DocstringAnnotations(docstring, transformer=transformer)
assert len(annotations.parameters) == 3
assert annotations.parameters["a"].value == "bool"
assert annotations.parameters["b"].value == "bool"
assert annotations.parameters["c"].value == "bool"
assert "d" not in annotations.parameters
assert "e" not in annotations.parameters