| 1 |
"""Tests for :py:mod:`sphinx.ext.napoleon.docstring` module.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import re |
| 6 |
import zlib |
| 7 |
from collections import namedtuple |
| 8 |
from inspect import cleandoc |
| 9 |
from itertools import product |
| 10 |
from textwrap import dedent |
| 11 |
from types import SimpleNamespace |
| 12 |
from typing import TYPE_CHECKING |
| 13 |
from unittest import mock |
| 14 |
|
| 15 |
import pytest |
| 16 |
|
| 17 |
from sphinx.ext.intersphinx._load import load_mappings, validate_intersphinx_mapping |
| 18 |
from sphinx.ext.napoleon import Config |
| 19 |
from sphinx.ext.napoleon.docstring import ( |
| 20 |
GoogleDocstring, |
| 21 |
NumpyDocstring, |
| 22 |
_convert_type_spec, |
| 23 |
_recombine_set_tokens, |
| 24 |
_token_type, |
| 25 |
_tokenize_type_spec, |
| 26 |
) |
| 27 |
from sphinx.testing.util import etree_parse |
| 28 |
|
| 29 |
from tests.test_ext_napoleon.pep526_data_google import PEP526GoogleClass |
| 30 |
from tests.test_ext_napoleon.pep526_data_numpy import PEP526NumpyClass |
| 31 |
|
| 32 |
if TYPE_CHECKING: |
| 33 |
from sphinx.testing.util import SphinxTestApp |
| 34 |
|
| 35 |
|
| 36 |
class NamedtupleSubclass(namedtuple('NamedtupleSubclass', ('attr1', 'attr2'))): # NoQA: PYI024 |
| 37 |
"""Sample namedtuple subclass |
| 38 |
|
| 39 |
Attributes |
| 40 |
---------- |
| 41 |
attr1 : Arbitrary type |
| 42 |
Quick description of attr1 |
| 43 |
attr2 : Another arbitrary type |
| 44 |
Quick description of attr2 |
| 45 |
attr3 : Type |
| 46 |
|
| 47 |
Adds a newline after the type |
| 48 |
|
| 49 |
""" |
| 50 |
|
| 51 |
# To avoid creating a dict, as a namedtuple doesn't have it: |
| 52 |
__slots__ = () |
| 53 |
|
| 54 |
def __new__(cls, attr1, attr2=None): |
| 55 |
return super().__new__(cls, attr1, attr2) |
| 56 |
|
| 57 |
|
| 58 |
class TestNamedtupleSubclass: |
| 59 |
def test_attributes_docstring(self): |
| 60 |
config = Config() |
| 61 |
actual = NumpyDocstring( |
| 62 |
cleandoc(NamedtupleSubclass.__doc__), |
| 63 |
config=config, |
| 64 |
app=None, |
| 65 |
what='class', |
| 66 |
name='NamedtupleSubclass', |
| 67 |
obj=NamedtupleSubclass, |
| 68 |
) |
| 69 |
expected = """\ |
| 70 |
Sample namedtuple subclass |
| 71 |
|
| 72 |
.. attribute:: attr1 |
| 73 |
|
| 74 |
Quick description of attr1 |
| 75 |
|
| 76 |
:type: Arbitrary type |
| 77 |
|
| 78 |
.. attribute:: attr2 |
| 79 |
|
| 80 |
Quick description of attr2 |
| 81 |
|
| 82 |
:type: Another arbitrary type |
| 83 |
|
| 84 |
.. attribute:: attr3 |
| 85 |
|
| 86 |
Adds a newline after the type |
| 87 |
|
| 88 |
:type: Type |
| 89 |
""" |
| 90 |
|
| 91 |
assert str(actual) == expected |
| 92 |
|
| 93 |
|
| 94 |
class TestInlineAttribute: |
| 95 |
inline_google_docstring = ( |
| 96 |
'inline description with ' |
| 97 |
'``a : in code``, ' |
| 98 |
'a :ref:`reference`, ' |
| 99 |
'a `link <https://foo.bar>`_, ' |
| 100 |
'a :meta public:, ' |
| 101 |
'a :meta field: value and ' |
| 102 |
'an host:port and HH:MM strings.' |
| 103 |
) |
| 104 |
|
| 105 |
@staticmethod |
| 106 |
def _docstring(source): |
| 107 |
rst = GoogleDocstring( |
| 108 |
source, config=Config(), app=None, what='attribute', name='some_data', obj=0 |
| 109 |
) |
| 110 |
return str(rst) |
| 111 |
|
| 112 |
def test_class_data_member(self): |
| 113 |
source = 'data member description:\n\n- a: b' |
| 114 |
actual = self._docstring(source).splitlines() |
| 115 |
assert actual == ['data member description:', '', '- a: b'] |
| 116 |
|
| 117 |
def test_class_data_member_inline(self): |
| 118 |
source = f'CustomType: {self.inline_google_docstring}' |
| 119 |
actual = self._docstring(source).splitlines() |
| 120 |
assert actual == [self.inline_google_docstring, '', ':type: CustomType'] |
| 121 |
|
| 122 |
def test_class_data_member_inline_no_type(self): |
| 123 |
source = self.inline_google_docstring |
| 124 |
actual = self._docstring(source).splitlines() |
| 125 |
assert actual == [source] |
| 126 |
|
| 127 |
def test_class_data_member_inline_ref_in_type(self): |
| 128 |
source = f':py:class:`int`: {self.inline_google_docstring}' |
| 129 |
actual = self._docstring(source).splitlines() |
| 130 |
assert actual == [self.inline_google_docstring, '', ':type: :py:class:`int`'] |
| 131 |
|
| 132 |
|
| 133 |
class TestGoogleDocstring: |
| 134 |
docstrings = [ |
| 135 |
( |
| 136 |
"""Single line summary""", |
| 137 |
"""Single line summary""", |
| 138 |
), |
| 139 |
( |
| 140 |
""" |
| 141 |
Single line summary |
| 142 |
|
| 143 |
Extended description |
| 144 |
|
| 145 |
""", |
| 146 |
""" |
| 147 |
Single line summary |
| 148 |
|
| 149 |
Extended description |
| 150 |
""", |
| 151 |
), |
| 152 |
( |
| 153 |
""" |
| 154 |
Single line summary |
| 155 |
|
| 156 |
Args: |
| 157 |
arg1(str):Extended |
| 158 |
description of arg1 |
| 159 |
""", |
| 160 |
""" |
| 161 |
Single line summary |
| 162 |
|
| 163 |
:Parameters: **arg1** (*str*) -- Extended |
| 164 |
description of arg1 |
| 165 |
""", |
| 166 |
), |
| 167 |
( |
| 168 |
""" |
| 169 |
Single line summary |
| 170 |
|
| 171 |
Args: |
| 172 |
arg1(str):Extended |
| 173 |
description of arg1 |
| 174 |
arg2 ( int ) : Extended |
| 175 |
description of arg2 |
| 176 |
|
| 177 |
Keyword Args: |
| 178 |
kwarg1(str):Extended |
| 179 |
description of kwarg1 |
| 180 |
kwarg2 ( int ) : Extended |
| 181 |
description of kwarg2""", |
| 182 |
""" |
| 183 |
Single line summary |
| 184 |
|
| 185 |
:Parameters: * **arg1** (*str*) -- Extended |
| 186 |
description of arg1 |
| 187 |
* **arg2** (*int*) -- Extended |
| 188 |
description of arg2 |
| 189 |
|
| 190 |
:Keyword Arguments: * **kwarg1** (*str*) -- Extended |
| 191 |
description of kwarg1 |
| 192 |
* **kwarg2** (*int*) -- Extended |
| 193 |
description of kwarg2 |
| 194 |
""", |
| 195 |
), |
| 196 |
( |
| 197 |
""" |
| 198 |
Single line summary |
| 199 |
|
| 200 |
Arguments: |
| 201 |
arg1(str):Extended |
| 202 |
description of arg1 |
| 203 |
arg2 ( int ) : Extended |
| 204 |
description of arg2 |
| 205 |
|
| 206 |
Keyword Arguments: |
| 207 |
kwarg1(str):Extended |
| 208 |
description of kwarg1 |
| 209 |
kwarg2 ( int ) : Extended |
| 210 |
description of kwarg2""", |
| 211 |
""" |
| 212 |
Single line summary |
| 213 |
|
| 214 |
:Parameters: * **arg1** (*str*) -- Extended |
| 215 |
description of arg1 |
| 216 |
* **arg2** (*int*) -- Extended |
| 217 |
description of arg2 |
| 218 |
|
| 219 |
:Keyword Arguments: * **kwarg1** (*str*) -- Extended |
| 220 |
description of kwarg1 |
| 221 |
* **kwarg2** (*int*) -- Extended |
| 222 |
description of kwarg2 |
| 223 |
""", |
| 224 |
), |
| 225 |
( |
| 226 |
""" |
| 227 |
Single line summary |
| 228 |
|
| 229 |
Return: |
| 230 |
str:Extended |
| 231 |
description of return value |
| 232 |
""", |
| 233 |
""" |
| 234 |
Single line summary |
| 235 |
|
| 236 |
:returns: *str* -- Extended |
| 237 |
description of return value |
| 238 |
""", |
| 239 |
), |
| 240 |
( |
| 241 |
""" |
| 242 |
Single line summary |
| 243 |
|
| 244 |
Returns: |
| 245 |
str:Extended |
| 246 |
description of return value |
| 247 |
""", |
| 248 |
""" |
| 249 |
Single line summary |
| 250 |
|
| 251 |
:returns: *str* -- Extended |
| 252 |
description of return value |
| 253 |
""", |
| 254 |
), |
| 255 |
( |
| 256 |
""" |
| 257 |
Single line summary |
| 258 |
|
| 259 |
Returns: |
| 260 |
Extended |
| 261 |
description of return value |
| 262 |
""", |
| 263 |
""" |
| 264 |
Single line summary |
| 265 |
|
| 266 |
:returns: Extended |
| 267 |
description of return value |
| 268 |
""", |
| 269 |
), |
| 270 |
( |
| 271 |
""" |
| 272 |
Single line summary |
| 273 |
|
| 274 |
Returns: |
| 275 |
Extended |
| 276 |
""", |
| 277 |
""" |
| 278 |
Single line summary |
| 279 |
|
| 280 |
:returns: Extended |
| 281 |
""", |
| 282 |
), |
| 283 |
( |
| 284 |
""" |
| 285 |
Single line summary |
| 286 |
|
| 287 |
Args: |
| 288 |
arg1(str):Extended |
| 289 |
description of arg1 |
| 290 |
*args: Variable length argument list. |
| 291 |
**kwargs: Arbitrary keyword arguments. |
| 292 |
""", |
| 293 |
""" |
| 294 |
Single line summary |
| 295 |
|
| 296 |
:Parameters: * **arg1** (*str*) -- Extended |
| 297 |
description of arg1 |
| 298 |
* **\\*args** -- Variable length argument list. |
| 299 |
* **\\*\\*kwargs** -- Arbitrary keyword arguments. |
| 300 |
""", |
| 301 |
), |
| 302 |
( |
| 303 |
""" |
| 304 |
Single line summary |
| 305 |
|
| 306 |
Args: |
| 307 |
arg1 (list(int)): Description |
| 308 |
arg2 (list[int]): Description |
| 309 |
arg3 (dict(str, int)): Description |
| 310 |
arg4 (dict[str, int]): Description |
| 311 |
""", |
| 312 |
""" |
| 313 |
Single line summary |
| 314 |
|
| 315 |
:Parameters: * **arg1** (*list(int)*) -- Description |
| 316 |
* **arg2** (*list[int]*) -- Description |
| 317 |
* **arg3** (*dict(str, int)*) -- Description |
| 318 |
* **arg4** (*dict[str, int]*) -- Description |
| 319 |
""", |
| 320 |
), |
| 321 |
( |
| 322 |
""" |
| 323 |
Single line summary |
| 324 |
|
| 325 |
Receive: |
| 326 |
arg1 (list(int)): Description |
| 327 |
arg2 (list[int]): Description |
| 328 |
""", |
| 329 |
""" |
| 330 |
Single line summary |
| 331 |
|
| 332 |
:Receives: * **arg1** (*list(int)*) -- Description |
| 333 |
* **arg2** (*list[int]*) -- Description |
| 334 |
""", |
| 335 |
), |
| 336 |
( |
| 337 |
""" |
| 338 |
Single line summary |
| 339 |
|
| 340 |
Receives: |
| 341 |
arg1 (list(int)): Description |
| 342 |
arg2 (list[int]): Description |
| 343 |
""", |
| 344 |
""" |
| 345 |
Single line summary |
| 346 |
|
| 347 |
:Receives: * **arg1** (*list(int)*) -- Description |
| 348 |
* **arg2** (*list[int]*) -- Description |
| 349 |
""", |
| 350 |
), |
| 351 |
( |
| 352 |
""" |
| 353 |
Single line summary |
| 354 |
|
| 355 |
Yield: |
| 356 |
str:Extended |
| 357 |
description of yielded value |
| 358 |
""", |
| 359 |
""" |
| 360 |
Single line summary |
| 361 |
|
| 362 |
:Yields: *str* -- Extended |
| 363 |
description of yielded value |
| 364 |
""", |
| 365 |
), |
| 366 |
( |
| 367 |
""" |
| 368 |
Single line summary |
| 369 |
|
| 370 |
Yields: |
| 371 |
Extended |
| 372 |
description of yielded value |
| 373 |
""", |
| 374 |
""" |
| 375 |
Single line summary |
| 376 |
|
| 377 |
:Yields: Extended |
| 378 |
description of yielded value |
| 379 |
""", |
| 380 |
), |
| 381 |
( |
| 382 |
""" |
| 383 |
Single line summary |
| 384 |
|
| 385 |
Args: |
| 386 |
|
| 387 |
arg1 (list of str): Extended |
| 388 |
description of arg1. |
| 389 |
arg2 (tuple of int): Extended |
| 390 |
description of arg2. |
| 391 |
arg3 (tuple of list of float): Extended |
| 392 |
description of arg3. |
| 393 |
arg4 (int, float, or list of bool): Extended |
| 394 |
description of arg4. |
| 395 |
arg5 (list of int, float, or bool): Extended |
| 396 |
description of arg5. |
| 397 |
arg6 (list of int or float): Extended |
| 398 |
description of arg6. |
| 399 |
""", |
| 400 |
""" |
| 401 |
Single line summary |
| 402 |
|
| 403 |
:Parameters: * **arg1** (*list of str*) -- Extended |
| 404 |
description of arg1. |
| 405 |
* **arg2** (*tuple of int*) -- Extended |
| 406 |
description of arg2. |
| 407 |
* **arg3** (*tuple of list of float*) -- Extended |
| 408 |
description of arg3. |
| 409 |
* **arg4** (*int, float, or list of bool*) -- Extended |
| 410 |
description of arg4. |
| 411 |
* **arg5** (*list of int, float, or bool*) -- Extended |
| 412 |
description of arg5. |
| 413 |
* **arg6** (*list of int or float*) -- Extended |
| 414 |
description of arg6. |
| 415 |
""", |
| 416 |
), |
| 417 |
] |
| 418 |
|
| 419 |
def test_sphinx_admonitions(self): |
| 420 |
admonition_map = { |
| 421 |
'Attention': 'attention', |
| 422 |
'Caution': 'caution', |
| 423 |
'Danger': 'danger', |
| 424 |
'Error': 'error', |
| 425 |
'Hint': 'hint', |
| 426 |
'Important': 'important', |
| 427 |
'Note': 'note', |
| 428 |
'Tip': 'tip', |
| 429 |
'Todo': 'todo', |
| 430 |
'Warning': 'warning', |
| 431 |
'Warnings': 'warning', |
| 432 |
} |
| 433 |
config = Config() |
| 434 |
for section, admonition in admonition_map.items(): |
| 435 |
# Multiline |
| 436 |
actual = GoogleDocstring( |
| 437 |
f'{section}:\n' |
| 438 |
' this is the first line\n' |
| 439 |
'\n' |
| 440 |
' and this is the second line\n', |
| 441 |
config, |
| 442 |
) |
| 443 |
expect = ( |
| 444 |
f'.. {admonition}::\n' |
| 445 |
'\n' |
| 446 |
' this is the first line\n' |
| 447 |
' \n' |
| 448 |
' and this is the second line\n' |
| 449 |
) |
| 450 |
assert str(actual) == expect |
| 451 |
|
| 452 |
# Single line |
| 453 |
actual = GoogleDocstring(f'{section}:\n this is a single line\n', config) |
| 454 |
expect = f'.. {admonition}:: this is a single line\n' |
| 455 |
assert str(actual) == expect |
| 456 |
|
| 457 |
def test_docstrings(self): |
| 458 |
config = Config( |
| 459 |
napoleon_use_param=False, |
| 460 |
napoleon_use_rtype=False, |
| 461 |
napoleon_use_keyword=False, |
| 462 |
) |
| 463 |
for docstring, expected in self.docstrings: |
| 464 |
actual = GoogleDocstring(dedent(docstring), config) |
| 465 |
expected = dedent(expected) |
| 466 |
assert str(actual) == expected |
| 467 |
|
| 468 |
def test_parameters_with_class_reference(self): |
| 469 |
docstring = """\ |
| 470 |
Construct a new XBlock. |
| 471 |
|
| 472 |
This class should only be used by runtimes. |
| 473 |
|
| 474 |
Arguments: |
| 475 |
runtime (:py:class:`~typing.Dict`\\[:py:class:`int`,:py:class:`str`\\]): Use it to |
| 476 |
access the environment. It is available in XBlock code |
| 477 |
as ``self.runtime``. |
| 478 |
|
| 479 |
field_data (:py:class:`FieldData`): Interface used by the XBlock |
| 480 |
fields to access their data from wherever it is persisted. |
| 481 |
|
| 482 |
scope_ids (:py:class:`ScopeIds`): Identifiers needed to resolve scopes. |
| 483 |
|
| 484 |
""" |
| 485 |
|
| 486 |
actual = GoogleDocstring(docstring) |
| 487 |
expected = """\ |
| 488 |
Construct a new XBlock. |
| 489 |
|
| 490 |
This class should only be used by runtimes. |
| 491 |
|
| 492 |
:param runtime: Use it to |
| 493 |
access the environment. It is available in XBlock code |
| 494 |
as ``self.runtime``. |
| 495 |
:type runtime: :py:class:`~typing.Dict`\\[:py:class:`int`,:py:class:`str`\\] |
| 496 |
:param field_data: Interface used by the XBlock |
| 497 |
fields to access their data from wherever it is persisted. |
| 498 |
:type field_data: :py:class:`FieldData` |
| 499 |
:param scope_ids: Identifiers needed to resolve scopes. |
| 500 |
:type scope_ids: :py:class:`ScopeIds` |
| 501 |
""" |
| 502 |
assert str(actual) == expected |
| 503 |
|
| 504 |
def test_attributes_with_class_reference(self): |
| 505 |
docstring = """\ |
| 506 |
Attributes: |
| 507 |
in_attr(:py:class:`numpy.ndarray`): super-dooper attribute |
| 508 |
""" |
| 509 |
|
| 510 |
actual = GoogleDocstring(docstring) |
| 511 |
expected = """\ |
| 512 |
.. attribute:: in_attr |
| 513 |
|
| 514 |
super-dooper attribute |
| 515 |
|
| 516 |
:type: :py:class:`numpy.ndarray` |
| 517 |
""" |
| 518 |
assert str(actual) == expected |
| 519 |
|
| 520 |
docstring = """\ |
| 521 |
Attributes: |
| 522 |
in_attr(numpy.ndarray): super-dooper attribute |
| 523 |
""" |
| 524 |
|
| 525 |
actual = GoogleDocstring(docstring) |
| 526 |
expected = """\ |
| 527 |
.. attribute:: in_attr |
| 528 |
|
| 529 |
super-dooper attribute |
| 530 |
|
| 531 |
:type: numpy.ndarray |
| 532 |
""" |
| 533 |
|
| 534 |
def test_attributes_with_use_ivar(self): |
| 535 |
docstring = """\ |
| 536 |
Attributes: |
| 537 |
foo (int): blah blah |
| 538 |
bar (str): blah blah |
| 539 |
""" |
| 540 |
|
| 541 |
config = Config(napoleon_use_ivar=True) |
| 542 |
actual = GoogleDocstring(docstring, config, obj=self.__class__) |
| 543 |
expected = """\ |
| 544 |
:ivar foo: blah blah |
| 545 |
:vartype foo: int |
| 546 |
:ivar bar: blah blah |
| 547 |
:vartype bar: str |
| 548 |
""" |
| 549 |
assert str(actual) == expected |
| 550 |
|
| 551 |
def test_code_block_in_returns_section(self): |
| 552 |
docstring = """ |
| 553 |
Returns: |
| 554 |
foobar: foo:: |
| 555 |
|
| 556 |
codecode |
| 557 |
codecode |
| 558 |
""" |
| 559 |
expected = """ |
| 560 |
:returns: |
| 561 |
|
| 562 |
foo:: |
| 563 |
|
| 564 |
codecode |
| 565 |
codecode |
| 566 |
:rtype: foobar |
| 567 |
""" |
| 568 |
actual = GoogleDocstring(docstring) |
| 569 |
assert str(actual) == expected |
| 570 |
|
| 571 |
def test_colon_in_return_type(self): |
| 572 |
docstring = """Example property. |
| 573 |
|
| 574 |
Returns: |
| 575 |
:py:class:`~.module.submodule.SomeClass`: an example instance |
| 576 |
if available, None if not available. |
| 577 |
""" |
| 578 |
expected = """Example property. |
| 579 |
|
| 580 |
:returns: an example instance |
| 581 |
if available, None if not available. |
| 582 |
:rtype: :py:class:`~.module.submodule.SomeClass` |
| 583 |
""" |
| 584 |
actual = GoogleDocstring(docstring) |
| 585 |
assert str(actual) == expected |
| 586 |
|
| 587 |
def test_xrefs_in_return_type(self): |
| 588 |
docstring = """Example Function |
| 589 |
|
| 590 |
Returns: |
| 591 |
:py:class:`numpy.ndarray`: A :math:`n \\times 2` array containing |
| 592 |
a bunch of math items |
| 593 |
""" |
| 594 |
expected = """Example Function |
| 595 |
|
| 596 |
:returns: A :math:`n \\times 2` array containing |
| 597 |
a bunch of math items |
| 598 |
:rtype: :py:class:`numpy.ndarray` |
| 599 |
""" |
| 600 |
actual = GoogleDocstring(docstring) |
| 601 |
assert str(actual) == expected |
| 602 |
|
| 603 |
def test_raises_types(self): |
| 604 |
docstrings = [ |
| 605 |
( |
| 606 |
""" |
| 607 |
Example Function |
| 608 |
|
| 609 |
Raises: |
| 610 |
RuntimeError: |
| 611 |
A setting wasn't specified, or was invalid. |
| 612 |
ValueError: |
| 613 |
Something something value error. |
| 614 |
:py:class:`AttributeError` |
| 615 |
errors for missing attributes. |
| 616 |
~InvalidDimensionsError |
| 617 |
If the dimensions couldn't be parsed. |
| 618 |
`InvalidArgumentsError` |
| 619 |
If the arguments are invalid. |
| 620 |
:py:exc:`~ValueError` |
| 621 |
If the arguments are wrong. |
| 622 |
|
| 623 |
""", |
| 624 |
""" |
| 625 |
Example Function |
| 626 |
|
| 627 |
:raises RuntimeError: A setting wasn't specified, or was invalid. |
| 628 |
:raises ValueError: Something something value error. |
| 629 |
:raises AttributeError: errors for missing attributes. |
| 630 |
:raises ~InvalidDimensionsError: If the dimensions couldn't be parsed. |
| 631 |
:raises InvalidArgumentsError: If the arguments are invalid. |
| 632 |
:raises ~ValueError: If the arguments are wrong. |
| 633 |
""", |
| 634 |
), |
| 635 |
################################ |
| 636 |
( |
| 637 |
""" |
| 638 |
Example Function |
| 639 |
|
| 640 |
Raises: |
| 641 |
InvalidDimensionsError |
| 642 |
|
| 643 |
""", |
| 644 |
""" |
| 645 |
Example Function |
| 646 |
|
| 647 |
:raises InvalidDimensionsError: |
| 648 |
""", |
| 649 |
), |
| 650 |
################################ |
| 651 |
( |
| 652 |
""" |
| 653 |
Example Function |
| 654 |
|
| 655 |
Raises: |
| 656 |
Invalid Dimensions Error |
| 657 |
|
| 658 |
""", |
| 659 |
""" |
| 660 |
Example Function |
| 661 |
|
| 662 |
:raises Invalid Dimensions Error: |
| 663 |
""", |
| 664 |
), |
| 665 |
################################ |
| 666 |
( |
| 667 |
""" |
| 668 |
Example Function |
| 669 |
|
| 670 |
Raises: |
| 671 |
Invalid Dimensions Error: With description |
| 672 |
|
| 673 |
""", |
| 674 |
""" |
| 675 |
Example Function |
| 676 |
|
| 677 |
:raises Invalid Dimensions Error: With description |
| 678 |
""", |
| 679 |
), |
| 680 |
################################ |
| 681 |
( |
| 682 |
""" |
| 683 |
Example Function |
| 684 |
|
| 685 |
Raises: |
| 686 |
InvalidDimensionsError: If the dimensions couldn't be parsed. |
| 687 |
|
| 688 |
""", |
| 689 |
""" |
| 690 |
Example Function |
| 691 |
|
| 692 |
:raises InvalidDimensionsError: If the dimensions couldn't be parsed. |
| 693 |
""", |
| 694 |
), |
| 695 |
################################ |
| 696 |
( |
| 697 |
""" |
| 698 |
Example Function |
| 699 |
|
| 700 |
Raises: |
| 701 |
Invalid Dimensions Error: If the dimensions couldn't be parsed. |
| 702 |
|
| 703 |
""", |
| 704 |
""" |
| 705 |
Example Function |
| 706 |
|
| 707 |
:raises Invalid Dimensions Error: If the dimensions couldn't be parsed. |
| 708 |
""", |
| 709 |
), |
| 710 |
################################ |
| 711 |
( |
| 712 |
""" |
| 713 |
Example Function |
| 714 |
|
| 715 |
Raises: |
| 716 |
If the dimensions couldn't be parsed. |
| 717 |
|
| 718 |
""", |
| 719 |
""" |
| 720 |
Example Function |
| 721 |
|
| 722 |
:raises If the dimensions couldn't be parsed.: |
| 723 |
""", |
| 724 |
), |
| 725 |
################################ |
| 726 |
( |
| 727 |
""" |
| 728 |
Example Function |
| 729 |
|
| 730 |
Raises: |
| 731 |
:py:class:`exc.InvalidDimensionsError` |
| 732 |
|
| 733 |
""", |
| 734 |
""" |
| 735 |
Example Function |
| 736 |
|
| 737 |
:raises exc.InvalidDimensionsError: |
| 738 |
""", |
| 739 |
), |
| 740 |
################################ |
| 741 |
( |
| 742 |
""" |
| 743 |
Example Function |
| 744 |
|
| 745 |
Raises: |
| 746 |
:py:class:`exc.InvalidDimensionsError`: If the dimensions couldn't be parsed. |
| 747 |
|
| 748 |
""", |
| 749 |
""" |
| 750 |
Example Function |
| 751 |
|
| 752 |
:raises exc.InvalidDimensionsError: If the dimensions couldn't be parsed. |
| 753 |
""", |
| 754 |
), |
| 755 |
################################ |
| 756 |
( |
| 757 |
""" |
| 758 |
Example Function |
| 759 |
|
| 760 |
Raises: |
| 761 |
:py:class:`exc.InvalidDimensionsError`: If the dimensions couldn't be parsed, |
| 762 |
then a :py:class:`exc.InvalidDimensionsError` will be raised. |
| 763 |
|
| 764 |
""", |
| 765 |
""" |
| 766 |
Example Function |
| 767 |
|
| 768 |
:raises exc.InvalidDimensionsError: If the dimensions couldn't be parsed, |
| 769 |
then a :py:class:`exc.InvalidDimensionsError` will be raised. |
| 770 |
""", |
| 771 |
), |
| 772 |
################################ |
| 773 |
( |
| 774 |
""" |
| 775 |
Example Function |
| 776 |
|
| 777 |
Raises: |
| 778 |
:py:class:`exc.InvalidDimensionsError`: If the dimensions couldn't be parsed. |
| 779 |
:py:class:`exc.InvalidArgumentsError`: If the arguments are invalid. |
| 780 |
|
| 781 |
""", |
| 782 |
""" |
| 783 |
Example Function |
| 784 |
|
| 785 |
:raises exc.InvalidDimensionsError: If the dimensions couldn't be parsed. |
| 786 |
:raises exc.InvalidArgumentsError: If the arguments are invalid. |
| 787 |
""", |
| 788 |
), |
| 789 |
################################ |
| 790 |
( |
| 791 |
""" |
| 792 |
Example Function |
| 793 |
|
| 794 |
Raises: |
| 795 |
:py:class:`exc.InvalidDimensionsError` |
| 796 |
:py:class:`exc.InvalidArgumentsError` |
| 797 |
|
| 798 |
""", |
| 799 |
""" |
| 800 |
Example Function |
| 801 |
|
| 802 |
:raises exc.InvalidDimensionsError: |
| 803 |
:raises exc.InvalidArgumentsError: |
| 804 |
""", |
| 805 |
), |
| 806 |
] |
| 807 |
for docstring, expected in docstrings: |
| 808 |
actual = GoogleDocstring(docstring) |
| 809 |
assert str(actual) == expected |
| 810 |
|
| 811 |
def test_kwargs_in_arguments(self): |
| 812 |
docstring = """Allows to create attributes binded to this device. |
| 813 |
|
| 814 |
Some other paragraph. |
| 815 |
|
| 816 |
Code sample for usage:: |
| 817 |
|
| 818 |
dev.bind(loopback=Loopback) |
| 819 |
dev.loopback.configure() |
| 820 |
|
| 821 |
Arguments: |
| 822 |
**kwargs: name/class pairs that will create resource-managers |
| 823 |
bound as instance attributes to this instance. See code |
| 824 |
example above. |
| 825 |
""" |
| 826 |
expected = """Allows to create attributes binded to this device. |
| 827 |
|
| 828 |
Some other paragraph. |
| 829 |
|
| 830 |
Code sample for usage:: |
| 831 |
|
| 832 |
dev.bind(loopback=Loopback) |
| 833 |
dev.loopback.configure() |
| 834 |
|
| 835 |
:param \\*\\*kwargs: name/class pairs that will create resource-managers |
| 836 |
bound as instance attributes to this instance. See code |
| 837 |
example above. |
| 838 |
""" |
| 839 |
actual = GoogleDocstring(docstring) |
| 840 |
assert str(actual) == expected |
| 841 |
|
| 842 |
def test_section_header_formatting(self): |
| 843 |
docstrings = [ |
| 844 |
( |
| 845 |
""" |
| 846 |
Summary line |
| 847 |
|
| 848 |
Example: |
| 849 |
Multiline reStructuredText |
| 850 |
literal code block |
| 851 |
|
| 852 |
""", |
| 853 |
""" |
| 854 |
Summary line |
| 855 |
|
| 856 |
.. rubric:: Example |
| 857 |
|
| 858 |
Multiline reStructuredText |
| 859 |
literal code block |
| 860 |
""", |
| 861 |
), |
| 862 |
################################ |
| 863 |
( |
| 864 |
""" |
| 865 |
Summary line |
| 866 |
|
| 867 |
Example:: |
| 868 |
|
| 869 |
Multiline reStructuredText |
| 870 |
literal code block |
| 871 |
|
| 872 |
""", |
| 873 |
""" |
| 874 |
Summary line |
| 875 |
|
| 876 |
Example:: |
| 877 |
|
| 878 |
Multiline reStructuredText |
| 879 |
literal code block |
| 880 |
""", |
| 881 |
), |
| 882 |
################################ |
| 883 |
( |
| 884 |
""" |
| 885 |
Summary line |
| 886 |
|
| 887 |
:Example: |
| 888 |
|
| 889 |
Multiline reStructuredText |
| 890 |
literal code block |
| 891 |
|
| 892 |
""", |
| 893 |
""" |
| 894 |
Summary line |
| 895 |
|
| 896 |
:Example: |
| 897 |
|
| 898 |
Multiline reStructuredText |
| 899 |
literal code block |
| 900 |
""", |
| 901 |
), |
| 902 |
] |
| 903 |
for docstring, expected in docstrings: |
| 904 |
actual = GoogleDocstring(docstring) |
| 905 |
assert str(actual) == expected |
| 906 |
|
| 907 |
def test_list_in_parameter_description(self): |
| 908 |
docstring = """One line summary. |
| 909 |
|
| 910 |
Parameters: |
| 911 |
no_list (int): |
| 912 |
one_bullet_empty (int): |
| 913 |
* |
| 914 |
one_bullet_single_line (int): |
| 915 |
- first line |
| 916 |
one_bullet_two_lines (int): |
| 917 |
+ first line |
| 918 |
continued |
| 919 |
two_bullets_single_line (int): |
| 920 |
- first line |
| 921 |
- second line |
| 922 |
two_bullets_two_lines (int): |
| 923 |
* first line |
| 924 |
continued |
| 925 |
* second line |
| 926 |
continued |
| 927 |
one_enumeration_single_line (int): |
| 928 |
1. first line |
| 929 |
one_enumeration_two_lines (int): |
| 930 |
1) first line |
| 931 |
continued |
| 932 |
two_enumerations_one_line (int): |
| 933 |
(iii) first line |
| 934 |
(iv) second line |
| 935 |
two_enumerations_two_lines (int): |
| 936 |
a. first line |
| 937 |
continued |
| 938 |
b. second line |
| 939 |
continued |
| 940 |
one_definition_one_line (int): |
| 941 |
item 1 |
| 942 |
first line |
| 943 |
one_definition_two_lines (int): |
| 944 |
item 1 |
| 945 |
first line |
| 946 |
continued |
| 947 |
two_definitions_one_line (int): |
| 948 |
item 1 |
| 949 |
first line |
| 950 |
item 2 |
| 951 |
second line |
| 952 |
two_definitions_two_lines (int): |
| 953 |
item 1 |
| 954 |
first line |
| 955 |
continued |
| 956 |
item 2 |
| 957 |
second line |
| 958 |
continued |
| 959 |
one_definition_blank_line (int): |
| 960 |
item 1 |
| 961 |
|
| 962 |
first line |
| 963 |
|
| 964 |
extra first line |
| 965 |
|
| 966 |
two_definitions_blank_lines (int): |
| 967 |
item 1 |
| 968 |
|
| 969 |
first line |
| 970 |
|
| 971 |
extra first line |
| 972 |
|
| 973 |
item 2 |
| 974 |
|
| 975 |
second line |
| 976 |
|
| 977 |
extra second line |
| 978 |
|
| 979 |
definition_after_inline_text (int): text line |
| 980 |
|
| 981 |
item 1 |
| 982 |
first line |
| 983 |
|
| 984 |
definition_after_normal_text (int): |
| 985 |
text line |
| 986 |
|
| 987 |
item 1 |
| 988 |
first line |
| 989 |
""" |
| 990 |
|
| 991 |
expected = """One line summary. |
| 992 |
|
| 993 |
:param no_list: |
| 994 |
:type no_list: int |
| 995 |
:param one_bullet_empty: |
| 996 |
* |
| 997 |
:type one_bullet_empty: int |
| 998 |
:param one_bullet_single_line: |
| 999 |
- first line |
| 1000 |
:type one_bullet_single_line: int |
| 1001 |
:param one_bullet_two_lines: |
| 1002 |
+ first line |
| 1003 |
continued |
| 1004 |
:type one_bullet_two_lines: int |
| 1005 |
:param two_bullets_single_line: |
| 1006 |
- first line |
| 1007 |
- second line |
| 1008 |
:type two_bullets_single_line: int |
| 1009 |
:param two_bullets_two_lines: |
| 1010 |
* first line |
| 1011 |
continued |
| 1012 |
* second line |
| 1013 |
continued |
| 1014 |
:type two_bullets_two_lines: int |
| 1015 |
:param one_enumeration_single_line: |
| 1016 |
1. first line |
| 1017 |
:type one_enumeration_single_line: int |
| 1018 |
:param one_enumeration_two_lines: |
| 1019 |
1) first line |
| 1020 |
continued |
| 1021 |
:type one_enumeration_two_lines: int |
| 1022 |
:param two_enumerations_one_line: |
| 1023 |
(iii) first line |
| 1024 |
(iv) second line |
| 1025 |
:type two_enumerations_one_line: int |
| 1026 |
:param two_enumerations_two_lines: |
| 1027 |
a. first line |
| 1028 |
continued |
| 1029 |
b. second line |
| 1030 |
continued |
| 1031 |
:type two_enumerations_two_lines: int |
| 1032 |
:param one_definition_one_line: |
| 1033 |
item 1 |
| 1034 |
first line |
| 1035 |
:type one_definition_one_line: int |
| 1036 |
:param one_definition_two_lines: |
| 1037 |
item 1 |
| 1038 |
first line |
| 1039 |
continued |
| 1040 |
:type one_definition_two_lines: int |
| 1041 |
:param two_definitions_one_line: |
| 1042 |
item 1 |
| 1043 |
first line |
| 1044 |
item 2 |
| 1045 |
second line |
| 1046 |
:type two_definitions_one_line: int |
| 1047 |
:param two_definitions_two_lines: |
| 1048 |
item 1 |
| 1049 |
first line |
| 1050 |
continued |
| 1051 |
item 2 |
| 1052 |
second line |
| 1053 |
continued |
| 1054 |
:type two_definitions_two_lines: int |
| 1055 |
:param one_definition_blank_line: |
| 1056 |
item 1 |
| 1057 |
|
| 1058 |
first line |
| 1059 |
|
| 1060 |
extra first line |
| 1061 |
:type one_definition_blank_line: int |
| 1062 |
:param two_definitions_blank_lines: |
| 1063 |
item 1 |
| 1064 |
|
| 1065 |
first line |
| 1066 |
|
| 1067 |
extra first line |
| 1068 |
|
| 1069 |
item 2 |
| 1070 |
|
| 1071 |
second line |
| 1072 |
|
| 1073 |
extra second line |
| 1074 |
:type two_definitions_blank_lines: int |
| 1075 |
:param definition_after_inline_text: text line |
| 1076 |
|
| 1077 |
item 1 |
| 1078 |
first line |
| 1079 |
:type definition_after_inline_text: int |
| 1080 |
:param definition_after_normal_text: text line |
| 1081 |
|
| 1082 |
item 1 |
| 1083 |
first line |
| 1084 |
:type definition_after_normal_text: int |
| 1085 |
""" |
| 1086 |
config = Config(napoleon_use_param=True) |
| 1087 |
actual = GoogleDocstring(docstring, config) |
| 1088 |
assert str(actual) == expected |
| 1089 |
|
| 1090 |
expected = """One line summary. |
| 1091 |
|
| 1092 |
:Parameters: * **no_list** (*int*) |
| 1093 |
* **one_bullet_empty** (*int*) -- |
| 1094 |
|
| 1095 |
* |
| 1096 |
* **one_bullet_single_line** (*int*) -- |
| 1097 |
|
| 1098 |
- first line |
| 1099 |
* **one_bullet_two_lines** (*int*) -- |
| 1100 |
|
| 1101 |
+ first line |
| 1102 |
continued |
| 1103 |
* **two_bullets_single_line** (*int*) -- |
| 1104 |
|
| 1105 |
- first line |
| 1106 |
- second line |
| 1107 |
* **two_bullets_two_lines** (*int*) -- |
| 1108 |
|
| 1109 |
* first line |
| 1110 |
continued |
| 1111 |
* second line |
| 1112 |
continued |
| 1113 |
* **one_enumeration_single_line** (*int*) -- |
| 1114 |
|
| 1115 |
1. first line |
| 1116 |
* **one_enumeration_two_lines** (*int*) -- |
| 1117 |
|
| 1118 |
1) first line |
| 1119 |
continued |
| 1120 |
* **two_enumerations_one_line** (*int*) -- |
| 1121 |
|
| 1122 |
(iii) first line |
| 1123 |
(iv) second line |
| 1124 |
* **two_enumerations_two_lines** (*int*) -- |
| 1125 |
|
| 1126 |
a. first line |
| 1127 |
continued |
| 1128 |
b. second line |
| 1129 |
continued |
| 1130 |
* **one_definition_one_line** (*int*) -- |
| 1131 |
|
| 1132 |
item 1 |
| 1133 |
first line |
| 1134 |
* **one_definition_two_lines** (*int*) -- |
| 1135 |
|
| 1136 |
item 1 |
| 1137 |
first line |
| 1138 |
continued |
| 1139 |
* **two_definitions_one_line** (*int*) -- |
| 1140 |
|
| 1141 |
item 1 |
| 1142 |
first line |
| 1143 |
item 2 |
| 1144 |
second line |
| 1145 |
* **two_definitions_two_lines** (*int*) -- |
| 1146 |
|
| 1147 |
item 1 |
| 1148 |
first line |
| 1149 |
continued |
| 1150 |
item 2 |
| 1151 |
second line |
| 1152 |
continued |
| 1153 |
* **one_definition_blank_line** (*int*) -- |
| 1154 |
|
| 1155 |
item 1 |
| 1156 |
|
| 1157 |
first line |
| 1158 |
|
| 1159 |
extra first line |
| 1160 |
* **two_definitions_blank_lines** (*int*) -- |
| 1161 |
|
| 1162 |
item 1 |
| 1163 |
|
| 1164 |
first line |
| 1165 |
|
| 1166 |
extra first line |
| 1167 |
|
| 1168 |
item 2 |
| 1169 |
|
| 1170 |
second line |
| 1171 |
|
| 1172 |
extra second line |
| 1173 |
* **definition_after_inline_text** (*int*) -- text line |
| 1174 |
|
| 1175 |
item 1 |
| 1176 |
first line |
| 1177 |
* **definition_after_normal_text** (*int*) -- text line |
| 1178 |
|
| 1179 |
item 1 |
| 1180 |
first line |
| 1181 |
""" |
| 1182 |
config = Config(napoleon_use_param=False) |
| 1183 |
actual = GoogleDocstring(docstring, config) |
| 1184 |
assert str(actual) == expected |
| 1185 |
|
| 1186 |
def test_custom_generic_sections(self): |
| 1187 |
docstrings = ( |
| 1188 |
( |
| 1189 |
"""\ |
| 1190 |
Really Important Details: |
| 1191 |
You should listen to me! |
| 1192 |
""", |
| 1193 |
""".. rubric:: Really Important Details |
| 1194 |
|
| 1195 |
You should listen to me! |
| 1196 |
""", |
| 1197 |
), |
| 1198 |
( |
| 1199 |
"""\ |
| 1200 |
Sooper Warning: |
| 1201 |
Stop hitting yourself! |
| 1202 |
""", |
| 1203 |
""":Warns: **Stop hitting yourself!** |
| 1204 |
""", |
| 1205 |
), |
| 1206 |
( |
| 1207 |
"""\ |
| 1208 |
Params Style: |
| 1209 |
arg1 (int): Description of arg1 |
| 1210 |
arg2 (str): Description of arg2 |
| 1211 |
|
| 1212 |
""", |
| 1213 |
"""\ |
| 1214 |
:Params Style: * **arg1** (*int*) -- Description of arg1 |
| 1215 |
* **arg2** (*str*) -- Description of arg2 |
| 1216 |
""", |
| 1217 |
), |
| 1218 |
( |
| 1219 |
"""\ |
| 1220 |
Returns Style: |
| 1221 |
description of custom section |
| 1222 |
|
| 1223 |
""", |
| 1224 |
""":Returns Style: description of custom section |
| 1225 |
""", |
| 1226 |
), |
| 1227 |
) |
| 1228 |
|
| 1229 |
test_config = Config( |
| 1230 |
napoleon_custom_sections=[ |
| 1231 |
'Really Important Details', |
| 1232 |
('Sooper Warning', 'warns'), |
| 1233 |
('Params Style', 'params_style'), |
| 1234 |
('Returns Style', 'returns_style'), |
| 1235 |
] |
| 1236 |
) |
| 1237 |
|
| 1238 |
for docstring, expected in docstrings: |
| 1239 |
actual = GoogleDocstring(docstring, test_config) |
| 1240 |
assert str(actual) == expected |
| 1241 |
|
| 1242 |
def test_no_index(self): |
| 1243 |
docstring = """ |
| 1244 |
Attributes: |
| 1245 |
arg |
| 1246 |
description |
| 1247 |
|
| 1248 |
Methods: |
| 1249 |
func(i, j) |
| 1250 |
description |
| 1251 |
""" |
| 1252 |
|
| 1253 |
expected = """ |
| 1254 |
.. attribute:: arg |
| 1255 |
:no-index: |
| 1256 |
|
| 1257 |
description |
| 1258 |
|
| 1259 |
.. method:: func(i, j) |
| 1260 |
:no-index: |
| 1261 |
|
| 1262 |
|
| 1263 |
description |
| 1264 |
""" # NoQA: W293 |
| 1265 |
config = Config() |
| 1266 |
actual = GoogleDocstring( |
| 1267 |
docstring, |
| 1268 |
config=config, |
| 1269 |
app=None, |
| 1270 |
what='module', |
| 1271 |
options=SimpleNamespace(no_index=True), |
| 1272 |
) |
| 1273 |
assert str(actual) == expected |
| 1274 |
|
| 1275 |
def test_keywords_with_types(self): |
| 1276 |
docstring = """\ |
| 1277 |
Do as you please |
| 1278 |
|
| 1279 |
Keyword Args: |
| 1280 |
gotham_is_yours (None): shall interfere. |
| 1281 |
""" |
| 1282 |
actual = GoogleDocstring(docstring) |
| 1283 |
expected = """\ |
| 1284 |
Do as you please |
| 1285 |
|
| 1286 |
:keyword gotham_is_yours: shall interfere. |
| 1287 |
:kwtype gotham_is_yours: None |
| 1288 |
""" |
| 1289 |
assert str(actual) == expected |
| 1290 |
|
| 1291 |
def test_pep526_annotations(self): |
| 1292 |
# Test class attributes annotations |
| 1293 |
config = Config( |
| 1294 |
napoleon_attr_annotations=True, |
| 1295 |
) |
| 1296 |
actual = GoogleDocstring( |
| 1297 |
cleandoc(PEP526GoogleClass.__doc__), |
| 1298 |
config, |
| 1299 |
app=None, |
| 1300 |
what='class', |
| 1301 |
obj=PEP526GoogleClass, |
| 1302 |
) |
| 1303 |
expected = """\ |
| 1304 |
Sample class with PEP 526 annotations and google docstring. |
| 1305 |
|
| 1306 |
.. attribute:: attr1 |
| 1307 |
|
| 1308 |
Attr1 description. |
| 1309 |
|
| 1310 |
:type: int |
| 1311 |
|
| 1312 |
.. attribute:: attr2 |
| 1313 |
|
| 1314 |
Attr2 description. |
| 1315 |
|
| 1316 |
:type: str |
| 1317 |
""" |
| 1318 |
assert str(actual) == expected |
| 1319 |
|
| 1320 |
def test_preprocess_types(self): |
| 1321 |
docstring = """\ |
| 1322 |
Do as you please |
| 1323 |
|
| 1324 |
Yield: |
| 1325 |
str:Extended |
| 1326 |
""" |
| 1327 |
actual = GoogleDocstring(docstring) |
| 1328 |
expected = """\ |
| 1329 |
Do as you please |
| 1330 |
|
| 1331 |
:Yields: *str* -- Extended |
| 1332 |
""" |
| 1333 |
assert str(actual) == expected |
| 1334 |
|
| 1335 |
config = Config(napoleon_preprocess_types=True) |
| 1336 |
actual = GoogleDocstring(docstring, config) |
| 1337 |
expected = """\ |
| 1338 |
Do as you please |
| 1339 |
|
| 1340 |
:Yields: :py:class:`str` -- Extended |
| 1341 |
""" |
| 1342 |
assert str(actual) == expected |
| 1343 |
|
| 1344 |
|
| 1345 |
class TestNumpyDocstring: |
| 1346 |
docstrings = [ |
| 1347 |
( |
| 1348 |
"""Single line summary""", |
| 1349 |
"""Single line summary""", |
| 1350 |
), |
| 1351 |
( |
| 1352 |
""" |
| 1353 |
Single line summary |
| 1354 |
|
| 1355 |
Extended description |
| 1356 |
|
| 1357 |
""", |
| 1358 |
""" |
| 1359 |
Single line summary |
| 1360 |
|
| 1361 |
Extended description |
| 1362 |
""", |
| 1363 |
), |
| 1364 |
( |
| 1365 |
""" |
| 1366 |
Single line summary |
| 1367 |
|
| 1368 |
Parameters |
| 1369 |
---------- |
| 1370 |
arg1:str |
| 1371 |
Extended |
| 1372 |
description of arg1 |
| 1373 |
""", |
| 1374 |
""" |
| 1375 |
Single line summary |
| 1376 |
|
| 1377 |
:Parameters: **arg1** (:py:class:`str`) -- Extended |
| 1378 |
description of arg1 |
| 1379 |
""", |
| 1380 |
), |
| 1381 |
( |
| 1382 |
""" |
| 1383 |
Single line summary |
| 1384 |
|
| 1385 |
Parameters |
| 1386 |
---------- |
| 1387 |
arg1:str |
| 1388 |
Extended |
| 1389 |
description of arg1 |
| 1390 |
arg2 : int |
| 1391 |
Extended |
| 1392 |
description of arg2 |
| 1393 |
|
| 1394 |
Keyword Arguments |
| 1395 |
----------------- |
| 1396 |
kwarg1:str |
| 1397 |
Extended |
| 1398 |
description of kwarg1 |
| 1399 |
kwarg2 : int |
| 1400 |
Extended |
| 1401 |
description of kwarg2 |
| 1402 |
""", |
| 1403 |
""" |
| 1404 |
Single line summary |
| 1405 |
|
| 1406 |
:Parameters: * **arg1** (:py:class:`str`) -- Extended |
| 1407 |
description of arg1 |
| 1408 |
* **arg2** (:py:class:`int`) -- Extended |
| 1409 |
description of arg2 |
| 1410 |
|
| 1411 |
:Keyword Arguments: * **kwarg1** (:py:class:`str`) -- Extended |
| 1412 |
description of kwarg1 |
| 1413 |
* **kwarg2** (:py:class:`int`) -- Extended |
| 1414 |
description of kwarg2 |
| 1415 |
""", |
| 1416 |
), |
| 1417 |
( |
| 1418 |
""" |
| 1419 |
Single line summary |
| 1420 |
|
| 1421 |
Return |
| 1422 |
------ |
| 1423 |
str |
| 1424 |
Extended |
| 1425 |
description of return value |
| 1426 |
""", |
| 1427 |
""" |
| 1428 |
Single line summary |
| 1429 |
|
| 1430 |
:returns: :py:class:`str` -- Extended |
| 1431 |
description of return value |
| 1432 |
""", |
| 1433 |
), |
| 1434 |
( |
| 1435 |
""" |
| 1436 |
Single line summary |
| 1437 |
|
| 1438 |
Returns |
| 1439 |
------- |
| 1440 |
str |
| 1441 |
Extended |
| 1442 |
description of return value |
| 1443 |
""", |
| 1444 |
""" |
| 1445 |
Single line summary |
| 1446 |
|
| 1447 |
:returns: :py:class:`str` -- Extended |
| 1448 |
description of return value |
| 1449 |
""", |
| 1450 |
), |
| 1451 |
( |
| 1452 |
""" |
| 1453 |
Single line summary |
| 1454 |
|
| 1455 |
Parameters |
| 1456 |
---------- |
| 1457 |
arg1:str |
| 1458 |
Extended description of arg1 |
| 1459 |
*args: |
| 1460 |
Variable length argument list. |
| 1461 |
**kwargs: |
| 1462 |
Arbitrary keyword arguments. |
| 1463 |
""", |
| 1464 |
""" |
| 1465 |
Single line summary |
| 1466 |
|
| 1467 |
:Parameters: * **arg1** (:py:class:`str`) -- Extended description of arg1 |
| 1468 |
* **\\*args** -- Variable length argument list. |
| 1469 |
* **\\*\\*kwargs** -- Arbitrary keyword arguments. |
| 1470 |
""", |
| 1471 |
), |
| 1472 |
( |
| 1473 |
""" |
| 1474 |
Single line summary |
| 1475 |
|
| 1476 |
Parameters |
| 1477 |
---------- |
| 1478 |
arg1:str |
| 1479 |
Extended description of arg1 |
| 1480 |
*args, **kwargs: |
| 1481 |
Variable length argument list and arbitrary keyword arguments. |
| 1482 |
""", |
| 1483 |
""" |
| 1484 |
Single line summary |
| 1485 |
|
| 1486 |
:Parameters: * **arg1** (:py:class:`str`) -- Extended description of arg1 |
| 1487 |
* **\\*args, \\*\\*kwargs** -- Variable length argument list and arbitrary keyword arguments. |
| 1488 |
""", |
| 1489 |
), |
| 1490 |
( |
| 1491 |
""" |
| 1492 |
Single line summary |
| 1493 |
|
| 1494 |
Receive |
| 1495 |
------- |
| 1496 |
arg1:str |
| 1497 |
Extended |
| 1498 |
description of arg1 |
| 1499 |
arg2 : int |
| 1500 |
Extended |
| 1501 |
description of arg2 |
| 1502 |
""", |
| 1503 |
""" |
| 1504 |
Single line summary |
| 1505 |
|
| 1506 |
:Receives: * **arg1** (:py:class:`str`) -- Extended |
| 1507 |
description of arg1 |
| 1508 |
* **arg2** (:py:class:`int`) -- Extended |
| 1509 |
description of arg2 |
| 1510 |
""", |
| 1511 |
), |
| 1512 |
( |
| 1513 |
""" |
| 1514 |
Single line summary |
| 1515 |
|
| 1516 |
Receives |
| 1517 |
-------- |
| 1518 |
arg1:str |
| 1519 |
Extended |
| 1520 |
description of arg1 |
| 1521 |
arg2 : int |
| 1522 |
Extended |
| 1523 |
description of arg2 |
| 1524 |
""", |
| 1525 |
""" |
| 1526 |
Single line summary |
| 1527 |
|
| 1528 |
:Receives: * **arg1** (:py:class:`str`) -- Extended |
| 1529 |
description of arg1 |
| 1530 |
* **arg2** (:py:class:`int`) -- Extended |
| 1531 |
description of arg2 |
| 1532 |
""", |
| 1533 |
), |
| 1534 |
( |
| 1535 |
""" |
| 1536 |
Single line summary |
| 1537 |
|
| 1538 |
Yield |
| 1539 |
----- |
| 1540 |
str |
| 1541 |
Extended |
| 1542 |
description of yielded value |
| 1543 |
""", |
| 1544 |
""" |
| 1545 |
Single line summary |
| 1546 |
|
| 1547 |
:Yields: :py:class:`str` -- Extended |
| 1548 |
description of yielded value |
| 1549 |
""", |
| 1550 |
), |
| 1551 |
( |
| 1552 |
""" |
| 1553 |
Single line summary |
| 1554 |
|
| 1555 |
Yields |
| 1556 |
------ |
| 1557 |
str |
| 1558 |
Extended |
| 1559 |
description of yielded value |
| 1560 |
""", |
| 1561 |
""" |
| 1562 |
Single line summary |
| 1563 |
|
| 1564 |
:Yields: :py:class:`str` -- Extended |
| 1565 |
description of yielded value |
| 1566 |
""", |
| 1567 |
), |
| 1568 |
] |
| 1569 |
|
| 1570 |
def test_sphinx_admonitions(self): |
| 1571 |
admonition_map = { |
| 1572 |
'Attention': 'attention', |
| 1573 |
'Caution': 'caution', |
| 1574 |
'Danger': 'danger', |
| 1575 |
'Error': 'error', |
| 1576 |
'Hint': 'hint', |
| 1577 |
'Important': 'important', |
| 1578 |
'Note': 'note', |
| 1579 |
'Tip': 'tip', |
| 1580 |
'Todo': 'todo', |
| 1581 |
'Warning': 'warning', |
| 1582 |
'Warnings': 'warning', |
| 1583 |
} |
| 1584 |
config = Config() |
| 1585 |
for section, admonition in admonition_map.items(): |
| 1586 |
# Multiline |
| 1587 |
underline = '-' * len(section) |
| 1588 |
actual = NumpyDocstring( |
| 1589 |
f'{section}\n' |
| 1590 |
f'{underline}\n' |
| 1591 |
' this is the first line\n' |
| 1592 |
'\n' |
| 1593 |
' and this is the second line\n', |
| 1594 |
config, |
| 1595 |
) |
| 1596 |
expect = ( |
| 1597 |
f'.. {admonition}::\n' |
| 1598 |
'\n' |
| 1599 |
' this is the first line\n' |
| 1600 |
' \n' |
| 1601 |
' and this is the second line\n' |
| 1602 |
) |
| 1603 |
assert str(actual) == expect |
| 1604 |
|
| 1605 |
# Single line |
| 1606 |
actual = NumpyDocstring( |
| 1607 |
f'{section}\n{"-" * len(section)}\n this is a single line\n', |
| 1608 |
config, |
| 1609 |
) |
| 1610 |
expect = f'.. {admonition}:: this is a single line\n' |
| 1611 |
assert str(actual) == expect |
| 1612 |
|
| 1613 |
def test_docstrings(self): |
| 1614 |
config = Config( |
| 1615 |
napoleon_use_param=False, |
| 1616 |
napoleon_use_rtype=False, |
| 1617 |
napoleon_use_keyword=False, |
| 1618 |
napoleon_preprocess_types=True, |
| 1619 |
) |
| 1620 |
for docstring, expected in self.docstrings: |
| 1621 |
actual = NumpyDocstring(dedent(docstring), config) |
| 1622 |
expected = dedent(expected) |
| 1623 |
assert str(actual) == expected |
| 1624 |
|
| 1625 |
def test_type_preprocessor(self): |
| 1626 |
docstring = dedent(""" |
| 1627 |
Single line summary |
| 1628 |
|
| 1629 |
Parameters |
| 1630 |
---------- |
| 1631 |
arg1:str |
| 1632 |
Extended |
| 1633 |
description of arg1 |
| 1634 |
""") |
| 1635 |
|
| 1636 |
config = Config(napoleon_preprocess_types=False, napoleon_use_param=False) |
| 1637 |
actual = NumpyDocstring(docstring, config) |
| 1638 |
expected = dedent(""" |
| 1639 |
Single line summary |
| 1640 |
|
| 1641 |
:Parameters: **arg1** (*str*) -- Extended |
| 1642 |
description of arg1 |
| 1643 |
""") |
| 1644 |
assert str(actual) == expected |
| 1645 |
|
| 1646 |
def test_parameters_with_class_reference(self): |
| 1647 |
docstring = """\ |
| 1648 |
Parameters |
| 1649 |
---------- |
| 1650 |
param1 : :class:`MyClass <name.space.MyClass>` instance |
| 1651 |
|
| 1652 |
Other Parameters |
| 1653 |
---------------- |
| 1654 |
param2 : :class:`MyClass <name.space.MyClass>` instance |
| 1655 |
|
| 1656 |
""" |
| 1657 |
|
| 1658 |
config = Config(napoleon_use_param=False) |
| 1659 |
actual = NumpyDocstring(docstring, config) |
| 1660 |
expected = """\ |
| 1661 |
:Parameters: **param1** (:class:`MyClass <name.space.MyClass>` instance) |
| 1662 |
|
| 1663 |
:Other Parameters: **param2** (:class:`MyClass <name.space.MyClass>` instance) |
| 1664 |
""" |
| 1665 |
assert str(actual) == expected |
| 1666 |
|
| 1667 |
config = Config(napoleon_use_param=True) |
| 1668 |
actual = NumpyDocstring(docstring, config) |
| 1669 |
expected = """\ |
| 1670 |
:param param1: |
| 1671 |
:type param1: :class:`MyClass <name.space.MyClass>` instance |
| 1672 |
|
| 1673 |
:param param2: |
| 1674 |
:type param2: :class:`MyClass <name.space.MyClass>` instance |
| 1675 |
""" |
| 1676 |
assert str(actual) == expected |
| 1677 |
|
| 1678 |
def test_multiple_parameters(self): |
| 1679 |
docstring = """\ |
| 1680 |
Parameters |
| 1681 |
---------- |
| 1682 |
x1, x2 : array_like |
| 1683 |
Input arrays, description of ``x1``, ``x2``. |
| 1684 |
|
| 1685 |
""" |
| 1686 |
|
| 1687 |
config = Config(napoleon_use_param=False) |
| 1688 |
actual = NumpyDocstring(docstring, config) |
| 1689 |
expected = """\ |
| 1690 |
:Parameters: **x1, x2** (*array_like*) -- Input arrays, description of ``x1``, ``x2``. |
| 1691 |
""" |
| 1692 |
assert str(actual) == expected |
| 1693 |
|
| 1694 |
config = Config(napoleon_use_param=True) |
| 1695 |
actual = NumpyDocstring(dedent(docstring), config) |
| 1696 |
expected = """\ |
| 1697 |
:param x1: Input arrays, description of ``x1``, ``x2``. |
| 1698 |
:type x1: array_like |
| 1699 |
:param x2: Input arrays, description of ``x1``, ``x2``. |
| 1700 |
:type x2: array_like |
| 1701 |
""" |
| 1702 |
assert str(actual) == expected |
| 1703 |
|
| 1704 |
def test_parameters_without_class_reference(self): |
| 1705 |
docstring = """\ |
| 1706 |
Parameters |
| 1707 |
---------- |
| 1708 |
param1 : MyClass instance |
| 1709 |
|
| 1710 |
""" |
| 1711 |
|
| 1712 |
config = Config(napoleon_use_param=False) |
| 1713 |
actual = NumpyDocstring(docstring, config) |
| 1714 |
expected = """\ |
| 1715 |
:Parameters: **param1** (*MyClass instance*) |
| 1716 |
""" |
| 1717 |
assert str(actual) == expected |
| 1718 |
|
| 1719 |
config = Config(napoleon_use_param=True) |
| 1720 |
actual = NumpyDocstring(dedent(docstring), config) |
| 1721 |
expected = """\ |
| 1722 |
:param param1: |
| 1723 |
:type param1: MyClass instance |
| 1724 |
""" |
| 1725 |
assert str(actual) == expected |
| 1726 |
|
| 1727 |
def test_see_also_refs(self): |
| 1728 |
docstring = """\ |
| 1729 |
numpy.multivariate_normal(mean, cov, shape=None, spam=None) |
| 1730 |
|
| 1731 |
See Also |
| 1732 |
-------- |
| 1733 |
some, other, funcs |
| 1734 |
otherfunc : relationship |
| 1735 |
|
| 1736 |
""" |
| 1737 |
|
| 1738 |
actual = NumpyDocstring(docstring) |
| 1739 |
|
| 1740 |
expected = """\ |
| 1741 |
numpy.multivariate_normal(mean, cov, shape=None, spam=None) |
| 1742 |
|
| 1743 |
.. seealso:: |
| 1744 |
|
| 1745 |
:py:obj:`some`, :py:obj:`other`, :py:obj:`funcs` |
| 1746 |
\n\ |
| 1747 |
:py:obj:`otherfunc` |
| 1748 |
relationship |
| 1749 |
""" |
| 1750 |
assert str(actual) == expected |
| 1751 |
|
| 1752 |
docstring = """\ |
| 1753 |
numpy.multivariate_normal(mean, cov, shape=None, spam=None) |
| 1754 |
|
| 1755 |
See Also |
| 1756 |
-------- |
| 1757 |
some, other, funcs |
| 1758 |
otherfunc : relationship |
| 1759 |
|
| 1760 |
""" |
| 1761 |
|
| 1762 |
config = Config() |
| 1763 |
app = mock.Mock() |
| 1764 |
actual = NumpyDocstring(docstring, config, app, 'method') |
| 1765 |
|
| 1766 |
expected = """\ |
| 1767 |
numpy.multivariate_normal(mean, cov, shape=None, spam=None) |
| 1768 |
|
| 1769 |
.. seealso:: |
| 1770 |
|
| 1771 |
:py:obj:`some`, :py:obj:`other`, :py:obj:`funcs` |
| 1772 |
\n\ |
| 1773 |
:py:obj:`otherfunc` |
| 1774 |
relationship |
| 1775 |
""" |
| 1776 |
assert str(actual) == expected |
| 1777 |
|
| 1778 |
docstring = """\ |
| 1779 |
numpy.multivariate_normal(mean, cov, shape=None, spam=None) |
| 1780 |
|
| 1781 |
See Also |
| 1782 |
-------- |
| 1783 |
some, other, :func:`funcs` |
| 1784 |
otherfunc : relationship |
| 1785 |
|
| 1786 |
""" |
| 1787 |
translations = { |
| 1788 |
'other': 'MyClass.other', |
| 1789 |
'otherfunc': ':func:`~my_package.otherfunc`', |
| 1790 |
} |
| 1791 |
config = Config(napoleon_type_aliases=translations) |
| 1792 |
app = mock.Mock() |
| 1793 |
actual = NumpyDocstring(docstring, config, app, 'method') |
| 1794 |
|
| 1795 |
expected = """\ |
| 1796 |
numpy.multivariate_normal(mean, cov, shape=None, spam=None) |
| 1797 |
|
| 1798 |
.. seealso:: |
| 1799 |
|
| 1800 |
:py:obj:`some`, :py:obj:`MyClass.other`, :func:`funcs` |
| 1801 |
\n\ |
| 1802 |
:func:`~my_package.otherfunc` |
| 1803 |
relationship |
| 1804 |
""" |
| 1805 |
assert str(actual) == expected |
| 1806 |
|
| 1807 |
def test_colon_in_return_type(self): |
| 1808 |
docstring = """ |
| 1809 |
Summary |
| 1810 |
|
| 1811 |
Returns |
| 1812 |
------- |
| 1813 |
:py:class:`~my_mod.my_class` |
| 1814 |
an instance of :py:class:`~my_mod.my_class` |
| 1815 |
""" |
| 1816 |
|
| 1817 |
expected = """ |
| 1818 |
Summary |
| 1819 |
|
| 1820 |
:returns: an instance of :py:class:`~my_mod.my_class` |
| 1821 |
:rtype: :py:class:`~my_mod.my_class` |
| 1822 |
""" |
| 1823 |
|
| 1824 |
config = Config() |
| 1825 |
app = mock.Mock() |
| 1826 |
actual = NumpyDocstring(docstring, config, app, 'method') |
| 1827 |
|
| 1828 |
assert str(actual) == expected |
| 1829 |
|
| 1830 |
def test_underscore_in_attribute(self): |
| 1831 |
docstring = """ |
| 1832 |
Attributes |
| 1833 |
---------- |
| 1834 |
|
| 1835 |
arg_ : type |
| 1836 |
some description |
| 1837 |
""" |
| 1838 |
|
| 1839 |
expected = """ |
| 1840 |
:ivar arg_: some description |
| 1841 |
:vartype arg_: type |
| 1842 |
""" |
| 1843 |
|
| 1844 |
config = Config(napoleon_use_ivar=True) |
| 1845 |
app = mock.Mock() |
| 1846 |
actual = NumpyDocstring(docstring, config, app, 'class') |
| 1847 |
|
| 1848 |
assert str(actual) == expected |
| 1849 |
|
| 1850 |
def test_underscore_in_attribute_strip_signature_backslash(self): |
| 1851 |
docstring = """ |
| 1852 |
Attributes |
| 1853 |
---------- |
| 1854 |
|
| 1855 |
arg_ : type |
| 1856 |
some description |
| 1857 |
""" |
| 1858 |
|
| 1859 |
expected = """ |
| 1860 |
:ivar arg\\_: some description |
| 1861 |
:vartype arg\\_: type |
| 1862 |
""" |
| 1863 |
|
| 1864 |
config = Config(napoleon_use_ivar=True) |
| 1865 |
config.strip_signature_backslash = True # ty: ignore[unresolved-attribute] |
| 1866 |
app = mock.Mock() |
| 1867 |
actual = NumpyDocstring(docstring, config, app, 'class') |
| 1868 |
|
| 1869 |
assert str(actual) == expected |
| 1870 |
|
| 1871 |
def test_return_types(self): |
| 1872 |
docstring = dedent(""" |
| 1873 |
Returns |
| 1874 |
------- |
| 1875 |
DataFrame |
| 1876 |
a dataframe |
| 1877 |
""") |
| 1878 |
expected = dedent(""" |
| 1879 |
:returns: a dataframe |
| 1880 |
:rtype: :py:class:`~pandas.DataFrame` |
| 1881 |
""") |
| 1882 |
translations = { |
| 1883 |
'DataFrame': '~pandas.DataFrame', |
| 1884 |
} |
| 1885 |
config = Config( |
| 1886 |
napoleon_use_param=True, |
| 1887 |
napoleon_use_rtype=True, |
| 1888 |
napoleon_preprocess_types=True, |
| 1889 |
napoleon_type_aliases=translations, |
| 1890 |
) |
| 1891 |
actual = NumpyDocstring(docstring, config) |
| 1892 |
assert str(actual) == expected |
| 1893 |
|
| 1894 |
def test_yield_types(self): |
| 1895 |
docstring = dedent(""" |
| 1896 |
Example Function |
| 1897 |
|
| 1898 |
Yields |
| 1899 |
------ |
| 1900 |
scalar or array-like |
| 1901 |
The result of the computation |
| 1902 |
""") |
| 1903 |
expected = dedent(""" |
| 1904 |
Example Function |
| 1905 |
|
| 1906 |
:Yields: :term:`scalar` or :py:class:`array-like <numpy.ndarray>` -- The result of the computation |
| 1907 |
""") |
| 1908 |
translations = { |
| 1909 |
'scalar': ':term:`scalar`', |
| 1910 |
'array-like': ':py:class:`array-like <numpy.ndarray>`', |
| 1911 |
} |
| 1912 |
config = Config( |
| 1913 |
napoleon_type_aliases=translations, napoleon_preprocess_types=True |
| 1914 |
) |
| 1915 |
app = mock.Mock() |
| 1916 |
actual = NumpyDocstring(docstring, config, app, 'method') |
| 1917 |
assert str(actual) == expected |
| 1918 |
|
| 1919 |
def test_raises_types(self): |
| 1920 |
docstrings = [ |
| 1921 |
( |
| 1922 |
""" |
| 1923 |
Example Function |
| 1924 |
|
| 1925 |
Raises |
| 1926 |
------ |
| 1927 |
RuntimeError |
| 1928 |
|
| 1929 |
A setting wasn't specified, or was invalid. |
| 1930 |
ValueError |
| 1931 |
|
| 1932 |
Something something value error. |
| 1933 |
|
| 1934 |
""", |
| 1935 |
""" |
| 1936 |
Example Function |
| 1937 |
|
| 1938 |
:raises RuntimeError: A setting wasn't specified, or was invalid. |
| 1939 |
:raises ValueError: Something something value error. |
| 1940 |
""", |
| 1941 |
), |
| 1942 |
################################ |
| 1943 |
( |
| 1944 |
""" |
| 1945 |
Example Function |
| 1946 |
|
| 1947 |
Raises |
| 1948 |
------ |
| 1949 |
InvalidDimensionsError |
| 1950 |
|
| 1951 |
""", |
| 1952 |
""" |
| 1953 |
Example Function |
| 1954 |
|
| 1955 |
:raises InvalidDimensionsError: |
| 1956 |
""", |
| 1957 |
), |
| 1958 |
################################ |
| 1959 |
( |
| 1960 |
""" |
| 1961 |
Example Function |
| 1962 |
|
| 1963 |
Raises |
| 1964 |
------ |
| 1965 |
Invalid Dimensions Error |
| 1966 |
|
| 1967 |
""", |
| 1968 |
""" |
| 1969 |
Example Function |
| 1970 |
|
| 1971 |
:raises Invalid Dimensions Error: |
| 1972 |
""", |
| 1973 |
), |
| 1974 |
################################ |
| 1975 |
( |
| 1976 |
""" |
| 1977 |
Example Function |
| 1978 |
|
| 1979 |
Raises |
| 1980 |
------ |
| 1981 |
Invalid Dimensions Error |
| 1982 |
With description |
| 1983 |
|
| 1984 |
""", |
| 1985 |
""" |
| 1986 |
Example Function |
| 1987 |
|
| 1988 |
:raises Invalid Dimensions Error: With description |
| 1989 |
""", |
| 1990 |
), |
| 1991 |
################################ |
| 1992 |
( |
| 1993 |
""" |
| 1994 |
Example Function |
| 1995 |
|
| 1996 |
Raises |
| 1997 |
------ |
| 1998 |
InvalidDimensionsError |
| 1999 |
If the dimensions couldn't be parsed. |
| 2000 |
|
| 2001 |
""", |
| 2002 |
""" |
| 2003 |
Example Function |
| 2004 |
|
| 2005 |
:raises InvalidDimensionsError: If the dimensions couldn't be parsed. |
| 2006 |
""", |
| 2007 |
), |
| 2008 |
################################ |
| 2009 |
( |
| 2010 |
""" |
| 2011 |
Example Function |
| 2012 |
|
| 2013 |
Raises |
| 2014 |
------ |
| 2015 |
Invalid Dimensions Error |
| 2016 |
If the dimensions couldn't be parsed. |
| 2017 |
|
| 2018 |
""", |
| 2019 |
""" |
| 2020 |
Example Function |
| 2021 |
|
| 2022 |
:raises Invalid Dimensions Error: If the dimensions couldn't be parsed. |
| 2023 |
""", |
| 2024 |
), |
| 2025 |
################################ |
| 2026 |
( |
| 2027 |
""" |
| 2028 |
Example Function |
| 2029 |
|
| 2030 |
Raises |
| 2031 |
------ |
| 2032 |
If the dimensions couldn't be parsed. |
| 2033 |
|
| 2034 |
""", |
| 2035 |
""" |
| 2036 |
Example Function |
| 2037 |
|
| 2038 |
:raises If the dimensions couldn't be parsed.: |
| 2039 |
""", |
| 2040 |
), |
| 2041 |
################################ |
| 2042 |
( |
| 2043 |
""" |
| 2044 |
Example Function |
| 2045 |
|
| 2046 |
Raises |
| 2047 |
------ |
| 2048 |
:class:`exc.InvalidDimensionsError` |
| 2049 |
|
| 2050 |
""", |
| 2051 |
""" |
| 2052 |
Example Function |
| 2053 |
|
| 2054 |
:raises exc.InvalidDimensionsError: |
| 2055 |
""", |
| 2056 |
), |
| 2057 |
################################ |
| 2058 |
( |
| 2059 |
""" |
| 2060 |
Example Function |
| 2061 |
|
| 2062 |
Raises |
| 2063 |
------ |
| 2064 |
:class:`exc.InvalidDimensionsError` |
| 2065 |
If the dimensions couldn't be parsed. |
| 2066 |
|
| 2067 |
""", |
| 2068 |
""" |
| 2069 |
Example Function |
| 2070 |
|
| 2071 |
:raises exc.InvalidDimensionsError: If the dimensions couldn't be parsed. |
| 2072 |
""", |
| 2073 |
), |
| 2074 |
################################ |
| 2075 |
( |
| 2076 |
""" |
| 2077 |
Example Function |
| 2078 |
|
| 2079 |
Raises |
| 2080 |
------ |
| 2081 |
:class:`exc.InvalidDimensionsError` |
| 2082 |
If the dimensions couldn't be parsed, |
| 2083 |
then a :class:`exc.InvalidDimensionsError` will be raised. |
| 2084 |
|
| 2085 |
""", |
| 2086 |
""" |
| 2087 |
Example Function |
| 2088 |
|
| 2089 |
:raises exc.InvalidDimensionsError: If the dimensions couldn't be parsed, |
| 2090 |
then a :class:`exc.InvalidDimensionsError` will be raised. |
| 2091 |
""", |
| 2092 |
), |
| 2093 |
################################ |
| 2094 |
( |
| 2095 |
""" |
| 2096 |
Example Function |
| 2097 |
|
| 2098 |
Raises |
| 2099 |
------ |
| 2100 |
:class:`exc.InvalidDimensionsError` |
| 2101 |
If the dimensions couldn't be parsed. |
| 2102 |
:class:`exc.InvalidArgumentsError` |
| 2103 |
If the arguments are invalid. |
| 2104 |
|
| 2105 |
""", |
| 2106 |
""" |
| 2107 |
Example Function |
| 2108 |
|
| 2109 |
:raises exc.InvalidDimensionsError: If the dimensions couldn't be parsed. |
| 2110 |
:raises exc.InvalidArgumentsError: If the arguments are invalid. |
| 2111 |
""", |
| 2112 |
), |
| 2113 |
################################ |
| 2114 |
( |
| 2115 |
""" |
| 2116 |
Example Function |
| 2117 |
|
| 2118 |
Raises |
| 2119 |
------ |
| 2120 |
CustomError |
| 2121 |
If the dimensions couldn't be parsed. |
| 2122 |
|
| 2123 |
""", |
| 2124 |
""" |
| 2125 |
Example Function |
| 2126 |
|
| 2127 |
:raises package.CustomError: If the dimensions couldn't be parsed. |
| 2128 |
""", |
| 2129 |
), |
| 2130 |
################################ |
| 2131 |
( |
| 2132 |
""" |
| 2133 |
Example Function |
| 2134 |
|
| 2135 |
Raises |
| 2136 |
------ |
| 2137 |
AnotherError |
| 2138 |
If the dimensions couldn't be parsed. |
| 2139 |
|
| 2140 |
""", |
| 2141 |
""" |
| 2142 |
Example Function |
| 2143 |
|
| 2144 |
:raises ~package.AnotherError: If the dimensions couldn't be parsed. |
| 2145 |
""", |
| 2146 |
), |
| 2147 |
################################ |
| 2148 |
( |
| 2149 |
""" |
| 2150 |
Example Function |
| 2151 |
|
| 2152 |
Raises |
| 2153 |
------ |
| 2154 |
:class:`exc.InvalidDimensionsError` |
| 2155 |
:class:`exc.InvalidArgumentsError` |
| 2156 |
|
| 2157 |
""", |
| 2158 |
""" |
| 2159 |
Example Function |
| 2160 |
|
| 2161 |
:raises exc.InvalidDimensionsError: |
| 2162 |
:raises exc.InvalidArgumentsError: |
| 2163 |
""", |
| 2164 |
), |
| 2165 |
] |
| 2166 |
for docstring, expected in docstrings: |
| 2167 |
translations = { |
| 2168 |
'CustomError': 'package.CustomError', |
| 2169 |
'AnotherError': ':py:exc:`~package.AnotherError`', |
| 2170 |
} |
| 2171 |
config = Config( |
| 2172 |
napoleon_type_aliases=translations, napoleon_preprocess_types=True |
| 2173 |
) |
| 2174 |
app = mock.Mock() |
| 2175 |
actual = NumpyDocstring(docstring, config, app, 'method') |
| 2176 |
assert str(actual) == expected |
| 2177 |
|
| 2178 |
def test_xrefs_in_return_type(self): |
| 2179 |
docstring = """ |
| 2180 |
Example Function |
| 2181 |
|
| 2182 |
Returns |
| 2183 |
------- |
| 2184 |
:class:`numpy.ndarray` |
| 2185 |
A :math:`n \\times 2` array containing |
| 2186 |
a bunch of math items |
| 2187 |
""" |
| 2188 |
expected = """ |
| 2189 |
Example Function |
| 2190 |
|
| 2191 |
:returns: A :math:`n \\times 2` array containing |
| 2192 |
a bunch of math items |
| 2193 |
:rtype: :class:`numpy.ndarray` |
| 2194 |
""" |
| 2195 |
config = Config() |
| 2196 |
app = mock.Mock() |
| 2197 |
actual = NumpyDocstring(docstring, config, app, 'method') |
| 2198 |
assert str(actual) == expected |
| 2199 |
|
| 2200 |
def test_section_header_underline_length(self): |
| 2201 |
docstrings = [ |
| 2202 |
( |
| 2203 |
""" |
| 2204 |
Summary line |
| 2205 |
|
| 2206 |
Example |
| 2207 |
- |
| 2208 |
Multiline example |
| 2209 |
body |
| 2210 |
|
| 2211 |
""", |
| 2212 |
""" |
| 2213 |
Summary line |
| 2214 |
|
| 2215 |
Example |
| 2216 |
- |
| 2217 |
Multiline example |
| 2218 |
body |
| 2219 |
""", |
| 2220 |
), |
| 2221 |
################################ |
| 2222 |
( |
| 2223 |
""" |
| 2224 |
Summary line |
| 2225 |
|
| 2226 |
Example |
| 2227 |
-- |
| 2228 |
Multiline example |
| 2229 |
body |
| 2230 |
|
| 2231 |
""", |
| 2232 |
""" |
| 2233 |
Summary line |
| 2234 |
|
| 2235 |
.. rubric:: Example |
| 2236 |
|
| 2237 |
Multiline example |
| 2238 |
body |
| 2239 |
""", |
| 2240 |
), |
| 2241 |
################################ |
| 2242 |
( |
| 2243 |
""" |
| 2244 |
Summary line |
| 2245 |
|
| 2246 |
Example |
| 2247 |
------- |
| 2248 |
Multiline example |
| 2249 |
body |
| 2250 |
|
| 2251 |
""", |
| 2252 |
""" |
| 2253 |
Summary line |
| 2254 |
|
| 2255 |
.. rubric:: Example |
| 2256 |
|
| 2257 |
Multiline example |
| 2258 |
body |
| 2259 |
""", |
| 2260 |
), |
| 2261 |
################################ |
| 2262 |
( |
| 2263 |
""" |
| 2264 |
Summary line |
| 2265 |
|
| 2266 |
Example |
| 2267 |
------------ |
| 2268 |
Multiline example |
| 2269 |
body |
| 2270 |
|
| 2271 |
""", |
| 2272 |
""" |
| 2273 |
Summary line |
| 2274 |
|
| 2275 |
.. rubric:: Example |
| 2276 |
|
| 2277 |
Multiline example |
| 2278 |
body |
| 2279 |
""", |
| 2280 |
), |
| 2281 |
] |
| 2282 |
for docstring, expected in docstrings: |
| 2283 |
actual = NumpyDocstring(docstring) |
| 2284 |
assert str(actual) == expected |
| 2285 |
|
| 2286 |
def test_list_in_parameter_description(self): |
| 2287 |
docstring = """One line summary. |
| 2288 |
|
| 2289 |
Parameters |
| 2290 |
---------- |
| 2291 |
no_list : int |
| 2292 |
one_bullet_empty : int |
| 2293 |
* |
| 2294 |
one_bullet_single_line : int |
| 2295 |
- first line |
| 2296 |
one_bullet_two_lines : int |
| 2297 |
+ first line |
| 2298 |
continued |
| 2299 |
two_bullets_single_line : int |
| 2300 |
- first line |
| 2301 |
- second line |
| 2302 |
two_bullets_two_lines : int |
| 2303 |
* first line |
| 2304 |
continued |
| 2305 |
* second line |
| 2306 |
continued |
| 2307 |
one_enumeration_single_line : int |
| 2308 |
1. first line |
| 2309 |
one_enumeration_two_lines : int |
| 2310 |
1) first line |
| 2311 |
continued |
| 2312 |
two_enumerations_one_line : int |
| 2313 |
(iii) first line |
| 2314 |
(iv) second line |
| 2315 |
two_enumerations_two_lines : int |
| 2316 |
a. first line |
| 2317 |
continued |
| 2318 |
b. second line |
| 2319 |
continued |
| 2320 |
one_definition_one_line : int |
| 2321 |
item 1 |
| 2322 |
first line |
| 2323 |
one_definition_two_lines : int |
| 2324 |
item 1 |
| 2325 |
first line |
| 2326 |
continued |
| 2327 |
two_definitions_one_line : int |
| 2328 |
item 1 |
| 2329 |
first line |
| 2330 |
item 2 |
| 2331 |
second line |
| 2332 |
two_definitions_two_lines : int |
| 2333 |
item 1 |
| 2334 |
first line |
| 2335 |
continued |
| 2336 |
item 2 |
| 2337 |
second line |
| 2338 |
continued |
| 2339 |
one_definition_blank_line : int |
| 2340 |
item 1 |
| 2341 |
|
| 2342 |
first line |
| 2343 |
|
| 2344 |
extra first line |
| 2345 |
|
| 2346 |
two_definitions_blank_lines : int |
| 2347 |
item 1 |
| 2348 |
|
| 2349 |
first line |
| 2350 |
|
| 2351 |
extra first line |
| 2352 |
|
| 2353 |
item 2 |
| 2354 |
|
| 2355 |
second line |
| 2356 |
|
| 2357 |
extra second line |
| 2358 |
|
| 2359 |
definition_after_normal_text : int |
| 2360 |
text line |
| 2361 |
|
| 2362 |
item 1 |
| 2363 |
first line |
| 2364 |
""" |
| 2365 |
|
| 2366 |
expected = """One line summary. |
| 2367 |
|
| 2368 |
:param no_list: |
| 2369 |
:type no_list: int |
| 2370 |
:param one_bullet_empty: |
| 2371 |
* |
| 2372 |
:type one_bullet_empty: int |
| 2373 |
:param one_bullet_single_line: |
| 2374 |
- first line |
| 2375 |
:type one_bullet_single_line: int |
| 2376 |
:param one_bullet_two_lines: |
| 2377 |
+ first line |
| 2378 |
continued |
| 2379 |
:type one_bullet_two_lines: int |
| 2380 |
:param two_bullets_single_line: |
| 2381 |
- first line |
| 2382 |
- second line |
| 2383 |
:type two_bullets_single_line: int |
| 2384 |
:param two_bullets_two_lines: |
| 2385 |
* first line |
| 2386 |
continued |
| 2387 |
* second line |
| 2388 |
continued |
| 2389 |
:type two_bullets_two_lines: int |
| 2390 |
:param one_enumeration_single_line: |
| 2391 |
1. first line |
| 2392 |
:type one_enumeration_single_line: int |
| 2393 |
:param one_enumeration_two_lines: |
| 2394 |
1) first line |
| 2395 |
continued |
| 2396 |
:type one_enumeration_two_lines: int |
| 2397 |
:param two_enumerations_one_line: |
| 2398 |
(iii) first line |
| 2399 |
(iv) second line |
| 2400 |
:type two_enumerations_one_line: int |
| 2401 |
:param two_enumerations_two_lines: |
| 2402 |
a. first line |
| 2403 |
continued |
| 2404 |
b. second line |
| 2405 |
continued |
| 2406 |
:type two_enumerations_two_lines: int |
| 2407 |
:param one_definition_one_line: |
| 2408 |
item 1 |
| 2409 |
first line |
| 2410 |
:type one_definition_one_line: int |
| 2411 |
:param one_definition_two_lines: |
| 2412 |
item 1 |
| 2413 |
first line |
| 2414 |
continued |
| 2415 |
:type one_definition_two_lines: int |
| 2416 |
:param two_definitions_one_line: |
| 2417 |
item 1 |
| 2418 |
first line |
| 2419 |
item 2 |
| 2420 |
second line |
| 2421 |
:type two_definitions_one_line: int |
| 2422 |
:param two_definitions_two_lines: |
| 2423 |
item 1 |
| 2424 |
first line |
| 2425 |
continued |
| 2426 |
item 2 |
| 2427 |
second line |
| 2428 |
continued |
| 2429 |
:type two_definitions_two_lines: int |
| 2430 |
:param one_definition_blank_line: |
| 2431 |
item 1 |
| 2432 |
|
| 2433 |
first line |
| 2434 |
|
| 2435 |
extra first line |
| 2436 |
:type one_definition_blank_line: int |
| 2437 |
:param two_definitions_blank_lines: |
| 2438 |
item 1 |
| 2439 |
|
| 2440 |
first line |
| 2441 |
|
| 2442 |
extra first line |
| 2443 |
|
| 2444 |
item 2 |
| 2445 |
|
| 2446 |
second line |
| 2447 |
|
| 2448 |
extra second line |
| 2449 |
:type two_definitions_blank_lines: int |
| 2450 |
:param definition_after_normal_text: text line |
| 2451 |
|
| 2452 |
item 1 |
| 2453 |
first line |
| 2454 |
:type definition_after_normal_text: int |
| 2455 |
""" |
| 2456 |
config = Config(napoleon_use_param=True) |
| 2457 |
actual = NumpyDocstring(docstring, config) |
| 2458 |
assert str(actual) == expected |
| 2459 |
|
| 2460 |
expected = """One line summary. |
| 2461 |
|
| 2462 |
:Parameters: * **no_list** (:py:class:`int`) |
| 2463 |
* **one_bullet_empty** (:py:class:`int`) -- |
| 2464 |
|
| 2465 |
* |
| 2466 |
* **one_bullet_single_line** (:py:class:`int`) -- |
| 2467 |
|
| 2468 |
- first line |
| 2469 |
* **one_bullet_two_lines** (:py:class:`int`) -- |
| 2470 |
|
| 2471 |
+ first line |
| 2472 |
continued |
| 2473 |
* **two_bullets_single_line** (:py:class:`int`) -- |
| 2474 |
|
| 2475 |
- first line |
| 2476 |
- second line |
| 2477 |
* **two_bullets_two_lines** (:py:class:`int`) -- |
| 2478 |
|
| 2479 |
* first line |
| 2480 |
continued |
| 2481 |
* second line |
| 2482 |
continued |
| 2483 |
* **one_enumeration_single_line** (:py:class:`int`) -- |
| 2484 |
|
| 2485 |
1. first line |
| 2486 |
* **one_enumeration_two_lines** (:py:class:`int`) -- |
| 2487 |
|
| 2488 |
1) first line |
| 2489 |
continued |
| 2490 |
* **two_enumerations_one_line** (:py:class:`int`) -- |
| 2491 |
|
| 2492 |
(iii) first line |
| 2493 |
(iv) second line |
| 2494 |
* **two_enumerations_two_lines** (:py:class:`int`) -- |
| 2495 |
|
| 2496 |
a. first line |
| 2497 |
continued |
| 2498 |
b. second line |
| 2499 |
continued |
| 2500 |
* **one_definition_one_line** (:py:class:`int`) -- |
| 2501 |
|
| 2502 |
item 1 |
| 2503 |
first line |
| 2504 |
* **one_definition_two_lines** (:py:class:`int`) -- |
| 2505 |
|
| 2506 |
item 1 |
| 2507 |
first line |
| 2508 |
continued |
| 2509 |
* **two_definitions_one_line** (:py:class:`int`) -- |
| 2510 |
|
| 2511 |
item 1 |
| 2512 |
first line |
| 2513 |
item 2 |
| 2514 |
second line |
| 2515 |
* **two_definitions_two_lines** (:py:class:`int`) -- |
| 2516 |
|
| 2517 |
item 1 |
| 2518 |
first line |
| 2519 |
continued |
| 2520 |
item 2 |
| 2521 |
second line |
| 2522 |
continued |
| 2523 |
* **one_definition_blank_line** (:py:class:`int`) -- |
| 2524 |
|
| 2525 |
item 1 |
| 2526 |
|
| 2527 |
first line |
| 2528 |
|
| 2529 |
extra first line |
| 2530 |
* **two_definitions_blank_lines** (:py:class:`int`) -- |
| 2531 |
|
| 2532 |
item 1 |
| 2533 |
|
| 2534 |
first line |
| 2535 |
|
| 2536 |
extra first line |
| 2537 |
|
| 2538 |
item 2 |
| 2539 |
|
| 2540 |
second line |
| 2541 |
|
| 2542 |
extra second line |
| 2543 |
* **definition_after_normal_text** (:py:class:`int`) -- text line |
| 2544 |
|
| 2545 |
item 1 |
| 2546 |
first line |
| 2547 |
""" |
| 2548 |
config = Config(napoleon_use_param=False, napoleon_preprocess_types=True) |
| 2549 |
actual = NumpyDocstring(docstring, config) |
| 2550 |
assert str(actual) == expected |
| 2551 |
|
| 2552 |
def test_token_type(self): |
| 2553 |
tokens = ( |
| 2554 |
('1', 'literal'), |
| 2555 |
('-4.6', 'literal'), |
| 2556 |
('2j', 'literal'), |
| 2557 |
("'string'", 'literal'), |
| 2558 |
('"another_string"', 'literal'), |
| 2559 |
('{1, 2}', 'literal'), |
| 2560 |
("{'va{ue', 'set'}", 'literal'), |
| 2561 |
('optional', 'control'), |
| 2562 |
('default', 'control'), |
| 2563 |
(', ', 'delimiter'), |
| 2564 |
(' of ', 'delimiter'), |
| 2565 |
(' or ', 'delimiter'), |
| 2566 |
(': ', 'delimiter'), |
| 2567 |
('True', 'obj'), |
| 2568 |
('None', 'obj'), |
| 2569 |
('name', 'obj'), |
| 2570 |
(':py:class:`Enum`', 'reference'), |
| 2571 |
) |
| 2572 |
|
| 2573 |
for token, expected in tokens: |
| 2574 |
actual = _token_type(token) |
| 2575 |
assert actual == expected |
| 2576 |
|
| 2577 |
def test_tokenize_type_spec(self): |
| 2578 |
specs = ( |
| 2579 |
'str', |
| 2580 |
'defaultdict', |
| 2581 |
'int, float, or complex', |
| 2582 |
'int or float or None, optional', |
| 2583 |
'list of list of int or float, optional', |
| 2584 |
'tuple of list of str, float, or int', |
| 2585 |
'{"F", "C", "N"}', |
| 2586 |
"{'F', 'C', 'N'}, default: 'F'", |
| 2587 |
"{'F', 'C', 'N or C'}, default 'F'", |
| 2588 |
"str, default: 'F or C'", |
| 2589 |
'int, default: None', |
| 2590 |
'int, default None', |
| 2591 |
'int, default :obj:`None`', |
| 2592 |
'"ma{icious"', |
| 2593 |
r"'with \'quotes\''", |
| 2594 |
) |
| 2595 |
|
| 2596 |
tokens = ( |
| 2597 |
['str'], |
| 2598 |
['defaultdict'], |
| 2599 |
['int', ', ', 'float', ', or ', 'complex'], |
| 2600 |
['int', ' or ', 'float', ' or ', 'None', ', ', 'optional'], |
| 2601 |
['list', ' of ', 'list', ' of ', 'int', ' or ', 'float', ', ', 'optional'], |
| 2602 |
['tuple', ' of ', 'list', ' of ', 'str', ', ', 'float', ', or ', 'int'], |
| 2603 |
['{', '"F"', ', ', '"C"', ', ', '"N"', '}'], |
| 2604 |
['{', "'F'", ', ', "'C'", ', ', "'N'", '}', ', ', 'default', ': ', "'F'"], |
| 2605 |
['{', "'F'", ', ', "'C'", ', ', "'N or C'", '}', ', ', 'default', ' ', "'F'"], |
| 2606 |
['str', ', ', 'default', ': ', "'F or C'"], |
| 2607 |
['int', ', ', 'default', ': ', 'None'], |
| 2608 |
['int', ', ', 'default', ' ', 'None'], |
| 2609 |
['int', ', ', 'default', ' ', ':obj:`None`'], |
| 2610 |
['"ma{icious"'], |
| 2611 |
[r"'with \'quotes\''"], |
| 2612 |
) # fmt: skip |
| 2613 |
|
| 2614 |
for spec, expected in zip(specs, tokens, strict=True): |
| 2615 |
actual = _tokenize_type_spec(spec) |
| 2616 |
assert actual == expected |
| 2617 |
|
| 2618 |
def test_recombine_set_tokens(self): |
| 2619 |
tokens = ( |
| 2620 |
['{', '1', ', ', '2', '}'], |
| 2621 |
['{', '"F"', ', ', '"C"', ', ', '"N"', '}', ', ', 'optional'], |
| 2622 |
['{', "'F'", ', ', "'C'", ', ', "'N'", '}', ', ', 'default', ': ', 'None'], |
| 2623 |
['{', "'F'", ', ', "'C'", ', ', "'N'", '}', ', ', 'default', ' ', 'None'], |
| 2624 |
) |
| 2625 |
|
| 2626 |
combined_tokens = ( |
| 2627 |
['{1, 2}'], |
| 2628 |
['{"F", "C", "N"}', ', ', 'optional'], |
| 2629 |
["{'F', 'C', 'N'}", ', ', 'default', ': ', 'None'], |
| 2630 |
["{'F', 'C', 'N'}", ', ', 'default', ' ', 'None'], |
| 2631 |
) |
| 2632 |
|
| 2633 |
for tokens_, expected in zip(tokens, combined_tokens, strict=True): |
| 2634 |
actual = _recombine_set_tokens(tokens_) |
| 2635 |
assert actual == expected |
| 2636 |
|
| 2637 |
def test_recombine_set_tokens_invalid(self): |
| 2638 |
tokens = ( |
| 2639 |
['{', '1', ', ', '2'], |
| 2640 |
['"F"', ', ', '"C"', ', ', '"N"', '}', ', ', 'optional'], |
| 2641 |
['{', '1', ', ', '2', ', ', 'default', ': ', 'None'], |
| 2642 |
) |
| 2643 |
combined_tokens = ( |
| 2644 |
['{1, 2'], |
| 2645 |
['"F"', ', ', '"C"', ', ', '"N"', '}', ', ', 'optional'], |
| 2646 |
['{1, 2', ', ', 'default', ': ', 'None'], |
| 2647 |
) |
| 2648 |
|
| 2649 |
for tokens_, expected in zip(tokens, combined_tokens, strict=True): |
| 2650 |
actual = _recombine_set_tokens(tokens_) |
| 2651 |
assert actual == expected |
| 2652 |
|
| 2653 |
def test_convert_numpy_type_spec(self): |
| 2654 |
translations = { |
| 2655 |
'DataFrame': 'pandas.DataFrame', |
| 2656 |
} |
| 2657 |
|
| 2658 |
specs = ( |
| 2659 |
'', |
| 2660 |
'optional', |
| 2661 |
'str, optional', |
| 2662 |
'int or float or None, default: None', |
| 2663 |
'list of tuple of str, optional', |
| 2664 |
'int, default None', |
| 2665 |
'{"F", "C", "N"}', |
| 2666 |
"{'F', 'C', 'N'}, default: 'N'", |
| 2667 |
"{'F', 'C', 'N'}, default 'N'", |
| 2668 |
'DataFrame, optional', |
| 2669 |
) |
| 2670 |
|
| 2671 |
converted = ( |
| 2672 |
'', |
| 2673 |
'*optional*', |
| 2674 |
':py:class:`str`, *optional*', |
| 2675 |
':py:class:`int` or :py:class:`float` or :py:obj:`None`, *default*: :py:obj:`None`', |
| 2676 |
':py:class:`list` of :py:class:`tuple` of :py:class:`str`, *optional*', |
| 2677 |
':py:class:`int`, *default* :py:obj:`None`', |
| 2678 |
'``{"F", "C", "N"}``', |
| 2679 |
"``{'F', 'C', 'N'}``, *default*: ``'N'``", |
| 2680 |
"``{'F', 'C', 'N'}``, *default* ``'N'``", |
| 2681 |
':py:class:`pandas.DataFrame`, *optional*', |
| 2682 |
) |
| 2683 |
|
| 2684 |
for spec, expected in zip(specs, converted, strict=True): |
| 2685 |
actual = _convert_type_spec(spec, translations=translations) |
| 2686 |
assert actual == expected |
| 2687 |
|
| 2688 |
def test_parameter_types(self): |
| 2689 |
docstring = dedent("""\ |
| 2690 |
Parameters |
| 2691 |
---------- |
| 2692 |
param1 : DataFrame |
| 2693 |
the data to work on |
| 2694 |
param2 : int or float or None, optional |
| 2695 |
a parameter with different types |
| 2696 |
param3 : dict-like, optional |
| 2697 |
a optional mapping |
| 2698 |
param4 : int or float or None, optional |
| 2699 |
a optional parameter with different types |
| 2700 |
param5 : {"F", "C", "N"}, optional |
| 2701 |
a optional parameter with fixed values |
| 2702 |
param6 : int, default None |
| 2703 |
different default format |
| 2704 |
param7 : mapping of hashable to str, optional |
| 2705 |
a optional mapping |
| 2706 |
param8 : ... or Ellipsis |
| 2707 |
ellipsis |
| 2708 |
param9 : tuple of list of int |
| 2709 |
a parameter with tuple of list of int |
| 2710 |
""") |
| 2711 |
expected = dedent("""\ |
| 2712 |
:param param1: the data to work on |
| 2713 |
:type param1: :py:class:`DataFrame` |
| 2714 |
:param param2: a parameter with different types |
| 2715 |
:type param2: :py:class:`int` or :py:class:`float` or :py:obj:`None`, *optional* |
| 2716 |
:param param3: a optional mapping |
| 2717 |
:type param3: :term:`dict-like <mapping>`, *optional* |
| 2718 |
:param param4: a optional parameter with different types |
| 2719 |
:type param4: :py:class:`int` or :py:class:`float` or :py:obj:`None`, *optional* |
| 2720 |
:param param5: a optional parameter with fixed values |
| 2721 |
:type param5: ``{"F", "C", "N"}``, *optional* |
| 2722 |
:param param6: different default format |
| 2723 |
:type param6: :py:class:`int`, *default* :py:obj:`None` |
| 2724 |
:param param7: a optional mapping |
| 2725 |
:type param7: :term:`mapping` of :term:`hashable` to :py:class:`str`, *optional* |
| 2726 |
:param param8: ellipsis |
| 2727 |
:type param8: :py:obj:`... <Ellipsis>` or :py:obj:`Ellipsis` |
| 2728 |
:param param9: a parameter with tuple of list of int |
| 2729 |
:type param9: :py:class:`tuple` of :py:class:`list` of :py:class:`int` |
| 2730 |
""") |
| 2731 |
translations = { |
| 2732 |
'dict-like': ':term:`dict-like <mapping>`', |
| 2733 |
'mapping': ':term:`mapping`', |
| 2734 |
'hashable': ':term:`hashable`', |
| 2735 |
} |
| 2736 |
config = Config( |
| 2737 |
napoleon_use_param=True, |
| 2738 |
napoleon_use_rtype=True, |
| 2739 |
napoleon_preprocess_types=True, |
| 2740 |
napoleon_type_aliases=translations, |
| 2741 |
) |
| 2742 |
actual = NumpyDocstring(docstring, config) |
| 2743 |
assert str(actual) == expected |
| 2744 |
|
| 2745 |
@pytest.mark.sphinx('html', testroot='root') |
| 2746 |
def test_token_type_invalid(self, app): |
| 2747 |
tokens = ( |
| 2748 |
'{1, 2', |
| 2749 |
'}', |
| 2750 |
"'abc", |
| 2751 |
"def'", |
| 2752 |
'"ghi', |
| 2753 |
'jkl"', |
| 2754 |
) |
| 2755 |
errors = ( |
| 2756 |
r'.+: invalid value set \(missing closing brace\):', |
| 2757 |
r'.+: invalid value set \(missing opening brace\):', |
| 2758 |
r'.+: malformed string literal \(missing closing quote\):', |
| 2759 |
r'.+: malformed string literal \(missing opening quote\):', |
| 2760 |
r'.+: malformed string literal \(missing closing quote\):', |
| 2761 |
r'.+: malformed string literal \(missing opening quote\):', |
| 2762 |
) |
| 2763 |
for token, error in zip(tokens, errors, strict=True): |
| 2764 |
try: |
| 2765 |
_token_type(token) |
| 2766 |
finally: |
| 2767 |
raw_warnings = app.warning.getvalue() |
| 2768 |
warnings = [w for w in raw_warnings.split('\n') if w.strip()] |
| 2769 |
|
| 2770 |
assert len(warnings) == 1 |
| 2771 |
assert re.compile(error).match(warnings[0]) |
| 2772 |
app.warning.truncate(0) |
| 2773 |
|
| 2774 |
@pytest.mark.parametrize( |
| 2775 |
('name', 'expected'), |
| 2776 |
[ |
| 2777 |
('x, y, z', 'x, y, z'), |
| 2778 |
('*args, **kwargs', r'\*args, \*\*kwargs'), |
| 2779 |
('*x, **y', r'\*x, \*\*y'), |
| 2780 |
], |
| 2781 |
) |
| 2782 |
def test_escape_args_and_kwargs(self, name, expected): |
| 2783 |
numpy_docstring = NumpyDocstring('') |
| 2784 |
actual = numpy_docstring._escape_args_and_kwargs(name) |
| 2785 |
|
| 2786 |
assert actual == expected |
| 2787 |
|
| 2788 |
def test_pep526_annotations(self): |
| 2789 |
# test class attributes annotations |
| 2790 |
config = Config( |
| 2791 |
napoleon_attr_annotations=True, |
| 2792 |
) |
| 2793 |
actual = NumpyDocstring( |
| 2794 |
cleandoc(PEP526NumpyClass.__doc__), |
| 2795 |
config, |
| 2796 |
app=None, |
| 2797 |
what='class', |
| 2798 |
obj=PEP526NumpyClass, |
| 2799 |
) |
| 2800 |
expected = """\ |
| 2801 |
Sample class with PEP 526 annotations and numpy docstring |
| 2802 |
|
| 2803 |
.. attribute:: attr1 |
| 2804 |
|
| 2805 |
Attr1 description |
| 2806 |
|
| 2807 |
:type: int |
| 2808 |
|
| 2809 |
.. attribute:: attr2 |
| 2810 |
|
| 2811 |
Attr2 description |
| 2812 |
|
| 2813 |
:type: str |
| 2814 |
""" |
| 2815 |
print(actual) |
| 2816 |
assert str(actual) == expected |
| 2817 |
|
| 2818 |
|
| 2819 |
@pytest.mark.sphinx( |
| 2820 |
'text', |
| 2821 |
testroot='ext-napoleon', |
| 2822 |
confoverrides={ |
| 2823 |
'autodoc_typehints': 'description', |
| 2824 |
'autodoc_typehints_description_target': 'all', |
| 2825 |
}, |
| 2826 |
) |
| 2827 |
def test_napoleon_and_autodoc_typehints_description_all(app: SphinxTestApp) -> None: |
| 2828 |
app.build() |
| 2829 |
content = (app.outdir / 'typehints.txt').read_text(encoding='utf-8') |
| 2830 |
assert content == ( |
| 2831 |
'typehints\n' |
| 2832 |
'*********\n' |
| 2833 |
'\n' |
| 2834 |
'mypackage.typehints.hello(x, *args, **kwargs)\n' |
| 2835 |
'\n' |
| 2836 |
' Parameters:\n' |
| 2837 |
' * **x** (*int*) -- X\n' |
| 2838 |
'\n' |
| 2839 |
' * ***args** (*int*) -- Additional arguments.\n' |
| 2840 |
'\n' |
| 2841 |
' * ****kwargs** (*int*) -- Extra arguments.\n' |
| 2842 |
'\n' |
| 2843 |
' Return type:\n' |
| 2844 |
' None\n' |
| 2845 |
) |
| 2846 |
|
| 2847 |
|
| 2848 |
@pytest.mark.sphinx( |
| 2849 |
'text', |
| 2850 |
testroot='ext-napoleon', |
| 2851 |
confoverrides={ |
| 2852 |
'autodoc_typehints': 'description', |
| 2853 |
'autodoc_typehints_description_target': 'documented_params', |
| 2854 |
}, |
| 2855 |
) |
| 2856 |
def test_napoleon_and_autodoc_typehints_description_documented_params( |
| 2857 |
app: SphinxTestApp, |
| 2858 |
) -> None: |
| 2859 |
app.build() |
| 2860 |
content = (app.outdir / 'typehints.txt').read_text(encoding='utf-8') |
| 2861 |
assert content == ( |
| 2862 |
'typehints\n' |
| 2863 |
'*********\n' |
| 2864 |
'\n' |
| 2865 |
'mypackage.typehints.hello(x, *args, **kwargs)\n' |
| 2866 |
'\n' |
| 2867 |
' Parameters:\n' |
| 2868 |
' * **x** (*int*) -- X\n' |
| 2869 |
'\n' |
| 2870 |
' * ***args** (*int*) -- Additional arguments.\n' |
| 2871 |
'\n' |
| 2872 |
' * ****kwargs** (*int*) -- Extra arguments.\n' |
| 2873 |
) |
| 2874 |
|
| 2875 |
|
| 2876 |
@pytest.mark.sphinx('html', testroot='ext-napoleon-paramtype', freshenv=True) |
| 2877 |
def test_napoleon_keyword_and_paramtype(app, tmp_path): |
| 2878 |
inv_file = tmp_path / 'objects.inv' |
| 2879 |
inv_file.write_bytes( |
| 2880 |
b"""\ |
| 2881 |
# Sphinx inventory version 2 |
| 2882 |
# Project: Intersphinx Test |
| 2883 |
# Version: 42 |
| 2884 |
# The remainder of this file is compressed using zlib. |
| 2885 |
""" |
| 2886 |
+ zlib.compress(b"""\ |
| 2887 |
None py:data 1 none.html - |
| 2888 |
list py:class 1 list.html - |
| 2889 |
int py:class 1 int.html - |
| 2890 |
""") |
| 2891 |
) |
| 2892 |
app.config.intersphinx_mapping = {'python': ('127.0.0.1:5555', str(inv_file))} |
| 2893 |
validate_intersphinx_mapping(app, app.config) |
| 2894 |
load_mappings(app) |
| 2895 |
|
| 2896 |
app.build(force_all=True) |
| 2897 |
|
| 2898 |
etree = etree_parse(app.outdir / 'index.html') |
| 2899 |
|
| 2900 |
for name, typename in product( |
| 2901 |
('keyword', 'kwarg', 'kwparam'), |
| 2902 |
('paramtype', 'kwtype'), |
| 2903 |
): |
| 2904 |
param = f'{name}_{typename}' |
| 2905 |
li_ = list(etree.findall(f'.//li/p/strong[.="{param}"]/../..')) |
| 2906 |
assert len(li_) == 1 |
| 2907 |
li = li_[0] |
| 2908 |
|
| 2909 |
text = li.text or ''.join(li.itertext()) |
| 2910 |
assert text == f'{param} (list[int]) \u2013 some param' |
| 2911 |
|
| 2912 |
a_ = list(li.findall('.//a[@class="reference external"]')) |
| 2913 |
|
| 2914 |
assert len(a_) == 2 |
| 2915 |
for a, uri in zip(a_, ('list.html', 'int.html'), strict=True): |
| 2916 |
assert a.attrib['href'] == f'127.0.0.1:5555/{uri}' |
| 2917 |
assert a.attrib['title'] == '(in Intersphinx Test v42)' |