Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Add pydocstyle and guide for editing templates (#363)

sarayourfriend1dd59ffbded0
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 691b63c26..4284ce81c 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -63,6 +63,20 @@ repos:
args:
- --safe
+ - repo: https://github.com/pycqa/pydocstyle
+ rev: 6.2.2 # 6.2.3 is slightly broken
+ hooks:
+ - id: pydocstyle
+ args:
+ - --convention=pep257
+ # Additional ignore reasons:
+ # D1xx: we do not want to force contributors to write redundant or useless docstrings
+ # D202: additional whitespace helps with readability
+ # D211: same as D202
+ # See the following documentation for what each rule does:
+ # https://www.pydocstyle.org/en/6.2.3/error_codes.html#error-codes
+ - --add-ignore=D1,D202,D211
+
- repo: https://github.com/pre-commit/mirrors-eslint
rev: v8.15.0
hooks:
diff --git a/automations/python/issues_with_prs.py b/automations/python/issues_with_prs.py
index 1bd8dc4c3..0e5fbceae 100755
--- a/automations/python/issues_with_prs.py
+++ b/automations/python/issues_with_prs.py
@@ -51,8 +51,7 @@ def get_open_issues_with_prs(
repo_names: list[str],
) -> list[Issue]:
"""
- From given repos in the given organization, retrieve a list of open issues
- that have PRs linked to them.
+ Retrieve open issues with linked PRs.
:param gh: the GitHub client
:param org_handle: the name of the org in which to look for issues
@@ -86,8 +85,9 @@ def get_open_issues_with_prs(
def get_issue_cards(col: ProjectColumn) -> list[tuple[ProjectCard, Issue]]:
"""
- Get all cards linked to issues in the given column. This excludes cards that
- either have no links (just notes) or are linked to PRs.
+ Get all cards linked to issues in the given column.
+
+ This excludes cards that either have no links (just notes) or are linked to PRs.
:param col: the project column from which to retrieve issue cards
:return: the list of cards linked to issues in the given column
diff --git a/automations/python/label_pr.py b/automations/python/label_pr.py
index 25c8576c5..d3dbf0974 100644
--- a/automations/python/label_pr.py
+++ b/automations/python/label_pr.py
@@ -81,8 +81,11 @@ def get_pull_request(gh: Github, html_url: str) -> PullRequest:
def get_authenticated_html(url: str) -> str:
"""
- Login to the GitHub UI using the username and password, followed by 2FA. Then
- navigate to the specified URL as the authenticated user and scrape the text body.
+ Retrieve HTML for GitHub webpages that require authentication to view.
+
+ Login to the GitHub UI using the username and password, followed by 2FA.
+ Then navigate to the specified URL as the authenticated user and scrape the
+ text body.
:param url: the URL to scrape after authenticating in GitHub
:return: the text content of the scraped page
@@ -106,9 +109,10 @@ def get_authenticated_html(url: str) -> str:
def get_linked_issues(url: str) -> list[str]:
"""
- Get the list of linked issues from the GitHub UI by parsing the HTML. This is a
- workaround because GitHub API does not provide a reliable way to find the linked
- issues for a PR.
+ Get the list of linked issues from the GitHub UI by parsing the HTML.
+
+ This is a workaround because GitHub API does not provide a reliable way
+ to find the linked issues for a PR.
If the page returns a 404 response, it is assumed that the page belongs to a private
repository and needs authentication. In these cases, ``get_authenticated_html`` is
diff --git a/automations/python/models/label.py b/automations/python/models/label.py
index b5cec3d70..a973d4daa 100644
--- a/automations/python/models/label.py
+++ b/automations/python/models/label.py
@@ -3,8 +3,11 @@ from shared.data import get_data
class Label:
+
"""
- This model represents a single label. A label is defined by four parameters
+ Represents a single label.
+
+ A label is defined by four parameters:
- name, which appears on the label
- description, which describes it in a little more detail
- emoji, which is a pictorial representation of the purpose of the label
@@ -33,10 +36,11 @@ class Label:
@property
def color(self) -> str:
"""
- Return the color to use on the emoji label, given as a 6-digit
- hexadecimal code without the prefix '#'. Labels can have their color
- specified as a constant and if missing inherit color from the parent
- group. If not resolved, the color defaults to pure black.
+ Get the hex code (sans '#') to use on the emoji label.
+
+ Labels can have their color specified as a constant and if
+ missing inherit color from the parent group. If not resolved,
+ the color defaults to pure black.
:return: the 6-digit hexadecimal code of the background color
"""
@@ -55,10 +59,11 @@ class Label:
@property
def qualified_name(self) -> str:
"""
- Return the fully qualified name of the label. Most label groups prefix
- the group name to the name of the label, separated by a colon, as
- indicated by the ``is_prefixed`` attribute on the associated ``Group``
- instance.
+ Get the fully qualified name of the label.
+
+ Most label groups prefix the group name to the name
+ of the label, separated by a colon, as indicated by the
+ ``is_prefixed`` attribute on the associated ``Group`` instance.
:return: the fully qualified name of the label
"""
@@ -73,6 +78,8 @@ class Label:
@property
def emojified_description(self) -> str:
"""
+ Get the label description with emoji prefix.
+
TODO: Use this when GitHub supports Unicode in label descriptions
Get the description of the label prefixed with the emoji.
@@ -84,8 +91,9 @@ class Label:
@property
def api_arguments(self) -> dict[str, str]:
"""
- Get the dictionary of arguments to pass to the API for creating the
- label. The API only accepts ``name``, ``color`` and ``description``.
+ Get label creation API parameters.
+
+ The API only accepts ``name``, ``color`` and ``description``.
:return: the API arguments as a dictionary
"""
@@ -98,8 +106,7 @@ class Label:
def __eq__(self, remote: "Label") -> bool:
"""
- Compare this instance with the corresponding PyGithub instance to
- determine whether the two are equal.
+ Compare ``self`` with PyGithub label object.
:param remote: the PyGithub label instance to compare itself against
:return: whether the instance is equal to its remote counterpart
@@ -115,8 +122,7 @@ class Label:
def __ne__(self, remote: "Label") -> bool:
"""
- Compare this instance with the corresponding PyGithub instance to
- determine whether the two are unequal and would need to be reconciled.
+ Compare ``self`` with PyGithub label object.
:param remote: the PyGithub label instance to compare itself against
:return: whether the instance is unequal to its remote counterpart
diff --git a/automations/python/models/label_group.py b/automations/python/models/label_group.py
index aefa86e59..b59796383 100644
--- a/automations/python/models/label_group.py
+++ b/automations/python/models/label_group.py
@@ -1,6 +1,8 @@
class LabelGroup:
"""
- This model represents a group of labels. A group has some fixed parameters
+ Represents a group of labels.
+
+ A group has some fixed parameters:
- name, which may be prefixed to all child label names
- color, which acts as a fallback for child labels that do not specify one
- is_prefixed, which determines if group name is prefixed on child labels
diff --git a/automations/python/shared/data.py b/automations/python/shared/data.py
index c982389b3..844564ae5 100644
--- a/automations/python/shared/data.py
+++ b/automations/python/shared/data.py
@@ -14,8 +14,9 @@ ROOT_DIR = CURR_FILE.parent.parent.parent
@cache
def get_data(file_name: str) -> dict:
"""
- Access YAML files in the `data/` directory as Python objects. Calls to the
- function with the same file name will be cached for performance.
+ Access YAML files in the `data/` directory as Python objects.
+
+ Calls to the function with the same file name will be cached for performance.
:param file_name: the name of the YAML file to read
:return: the contents of the YAML file as a Python object
diff --git a/automations/python/shared/github.py b/automations/python/shared/github.py
index d7889cff9..d6e37f614 100644
--- a/automations/python/shared/github.py
+++ b/automations/python/shared/github.py
@@ -9,8 +9,10 @@ log = logging.getLogger(__name__)
def get_client(is_authenticated: bool = True) -> Github:
"""
- Get a PyGithub client to access the GitHub API. The client can optionally be
- authenticated using the GITHUB_ACCESS_TOKEN from the environment variables.
+ Get a PyGithub client to access the GitHub API.
+
+ The client can optionally be authenticated using the GITHUB_ACCESS_TOKEN
+ from the environment variables.
:param is_authenticated: whether to authenticate the client
:return: the PyGithub client
diff --git a/automations/python/shared/log.py b/automations/python/shared/log.py
index 2b5b50205..e2d17a058 100644
--- a/automations/python/shared/log.py
+++ b/automations/python/shared/log.py
@@ -4,7 +4,9 @@ import os
def configure_logger():
"""
- Configures the logging module to
+ Configure the logging module.
+
+ Applies the following configuration:
- change the log level names to lowercase
- set the formatter that works with GitHub logging commands
- set the default log level to LOGGING_LEVEL environment variable
diff --git a/automations/python/sync_labels.py b/automations/python/sync_labels.py
index 4bb4c54e9..af8789be1 100644
--- a/automations/python/sync_labels.py
+++ b/automations/python/sync_labels.py
@@ -35,8 +35,10 @@ def get_labels() -> list[Label]:
def set_labels(repo: Repository, labels: list[Label]):
"""
- Set the given list of labels on the given repository. Missing labels will
- be added, but extraneous labels will be left intact.
+ Set the given list of labels on the given repository.
+
+ Missing labels will be added, but extraneous labels will
+ be left intact.
:param repo: the repo in which to set the given labels
:param labels: the list of labels to define on the repo
diff --git a/dead_links/dead_link_tally.py b/dead_links/dead_link_tally.py
index 536f5b9f6..9136ee392 100644
--- a/dead_links/dead_link_tally.py
+++ b/dead_links/dead_link_tally.py
@@ -1,4 +1,6 @@
"""
+Retrives dead link tallies from Openverse API Redis.
+
This script assumes that the API Redis instance you care about is present
on localhost (usually via tunneling). It will run through the link validation
entries in Redis.
diff --git a/justfile b/justfile
index d58494501..de6e9bbe5 100644
--- a/justfile
+++ b/justfile
@@ -53,3 +53,8 @@ render-prettier:
# Render GitHub issue & PR templates
render-github:
just render templates/PULL_REQUEST_TEMPLATE.md.jinja .github/PULL_REQUEST_TEMPLATE.md
+# Render all templates (shortcut for easy iteration)
+render-templates:
+ just render-precommit
+ just render-prettier
+ just render-github
diff --git a/templates/.pre-commit-config.yaml.jinja b/templates/.pre-commit-config.yaml.jinja
index 1941ff88d..f4126a718 100644
--- a/templates/.pre-commit-config.yaml.jinja
+++ b/templates/.pre-commit-config.yaml.jinja
@@ -70,6 +70,20 @@ repos:
args:
- --safe
+ - repo: https://github.com/pycqa/pydocstyle
+ rev: 6.2.2 # 6.2.3 is slightly broken
+ hooks:
+ - id: pydocstyle
+ args:
+ - --convention=pep257
+ # Additional ignore reasons:
+ # D1xx: we do not want to force contributors to write redundant or useless docstrings
+ # D202: additional whitespace helps with readability
+ # D211: same as D202
+ # See the following documentation for what each rule does:
+ # https://www.pydocstyle.org/en/6.2.3/error_codes.html#error-codes
+ - --add-ignore=D1,D202,D211
+
{% endif %}
{% if contains_js_code | default(true) %}
- repo: https://github.com/pre-commit/mirrors-eslint
diff --git a/templates/README.md b/templates/README.md
new file mode 100644
index 000000000..254657cd2
--- /dev/null
+++ b/templates/README.md
@@ -0,0 +1,27 @@
+# Template workflow
+
+Contributing to the linting templates can be unintuitive at first. If you follow
+the process outlined below, it makes things a _lot_ easier to manage and
+iterate.
+
+## Process for editing jinja templates
+
+1. Make changes to the jinja template
+1. Run `just render-templates` to re-render all templates. This runs pretty
+ quickly but if you need to go even faster, you can run the
+ `render-precommit`, `render-prettier`, or `render-github` recipes instead to
+ render only the specific files you're changing.
+1. Confirm your rendered changes meet prettier's requirements by:
+ 1. Staging `.pre-commit-config.yaml` with `git add .pre-commit-config.yaml`
+ 1. Run `pre-commit run prettier`
+ 1. Run `git diff` to see any differences prettier may have made to the final
+ output
+ - This separate step is necessary because prettier does not lint the jinja
+ files. For the rendered output of the jinja files to pass prettier, the
+ templates themselves must be perfect, so you have to manually push changes
+ that prettier will make into the templates. Often this is stuff like
+ tedious whitespace, line-break, and indentation issues in the YAML or the
+ Markdown templates.
+1. If you had to make changes to appease prettier, follow steps 2 and 3 again
+ and confirm there are no more changes from prettier.
+1. You're done!