Skip to content

Commit

Permalink
Add spelling and docstring enforcement (#1929)
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 authored Dec 23, 2022
1 parent d89871b commit 201cd58
Show file tree
Hide file tree
Showing 43 changed files with 147 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install texlive-plain-generic inkscape texlive-xetex latexmk
sudo apt-get install texlive-plain-generic inkscape texlive-xetex latexmk enchant-2
# pandoc is not up to date in the ubuntu repos, so we install directly
wget https://github.com/jgm/pandoc/releases/download/2.14.2/pandoc-2.14.2-1-amd64.deb && sudo dpkg -i pandoc-2.14.2-1-amd64.deb
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ jobs:
- name: Run Linters
run: |
hatch run lint:style
pipx run 'validate-pyproject[all]' pyproject.toml
pipx run interrogate -v .
pipx run doc8 --max-line-length=200 --ignore-path=docs/source/other/full-config.rst
check_release:
Expand Down
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: end-of-file-fixer
- id: check-case-conflict
- id: check-ast
- id: check-docstring-first
- id: check-executables-have-shebangs
- id: requirements-txt-fixer
- id: check-added-large-files
- id: check-case-conflict
- id: check-merge-conflict
- id: check-json
- id: check-toml
- id: check-yaml
- id: debug-statements
- id: forbid-new-submodules
- id: check-builtin-literals
- id: end-of-file-fixer
- id: trailing-whitespace

- repo: https://github.com/python-jsonschema/check-jsonschema
Expand All @@ -36,7 +36,7 @@ repos:
- id: black

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.181
rev: v0.0.189
hooks:
- id: ruff
args: ["--fix"]
2 changes: 1 addition & 1 deletion check_requirements.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Verify that the "all" reqs are in sync.
"""Verify that the "all" reqs are in sync."""
import sys

from tomli import load
Expand Down
7 changes: 7 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
"IPython.sphinxext.ipython_console_highlighting",
]

try:
import enchant # type:ignore # noqa

extensions += ["sphinxcontrib.spelling"]
except ImportError:
pass

myst_enable_extensions = ["html_image"]

# Add any paths that contain templates here, relative to this directory.
Expand Down
3 changes: 3 additions & 0 deletions hatch_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ def _get_css_files():


class CustomHook(BuildHookInterface):
"""A custom build hook for nbconvert."""

def initialize(self, version, build_data):
"""Initialize the hook."""
if self.target_name not in ["wheel", "sdist"]:
return
_get_css_files()
1 change: 1 addition & 0 deletions nbconvert/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""nbconvert cli entry point."""
from .nbconvertapp import main

main()
1 change: 1 addition & 0 deletions nbconvert/_version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""nbconvert version info."""
import re
from typing import List

Expand Down
1 change: 1 addition & 0 deletions nbconvert/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""pytest configuration."""
import asyncio
import os

Expand Down
4 changes: 4 additions & 0 deletions nbconvert/exporters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@


class ExporterNameError(NameError):
"""An exporter name error."""

pass


class ExporterDisabledError(ValueError):
"""An exporter disabled error."""

pass


Expand Down
4 changes: 4 additions & 0 deletions nbconvert/exporters/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@


class ResourcesDict(collections.defaultdict):
"""A default dict for resources."""

def __missing__(self, key):
"""Handle missing value."""
return ""


Expand All @@ -33,6 +36,7 @@ class FilenameExtension(Unicode):
info_text = "a filename extension, beginning with a dot"

def validate(self, obj, value):
"""Validate the file name."""
# cast to proper unicode
value = super().validate(obj, value)

Expand Down
4 changes: 4 additions & 0 deletions nbconvert/exporters/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,12 @@ def markdown2html(self, context, source):
return MarkdownWithMath(renderer=renderer).render(source)

def default_filters(self):
"""Get the default filters."""
yield from super().default_filters()
yield ("markdown2html", self.markdown2html)

def from_notebook_node(self, nb, resources=None, **kw):
"""Convert from notebook node."""
langinfo = nb.metadata.get("language_info", {})
lexer = langinfo.get("pygments_lexer", langinfo.get("name", None))
highlight_code = self.filters.get(
Expand Down Expand Up @@ -255,11 +257,13 @@ def resources_include_lab_theme(name):
return markupsafe.Markup(code)

def resources_include_js(name):
"""Get the resources include JS for a name."""
env = self.environment
code = """<script>\n%s</script>""" % (env.loader.get_source(env, name)[0])
return markupsafe.Markup(code)

def resources_include_url(name):
"""Get the resources include url for a name."""
env = self.environment
mime_type, encoding = mimetypes.guess_type(name)
try:
Expand Down
2 changes: 2 additions & 0 deletions nbconvert/exporters/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def _template_name_default(self):
output_mimetype = "text/latex"

def default_filters(self):
"""Get the default filters."""
yield from super().default_filters()
yield ("resolve_references", resolve_references)

Expand Down Expand Up @@ -64,6 +65,7 @@ def default_config(self):
return c

def from_notebook_node(self, nb, resources=None, **kw):
"""Convert from notebook node."""
langinfo = nb.metadata.get("language_info", {})
lexer = langinfo.get("pygments_lexer", langinfo.get("name", None))
highlight_code = self.filters.get(
Expand Down
1 change: 1 addition & 0 deletions nbconvert/exporters/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def _file_extension_default(self):
export_from_notebook = "Notebook"

def from_notebook_node(self, nb, resources=None, **kw):
"""Convert from notebook node."""
nb_copy, resources = super().from_notebook_node(nb, resources, **kw)
if self.nbformat_version != nb_copy.nbformat:
resources["output_suffix"] = ".v%i" % self.nbformat_version
Expand Down
4 changes: 4 additions & 0 deletions nbconvert/exporters/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ class LatexFailed(IOError): # noqa
"""

def __init__(self, output):
"""Initialize the error."""
self.output = output

def __unicode__(self):
"""Unicode representation."""
return "PDF creating failed, captured latex output:\n%s" % self.output

def __str__(self):
"""String representation."""
u = self.__unicode__()
return u

Expand Down Expand Up @@ -178,6 +181,7 @@ def log_error(command, out):
return self.run_command(self.bib_command, filename, 1, log_error, raise_on_failure)

def from_notebook_node(self, nb, resources=None, **kw):
"""Convert from notebook node."""
latex, resources = super().from_notebook_node(nb, resources=resources, **kw)
# set texinputs directory, so that local files will be found
if resources and resources.get("metadata", {}).get("path"):
Expand Down
3 changes: 3 additions & 0 deletions nbconvert/exporters/qt_exporter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""A qt exporter."""
import os
import sys
import tempfile
Expand All @@ -8,6 +9,7 @@


class QtExporter(HTMLExporter):
"""A qt exporter."""

paginate = None

Expand Down Expand Up @@ -45,6 +47,7 @@ def _run_pyqtwebengine(self, html):
return s.data

def from_notebook_node(self, nb, resources=None, **kw):
"""Convert from notebook node."""
self._check_launch_reqs()
html, resources = super().from_notebook_node(nb, resources=resources, **kw)

Expand Down
10 changes: 10 additions & 0 deletions nbconvert/exporters/qt_screenshot.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""A qt screenshot exporter."""
import os

try:
Expand All @@ -17,11 +18,15 @@
APP = QApplication([])

class QtScreenshot(QWebEngineView):
"""A qt screenshot exporter."""

def __init__(self):
"""Initialize the exporter."""
super().__init__()
self.app = APP

def capture(self, url, output_file, paginate):
"""Capture the screenshot."""
self.output_file = output_file
self.paginate = paginate
self.load(QtCore.QUrl(url))
Expand All @@ -34,6 +39,7 @@ def capture(self, url, output_file, paginate):
self.export = self.export_pdf

def cleanup(*args):
"""Cleanup the app."""
self.app.quit()
self.get_data()

Expand All @@ -46,12 +52,14 @@ def cleanup(*args):
self.app.exec()

def on_loaded(self):
"""Handle app load."""
self.size = self.page().contentsSize().toSize()
self.resize(self.size)
# Wait for resize
QtCore.QTimer.singleShot(1000, self.export)

def export_pdf(self):
"""Export to pdf."""
if self.paginate:
page_size = QPageSize(QPageSize.A4)
page_layout = QPageLayout(page_size, QPageLayout.Portrait, QtCore.QMarginsF())
Expand All @@ -66,11 +74,13 @@ def export_pdf(self):
self.page().printToPdf(self.output_file, pageLayout=page_layout)

def export_png(self):
"""Export to png."""
self.grab().save(self.output_file, "PNG")
self.app.quit()
self.get_data()

def get_data(self):
"""Get output data."""
if os.path.exists(self.output_file):
with open(self.output_file, "rb") as f:
self.data = f.read()
Expand Down
3 changes: 3 additions & 0 deletions nbconvert/exporters/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@


class ScriptExporter(TemplateExporter):
"""A script exporter."""

# Caches of already looked-up and instantiated exporters for delegation:
_exporters = Dict()
_lang_exporters = Dict()
Expand Down Expand Up @@ -46,6 +48,7 @@ def _get_language_exporter(self, lang_name):
return self._lang_exporters[lang_name]

def from_notebook_node(self, nb, resources=None, **kw):
"""Convert from notebook node."""
langinfo = nb.metadata.get("language_info", {})

# delegate to custom exporter, if specified
Expand Down
8 changes: 7 additions & 1 deletion nbconvert/exporters/templateexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def recursive_update(target, new):

# define function at the top level to avoid pickle errors
def deprecated(msg):
"""Emit a deprecation warning."""
warnings.warn(msg, DeprecationWarning)


Expand All @@ -114,10 +115,12 @@ class ExtensionTolerantLoader(BaseLoader):
"""

def __init__(self, loader, extension):
"""Initialize the loader."""
self.loader = loader
self.extension = extension

def get_source(self, environment, template):
"""Get the source for a template."""
try:
return self.loader.get_source(environment, template)
except TemplateNotFound:
Expand All @@ -126,6 +129,7 @@ def get_source(self, environment, template):
return self.loader.get_source(environment, template + self.extension)

def list_templates(self):
"""List available templates."""
return self.loader.list_templates()


Expand Down Expand Up @@ -567,14 +571,15 @@ def _template_paths(self, prune=True, root_dirs=None):

@classmethod
def get_compatibility_base_template_conf(cls, name):
"""Get the base template config."""
# Hard-coded base template confs to use for backwards compatibility for 5.x-only templates
if name == "display_priority":
return {"base_template": "base"}
if name == "full":
return {"base_template": "classic", "mimetypes": {"text/html": True}}

def get_template_names(self):
# finds a list of template names where each successive template name is the base template
"""Finds a list of template names where each successive template name is the base template"""
template_names = []
root_dirs = self.get_prefix_root_dirs()
base_template = self.template_name
Expand Down Expand Up @@ -635,6 +640,7 @@ def get_template_names(self):
return template_names

def get_prefix_root_dirs(self):
"""Get the prefix root dirs."""
# We look at the usual jupyter locations, and for development purposes also
# relative to the package directory (first entry, meaning with highest precedence)
root_dirs = []
Expand Down
3 changes: 3 additions & 0 deletions nbconvert/exporters/webpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def run_pyppeteer(self, html):
"""Run pyppeteer."""

async def main(temp_file):
"""Run main pyppeteer script."""
args = ["--no-sandbox"] if self.disable_sandbox else []
browser = await self._check_launch_reqs()(
handleSIGINT=False, handleSIGTERM=False, handleSIGHUP=False, args=args
Expand Down Expand Up @@ -139,6 +140,7 @@ async def main(temp_file):
# TODO: when dropping Python 3.6, use
# pdf_data = pool.submit(asyncio.run, main(temp_file)).result()
def run_coroutine(coro):
"""Run an internal coroutine."""
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
return loop.run_until_complete(coro)
Expand All @@ -150,6 +152,7 @@ def run_coroutine(coro):
return pdf_data

def from_notebook_node(self, nb, resources=None, **kw):
"""Convert from a notebook node."""
self._check_launch_reqs()
html, resources = super().from_notebook_node(nb, resources=resources, **kw)

Expand Down
Loading

0 comments on commit 201cd58

Please sign in to comment.