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 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
|
# Copyright (c) Meta Platforms, Inc. and affiliates.
# SPDX-License-Identifier: LGPL-2.1-or-later
import ast
from typing import Any, List, Optional, Pattern, Sequence, Tuple, cast
from drgndoc.namespace import BoundNode, Namespace, ResolvedNode
from drgndoc.parse import (
Class,
DocumentedNode,
Function,
FunctionSignature,
Module,
Variable,
)
from drgndoc.visitor import NodeVisitor
def _is_name_constant(node: ast.Constant) -> bool:
return node.value is None or node.value is True or node.value is False
class _FormatVisitor(NodeVisitor):
def __init__(
self,
namespace: Namespace,
substitutions: Sequence[Tuple[Pattern[str], Any]],
modules: Sequence[BoundNode[Module]],
classes: Sequence[BoundNode[Class]],
context_module: Optional[str],
context_class: Optional[str],
) -> None:
self._namespace = namespace
self._substitutions = substitutions
self._modules = modules
self._classes = classes
self._context_module = context_module
self._context_class = context_class
self._parts: List[str] = []
def visit( # type: ignore[override] # This is intentionally incompatible with the supertype.
self,
node: ast.AST,
*,
rst: bool,
qualify_typing: bool,
qualify_typeshed: bool,
) -> str:
self._rst = rst
self._qualify_typing = qualify_typing
self._qualify_typeshed = qualify_typeshed
super().visit(node)
ret = "".join(self._parts)
self._parts.clear()
return ret
def generic_visit(self, node: ast.AST) -> None:
raise NotImplementedError(
f"{node.__class__.__name__} formatting is not implemented"
)
@staticmethod
def _check_ctx_is_load(node: Any) -> None:
if not isinstance(node.ctx, ast.Load):
raise NotImplementedError(
f"{node.ctx.__class__.__name__} formatting is not implemented"
)
def visit_Constant(
self, node: ast.Constant, parent: Optional[ast.AST], sibling: Optional[ast.AST]
) -> None:
if node.value is ...:
self._parts.append("...")
else:
obj = self._rst and _is_name_constant(node)
quote = self._rst and not isinstance(node.value, (int, float))
if obj:
self._parts.append(":py:obj:`")
elif quote:
self._parts.append("``")
self._parts.append(repr(node.value))
if obj:
self._parts.append("`")
elif quote:
self._parts.append("``")
def _append_resolved_name(self, name: str) -> None:
if self._rst:
self._parts.append(":py:obj:`")
resolved = self._namespace.resolve_name_in_scope(
self._modules, self._classes, name
)
if isinstance(resolved, ResolvedNode):
target = resolved.qualified_name()
else:
target = resolved
for pattern, repl in self._substitutions:
target, num_subs = pattern.subn(repl, target)
if num_subs:
break
title = target
if not self._qualify_typing and title.startswith("typing."):
title = title[len("typing.") :]
elif not self._qualify_typeshed and title.startswith("_typeshed."):
title = title[len("_typeshed.") :]
elif self._context_module and title.startswith(self._context_module + "."):
title = title[len(self._context_module) + 1 :]
if self._context_class and title.startswith(self._context_class + "."):
title = title[len(self._context_class) + 1 :]
self._parts.append(title)
if self._rst:
if title != target:
self._parts.append(" <")
self._parts.append(target)
self._parts.append(">")
self._parts.append("`")
def visit_Name(
self, node: ast.Name, parent: Optional[ast.AST], sibling: Optional[ast.AST]
) -> None:
self._check_ctx_is_load(node)
self._append_resolved_name(node.id)
def visit_Attribute(
self, node: ast.Attribute, parent: Optional[ast.AST], sibling: Optional[ast.AST]
) -> None:
self._check_ctx_is_load(node)
name_stack = [node.attr]
while True:
value = node.value
if isinstance(value, ast.Attribute):
name_stack.append(value.attr)
node = value
continue
elif isinstance(value, ast.Name):
name_stack.append(value.id)
name_stack.reverse()
self._append_resolved_name(".".join(name_stack))
elif isinstance(value, ast.Constant) and _is_name_constant(value):
name_stack.append(repr(value.value))
name_stack.reverse()
self._append_resolved_name(".".join(name_stack))
elif isinstance(value, ast.Constant) and not isinstance(
value.value, (type(...), int, float)
):
name_stack.append(repr(value.value))
name_stack.reverse()
if self._rst:
self._parts.append("``")
self._parts.append(".".join(name_stack))
if self._rst:
self._parts.append("``")
else:
self._visit(value, node, None)
name_stack.append("")
name_stack.reverse()
if isinstance(value, ast.Constant) and isinstance(value.value, int):
# "1.foo()" is a syntax error without parentheses or an
# extra space.
self._parts.append(" ")
elif self._rst:
# Make sure the "``" doesn't get squashed into a previous
# special character.
self._parts.append("\\ ")
if self._rst:
self._parts.append("``")
self._parts.append(".".join(name_stack))
if self._rst:
self._parts.append("``")
break
def visit_Subscript(
self, node: ast.Subscript, parent: Optional[ast.AST], sibling: Optional[ast.AST]
) -> None:
self._check_ctx_is_load(node)
self._visit(node.value, node, None)
if self._rst:
self._parts.append("\\")
self._parts.append("[")
self._visit(node.slice, node, None)
if self._rst:
self._parts.append("\\")
self._parts.append("]")
def visit_Tuple(
self, node: ast.Tuple, parent: Optional[ast.AST], sibling: Optional[ast.AST]
) -> None:
self._check_ctx_is_load(node)
parens = (
len(node.elts) == 0
or not isinstance(parent, ast.Subscript)
or node is not parent.slice
)
if parens:
self._parts.append("(")
for i, elt in enumerate(node.elts):
if i > 0:
self._parts.append(", ")
self._visit(elt, node, node.elts[i + 1] if i < len(node.elts) - 1 else None)
if len(node.elts) == 1:
self._parts.append(",")
if parens:
self._parts.append(")")
def visit_List(
self, node: ast.List, parent: Optional[ast.AST], sibling: Optional[ast.AST]
) -> None:
self._check_ctx_is_load(node)
if self._rst:
self._parts.append("\\")
self._parts.append("[")
for i, elt in enumerate(node.elts):
if i > 0:
self._parts.append(", ")
self._visit(elt, node, node.elts[i + 1] if i < len(node.elts) - 1 else None)
if self._rst:
self._parts.append("\\")
self._parts.append("]")
def visit_UnaryOp(
self, node: ast.UnaryOp, parent: Optional[ast.AST], sibling: Optional[ast.AST]
) -> None:
if isinstance(node.op, ast.UAdd):
self._parts.append("+")
elif isinstance(node.op, ast.USub):
self._parts.append("-")
elif isinstance(node.op, ast.Not):
self._parts.append("not ")
elif isinstance(node.op, ast.Invert):
self._parts.append("~")
else:
raise NotImplementedError(
f"{node.op.__class__.__name__} formatting is not implemented"
)
parens = not isinstance(node.operand, (ast.Constant, ast.Name))
if parens:
self._parts.append("(")
self._visit(node.operand, node, None)
if parens:
self._parts.append(")")
class Formatter:
def __init__(
self,
namespace: Namespace,
substitutions: Sequence[Tuple[Pattern[str], Any]] = (),
) -> None:
self._namespace = namespace
self._substitutions = substitutions
def _format_function_signature(
self,
node: FunctionSignature,
modules: Sequence[BoundNode[Module]],
classes: Sequence[BoundNode[Class]],
context_module: Optional[str],
context_class: Optional[str],
rst: bool,
want_rtype: bool,
) -> Tuple[str, List[str]]:
visitor = _FormatVisitor(
self._namespace,
self._substitutions,
modules,
classes,
context_module,
context_class,
)
assert node.docstring is not None
lines = node.docstring.splitlines()
if rst:
lines = [" " + line for line in lines]
signature = ["("]
need_comma = False
def visit_arg(
arg: ast.arg, default: Optional[ast.expr] = None, name: Optional[str] = None
) -> None:
nonlocal need_comma
if need_comma:
signature.append(", ")
signature.append(arg.arg if name is None else name)
default_sep = "="
if arg.annotation:
signature.append(": ")
signature.append(
visitor.visit(
arg.annotation,
rst=False,
qualify_typing=rst,
qualify_typeshed=False,
)
)
default_sep = " = "
if default:
signature.append(default_sep)
signature.append(
visitor.visit(
default, rst=False, qualify_typing=True, qualify_typeshed=True
)
)
need_comma = True
try:
posargs = node.args.posonlyargs + node.args.args
num_posonlyargs = len(node.args.posonlyargs)
except AttributeError:
posargs = node.args.args
num_posonlyargs = 0
# Type checkers treat parameters with names that begin but don't end
# with __ as positional-only:
# https://typing.readthedocs.io/en/latest/spec/historical.html#positional-only-parameters
# We translate those to the PEP 570 syntax.
def _is_posonly(arg: ast.arg) -> bool:
return arg.arg.startswith("__") and not arg.arg.endswith("__")
num_pep_570_posonlyargs = num_posonlyargs
if (
num_posonlyargs == 0
and classes
and not node.has_decorator("staticmethod")
and len(posargs) > 1
and _is_posonly(posargs[1])
):
num_posonlyargs = 2
while num_posonlyargs < len(posargs) and _is_posonly(posargs[num_posonlyargs]):
num_posonlyargs += 1
for i, arg in enumerate(posargs):
default: Optional[ast.expr]
if i >= len(posargs) - len(node.args.defaults):
default = node.args.defaults[
i - (len(posargs) - len(node.args.defaults))
]
else:
default = None
if i == 0 and classes and not node.has_decorator("staticmethod"):
# Skip self for methods and cls for class methods.
continue
visit_arg(
arg,
default,
name=(
arg.arg[2:]
if num_pep_570_posonlyargs <= i < num_posonlyargs
else arg.arg
),
)
if i == num_posonlyargs - 1:
signature.append(", /")
if node.args.vararg:
visit_arg(node.args.vararg, name="*" + node.args.vararg.arg)
if node.args.kwonlyargs:
if not node.args.vararg:
if need_comma:
signature.append(", ")
signature.append("*")
need_comma = True
for i, arg in enumerate(node.args.kwonlyargs):
visit_arg(arg, node.args.kw_defaults[i])
if node.args.kwarg:
visit_arg(node.args.kwarg, name="**" + node.args.kwarg.arg)
signature.append(")")
if want_rtype and node.returns:
signature.append(" -> ")
signature.append(
visitor.visit(
node.returns, rst=False, qualify_typing=rst, qualify_typeshed=False
)
)
return "".join(signature), lines
def _format_class(
self,
resolved: ResolvedNode[Class],
name: str,
context_module: Optional[str] = None,
context_class: Optional[str] = None,
rst: bool = True,
) -> List[str]:
node = resolved.node
init_signatures: List[FunctionSignature] = []
try:
init = resolved.attr("__init__")
except KeyError:
pass
else:
if isinstance(init.node, Function):
init_signatures = [
signature
for signature in init.node.signatures
if signature.docstring is not None
]
init_context_class = resolved.name
if context_class:
init_context_class = context_class + "." + init_context_class
lines = []
if rst and len(init_signatures) == 1 and node.docstring is None:
class_signature, class_docstring_lines = self._format_function_signature(
init_signatures[0],
init.modules,
init.classes,
context_module,
init_context_class,
rst,
False,
)
del init_signatures[0]
else:
class_signature = ""
class_docstring_lines = (
node.docstring.splitlines() if node.docstring else []
)
if rst:
lines.append(f".. py:class:: {name}{class_signature}")
if node.bases:
visitor = _FormatVisitor(
self._namespace,
self._substitutions,
resolved.modules,
resolved.classes,
context_module,
context_class,
)
bases = [
visitor.visit(
base, rst=rst, qualify_typing=False, qualify_typeshed=False
)
for base in node.bases
]
if lines:
lines.append("")
lines.append((" " if rst else "") + "Bases: " + ", ".join(bases))
if class_docstring_lines:
if lines:
lines.append("")
if rst:
for line in class_docstring_lines:
lines.append(" " + line)
else:
lines.extend(class_docstring_lines)
for i, signature_node in enumerate(init_signatures):
if lines:
lines.append("")
signature, signature_lines = self._format_function_signature(
signature_node,
init.modules,
init.classes,
context_module,
init_context_class,
rst,
False,
)
if rst:
lines.append(f" .. py:method:: {name}{signature}")
lines.append(" :noindex:")
elif signature:
lines.append(f"{name}{signature}")
lines.append("")
if rst:
for line in signature_lines:
lines.append(" " + line)
else:
lines.extend(signature_lines)
return lines
def _format_function(
self,
resolved: ResolvedNode[Function],
name: str,
context_module: Optional[str] = None,
context_class: Optional[str] = None,
rst: bool = True,
) -> List[str]:
node = resolved.node
lines = []
for i, signature_node in enumerate(
signature
for signature in node.signatures
if signature.docstring is not None
):
if i > 0:
lines.append("")
signature, signature_lines = self._format_function_signature(
signature_node,
resolved.modules,
resolved.classes,
context_module,
context_class,
rst,
True,
)
if rst:
directive = "py:method" if resolved.classes else "py:function"
lines.append(f".. {directive}:: {name}{signature}")
if i > 0:
lines.append(" :noindex:")
if node.async_:
lines.append(" :async:")
if signature_node.has_decorator("classmethod") or name in (
"__init_subclass__",
"__class_getitem__",
):
lines.append(" :classmethod:")
if signature_node.has_decorator("staticmethod"):
lines.append(" :staticmethod:")
else:
lines.append(f"{name}{signature}")
if signature_lines:
lines.append("")
lines.extend(signature_lines)
return lines
def _format_variable(
self,
resolved: ResolvedNode[Variable],
name: str,
context_module: Optional[str],
context_class: Optional[str],
rst: bool,
) -> List[str]:
node = resolved.node
assert node.docstring is not None
docstring_lines = node.docstring.splitlines()
visitor = _FormatVisitor(
self._namespace,
self._substitutions,
resolved.modules,
resolved.classes,
context_module,
context_class,
)
if rst:
directive = "py:attribute" if resolved.classes else "py:data"
lines = [f".. {directive}:: {name}"]
if node.annotation:
lines.append(
" :type: "
+ visitor.visit(
node.annotation,
rst=False,
qualify_typing=True,
qualify_typeshed=False,
)
)
if docstring_lines:
lines.append("")
for line in docstring_lines:
lines.append(" " + line)
return lines
else:
if node.annotation:
if docstring_lines:
docstring_lines.insert(0, "")
docstring_lines.insert(
0,
visitor.visit(
node.annotation,
rst=False,
qualify_typing=False,
qualify_typeshed=False,
),
)
return docstring_lines
def format(
self,
resolved: ResolvedNode[DocumentedNode],
name: Optional[str] = None,
context_module: Optional[str] = None,
context_class: Optional[str] = None,
rst: bool = True,
) -> List[str]:
node = resolved.node
if not node.has_docstring():
return []
if name is None:
name = resolved.name
if context_module is None and resolved.modules:
context_module = ".".join([module.name for module in resolved.modules])
if context_class is None and resolved.classes:
context_module = ".".join([class_.name for class_ in resolved.classes])
if isinstance(node, Class):
return self._format_class(
cast(ResolvedNode[Class], resolved),
name,
context_module,
context_class,
rst,
)
elif isinstance(node, Function):
return self._format_function(
cast(ResolvedNode[Function], resolved),
name,
context_module,
context_class,
rst,
)
elif isinstance(node, Variable):
return self._format_variable(
cast(ResolvedNode[Variable], resolved),
name,
context_module,
context_class,
rst,
)
else:
assert isinstance(node, Module)
assert node.docstring is not None
return node.docstring.splitlines()
|