Skip to content

Commit

Permalink
Migrate to Hatch build and mypy type checker
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin committed May 29, 2023
1 parent 6f1140d commit 74cbe54
Show file tree
Hide file tree
Showing 24 changed files with 305 additions and 192 deletions.
49 changes: 36 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ defaults:
run:
shell: bash
jobs:
build:
test:
strategy:
fail-fast: false
matrix:
include:
- python: '^3.9'
- python: '^3.10'
os: macos-latest
- python: 3.8
- python: '3.10'
os: windows-latest
- python: 3.9
os: ubuntu-latest
- python: 3.8
os: macos-latest
- python: 3.7
os: windows-latest
- python: 3.7
Expand All @@ -33,19 +37,38 @@ jobs:
- name: Pin to lowest versions
if: matrix.versions == 'minimal'
run: |
sed -i -E 's/"(\^|>=)([0-9])/"==\2/' pyproject.toml
- name: Setup virtualenv
uses: syphar/restore-virtualenv@d0a933d92488e0505e012c3367e3f987a6276f5a
sed -i -E 's/#min //; s/\b >=([0-9])/ ==\1/' pyproject.toml
- name: Install Hatch
run: |
pip install hatch
- name: Install dependencies
run: |
hatch run test:pip freeze
- name: Run tests
run: |
hatch run test:test
style:
runs-on: ubuntu-latest
steps:
- name: Download source
uses: actions/checkout@v3
- name: Install Python
uses: actions/setup-python@v4
with:
requirement_files: pyproject.toml
- name: Install packages
python-version: '3.10'
- name: Install Hatch
run: |
python -m pip install -U pip'>=19'; pip install -U wheel
pip install -U --upgrade-strategy=eager . $(awk '/^$/ {p = 0} ! /${{runner.os}}/ { if (p) {print $1} } /dev-dependencies/ {p = 1}' pyproject.toml)
- name: Test
pip install hatch
- name: Install dependencies
run: |
.tools/ci.sh with_groups
hatch run style:pip freeze
- name: Check formatting
if: matrix.versions == null
run: |
hatch run style:format
git diff --exit-code
- name: Install dependencies
run: |
hatch run types:pip freeze
- name: Check types
run: |
hatch run types:check
4 changes: 2 additions & 2 deletions .github/workflows/deploy-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ jobs:
with:
python-version: '3.x'
- name: Install dependencies
run: pip install . -r docs/requirements.txt
run: pip install --no-deps -r docs/requirements.txt
- name: Build site
run: .tools/build_docs.sh
run: mkdocs build
- name: Deploy to gh-pages
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
uses: oprypin/push-to-gh-pages@v3
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/dist/
site/
.pytype/
site*/
.mypy_cache/
.pytest_cache/
__pycache__/
*.egg-info/
Expand Down
16 changes: 0 additions & 16 deletions .tools/build_docs.sh

This file was deleted.

16 changes: 0 additions & 16 deletions .tools/ci.sh

This file was deleted.

13 changes: 0 additions & 13 deletions .tools/copier-answers.yml

This file was deleted.

4 changes: 4 additions & 0 deletions .tools/docs-dependencies.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
set -e

hatch env show --json | jq -r ".docs.dependencies | .[]" | pip-compile -U - -o docs/requirements.txt
2 changes: 0 additions & 2 deletions .tools/hooks/pre-commit

This file was deleted.

12 changes: 6 additions & 6 deletions .tools/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ cd "$(dirname "$0")/.."

git diff --staged --quiet
git diff --quiet HEAD pyproject.toml
poetry version "$1"
poetry install
poetry build
git add pyproject.toml
rm -rf dist
hatch version "$1"
hatch build
git add mkdocstrings_handlers/crystal/__init__.py
git commit -m "v$1"
git tag -a -m "" "v$1"
poetry publish
echo git push origin master --tags
hatch publish
git push origin master --tags
7 changes: 0 additions & 7 deletions docs/requirements.in

This file was deleted.

65 changes: 38 additions & 27 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@
# This file is autogenerated by pip-compile with python 3.10
# To update, run:
#
# pip-compile docs/requirements.in
# pip-compile --output-file=docs/requirements.txt -
#
certifi==2023.5.7
# via requests
charset-normalizer==3.1.0
# via requests
click==8.1.3
# via mkdocs
colorama==0.4.6
# via
# griffe
# mkdocs-material
ghp-import==2.1.0
# via mkdocs
griffe==0.20.0
griffe==0.29.0
# via mkdocstrings-python
importlib-metadata==4.11.4
# via mkdocs
idna==3.4
# via requests
jinja2==3.1.2
# via
# mkdocs
Expand All @@ -25,57 +33,60 @@ markdown==3.3.7
# mkdocs-material
# mkdocstrings
# pymdown-extensions
markdown-callouts==0.2.0
# via -r docs/requirements.in
markupsafe==2.1.1
markdown-callouts==0.3.0
# via -r -
markupsafe==2.1.2
# via
# jinja2
# mkdocstrings
mergedeep==1.3.4
# via mkdocs
mkdocs==1.3.0
mkdocs==1.4.3
# via
# -r docs/requirements.in
# -r -
# mkdocs-autorefs
# mkdocs-material
# mkdocs-section-index
# mkdocstrings
mkdocs-autorefs==0.4.1
# via mkdocstrings
mkdocs-material==8.3.6
# via -r docs/requirements.in
mkdocs-material-extensions==1.0.3
mkdocs-material==9.1.14
# via -r -
mkdocs-material-extensions==1.1.1
# via mkdocs-material
mkdocs-section-index==0.3.4
# via -r docs/requirements.in
mkdocstrings==0.19.0
mkdocs-section-index==0.3.5
# via -r -
mkdocstrings==0.22.0
# via
# -r docs/requirements.in
# -r -
# mkdocstrings-python
mkdocstrings-python==0.7.1
# via -r docs/requirements.in
packaging==21.3
mkdocstrings-python==1.1.0
# via -r -
packaging==23.1
# via mkdocs
pygments==2.12.0
pygments==2.15.1
# via mkdocs-material
pymdown-extensions==9.5
pymdown-extensions==10.0.1
# via
# -r docs/requirements.in
# -r -
# mkdocs-material
# mkdocstrings
pyparsing==3.0.9
# via packaging
python-dateutil==2.8.2
# via ghp-import
pyyaml==6.0
# via
# mkdocs
# pymdown-extensions
# pyyaml-env-tag
pyyaml-env-tag==0.1
# via mkdocs
regex==2023.5.5
# via mkdocs-material
requests==2.31.0
# via mkdocs-material
six==1.16.0
# via python-dateutil
watchdog==2.1.9
urllib3==2.0.2
# via requests
watchdog==3.0.0
# via mkdocs
zipp==3.8.0
# via importlib-metadata
6 changes: 4 additions & 2 deletions mkdocstrings_handlers/crystal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
from .collector import CrystalCollector
from .renderer import CrystalRenderer

__version__ = "0.3.5"


class CrystalHandler(CrystalCollector, CrystalRenderer, BaseHandler):
load_inventory = staticmethod(inventory.list_object_urls)
load_inventory = staticmethod(inventory.list_object_urls) # type: ignore[assignment]

def __init__(
self,
Expand All @@ -20,7 +22,7 @@ def __init__(
source_locations: Mapping[str, str] = {},
**config: Any,
) -> None:
BaseHandler.__init__(self, "crystal", theme, custom_templates)
BaseHandler.__init__(self, "crystal", theme, custom_templates) # type: ignore
CrystalCollector.__init__(
self, crystal_docs_flags=crystal_docs_flags, source_locations=source_locations
)
Expand Down
30 changes: 18 additions & 12 deletions mkdocstrings_handlers/crystal/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,30 @@
import re
import shlex
import subprocess
from typing import Any, Callable, Iterable, Iterator, Mapping, Sequence, TypeVar, cast
import sys
from typing import Any, BinaryIO, Callable, Iterable, Iterator, Mapping, Sequence, TypeVar, cast

import mkdocs.exceptions
from cached_property import cached_property
from mkdocstrings.handlers.base import BaseCollector, CollectionError
if sys.version_info >= (3, 8):
from functools import cached_property
else:
from cached_property import cached_property

from mkdocstrings.handlers.base import BaseHandler, CollectionError

from . import inventory
from .items import DocConstant, DocItem, DocLocation, DocMapping, DocMethod, DocModule, DocType

try:
from mkdocs.exceptions import PluginError
except ImportError:
PluginError = SystemExit
PluginError = SystemExit # type: ignore

log = logging.getLogger(f"mkdocs.plugins.{__name__}")

D = TypeVar("D", bound=DocItem)


class CrystalCollector(BaseCollector):
class CrystalCollector(BaseHandler):
def __init__(
self, crystal_docs_flags: Sequence[str] = (), source_locations: Mapping[str, str] = {}
):
Expand Down Expand Up @@ -68,13 +72,14 @@ def root(self) -> DocRoot:
"""The top-level namespace, represented as a fake module."""
try:
with self._proc:
root = inventory.read(self._proc.stdout)
root.__class__ = DocRoot
module = inventory.read(cast(BinaryIO, self._proc.stdout))
module.__class__ = DocRoot
root = cast(DocRoot, module)
root.source_locations = self._source_locations
return root
finally:
if self._proc.returncode:
cmd = " ".join(shlex.quote(arg) for arg in self._proc.args)
cmd = " ".join(shlex.quote(arg) for arg in cast(Sequence[str], self._proc.args))
raise PluginError(f"Command `{cmd}` exited with status {self._proc.returncode}")

# pytype: enable=bad-return-type
Expand All @@ -91,7 +96,7 @@ def collect(self, identifier: str, config: Mapping[str, Any]) -> DocView:
**config,
}

item = self.root
item: DocItem = self.root
if identifier != "::":
item = item.lookup(identifier)
return DocView(item, config)
Expand Down Expand Up @@ -204,9 +209,10 @@ def walk_types(self) -> Iterator[DocType]:
@classmethod
def _get_locations(cls, obj: DocItem) -> Sequence[str]:
if isinstance(obj, DocConstant):
obj = obj.parent
if not obj:
parent = obj.parent
if not parent:
return ()
obj = parent
if isinstance(obj, DocType):
return [loc.filename for loc in obj.locations]
elif isinstance(obj, DocMethod):
Expand Down
2 changes: 1 addition & 1 deletion mkdocstrings_handlers/crystal/crystal_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def linkify_highlighted_html(


class _CrystalHTMLHandler(html.parser.HTMLParser):
def __init__(self):
def __init__(self) -> None:
super().__init__()
self.text = io.StringIO()
self.tokens: list[LinkToken] = []
Expand Down
Loading

0 comments on commit 74cbe54

Please sign in to comment.