Skip to content

Add Support For Attributes In Docstrings #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

# -- Project information -----------------------------------------------------

project = "Bot Core"
project = "Pydis Core"
copyright = "2021, Python Discord"
author = "Python Discord"

Expand Down
34 changes: 30 additions & 4 deletions docs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import inspect
import os
import subprocess
import types
import typing
from pathlib import Path

import docstring_parser
import docutils.nodes
import docutils.parsers.rst.states
import git
Expand All @@ -25,6 +27,18 @@ def get_build_root() -> Path:
return root


def is_attribute(module: types.ModuleType, parameter: str) -> bool:
"""Returns true if `parameter` is an attribute of `module`."""
docs = docstring_parser.parse(inspect.getdoc(module), docstring_parser.DocstringStyle.GOOGLE)
for param in docs.params:
# The docstring_parser library can mis-parse arguments like `arg (:obj:`str`)` as `arg (`
# which would create a false-negative below, so we just strip away the extra parenthesis.
if param.args[0] == "attribute" and param.arg_name.rstrip(" (") == parameter:
return True

return False


def linkcode_resolve(repo_link: str, domain: str, info: dict[str, str]) -> typing.Optional[str]:
"""
Function called by linkcode to get the URL for a given resource.
Expand Down Expand Up @@ -59,7 +73,15 @@ def linkcode_resolve(repo_link: str, domain: str, info: dict[str, str]) -> typin

symbol = [module]
for name in symbol_name.split("."):
symbol.append(getattr(symbol[-1], name))
try:
symbol.append(getattr(symbol[-1], name))
except AttributeError as e:
# This could be caused by trying to link a class attribute
if is_attribute(symbol[-1], name):
break
else:
raise e

symbol_name = name

try:
Expand Down Expand Up @@ -138,9 +160,13 @@ def cleanup() -> None:
content = file.read_text(encoding="utf-8").splitlines(keepends=True)

# Rename the extension to be less wordy
# Example: pydis_core.exts -> pydis_core Exts
title = content[0].split()[0].strip().replace("pydis_core.", "").replace(".", " ").title()
title = f"{title}\n{'=' * len(title)}\n\n"
# Example: pydis_core.exts -> Exts
title = content[0].split()[0].strip().replace("\\", "").replace("pydis_core.", "").replace(".", " ")
if "pydis_core" in title:
# Root title: pydis_core -> pydis core
title = title.replace("_", " ")

title = f"{title.title()}\n{'=' * len(title)}\n\n"
content = title, *content[3:]

file.write_text("".join(content), encoding="utf-8")
Expand Down
Loading