Skip to content

Commit

Permalink
Merge pull request #2 from astrojuanlu/fix-ci
Browse files Browse the repository at this point in the history
Fix CI
  • Loading branch information
astrojuanlu authored Jul 1, 2024
2 parents 15cbd85 + 58fb6e9 commit c412c37
Show file tree
Hide file tree
Showing 18 changed files with 132 additions and 90 deletions.
10 changes: 10 additions & 0 deletions .copier-answers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Changes here will be overwritten by Copier; NEVER EDIT MANUALLY
_commit: v2024.03.1
_src_path: gh:astrojuanlu/copier-pylib
author_email: juan_luis_cano@mckinsey.com
author_name: Juan Luis Cano Rodríguez
github_org: astrojuanlu
package_name: kedro_init
project_name: kedro-init
short_description: A simple CLI command that initialises a Kedro project from an existing
Python package
13 changes: 5 additions & 8 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ name: Publish library

on:
push:
branches:
- main
tags:
# Don't try to be smart about PEP 440 compliance,
# see https://www.python.org/dev/peps/pep-0440/#appendix-b-parsing-version-strings-with-regular-expressions
Expand All @@ -12,6 +10,10 @@ on:
jobs:
publish:
runs-on: ubuntu-latest
environment: release
permissions:
id-token: write
contents: write
steps:
- uses: actions/checkout@v2
- name: Set up Python
Expand All @@ -23,11 +25,6 @@ jobs:
- name: Build package
run: python -m build
- name: Publish to PyPI
uses: pypa/gh-action-pypi-publish@release/v1.4
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
uses: pypa/gh-action-pypi-publish@release/v1
- name: Create GitHub release
uses: softprops/action-gh-release@v1
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10"]
python-version: ["3.9", "3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
Expand All @@ -15,7 +15,7 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox tox-gh-actions
python -m pip install uv
uv pip install --system tox tox-uv tox-gh-actions
- name: Test with tox
run: tox
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repos:
- id: check-merge-conflict

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.6
rev: v0.3.2
hooks:
- id: ruff
args: [ --fix, --exit-non-zero-on-fix ]
Expand Down
2 changes: 1 addition & 1 deletion .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
version: 2

build:
os: ubuntu-20.04
os: ubuntu-22.04
tools:
python: "3.9"

Expand Down
19 changes: 10 additions & 9 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ make sure that you **read our code of conduct** (`CODE_OF_CONDUCT.md`).

## Contributing code

1. Set up a Python development environment
1. Clone the repository
2. Set up and activate a Python development environment
(advice: use [venv](https://docs.python.org/3/library/venv.html),
[virtualenv](https://virtualenv.pypa.io/), or [miniconda](https://docs.conda.io/en/latest/miniconda.html))
2. Install tox: `python -m pip install tox`
3. Clone the repository
4. Start a new branch off master: `git switch -c new-branch master`
5. Make your code changes
6. Check that your code follows the style guidelines of the project: `tox -e reformat && tox -e check`
7. (optional) Build the documentation: `tox -e docs`
8. (optional) Run the tests: `tox -e py39`
3. Install tox: `python -m pip install tox`
4. Make sure the tests run: `tox -e py39`
(change the version number according to the Python you are using)
9. Commit, push, and open a pull request!
5. Start a new branch: `git switch -c new-branch main`
6. Make your code changes
7. Check that your code follows the style guidelines of the project: `tox -e reformat && tox -e check`
8. Run the tests again and verify that they pass: `tox -e py39`
9. (optional) Build the documentation: `tox -e docs`
10. Commit, push, and open a pull request!
20 changes: 0 additions & 20 deletions docs/Makefile

This file was deleted.

2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

project = _metadata["Name"]
author = _metadata["Author-email"].split("<", 1)[0].strip()
copyright = f"2022, {author}"
copyright = f"2023, {author}"

version = _metadata["Version"]
release = ".".join(version.split(".")[:2])
Expand Down
17 changes: 10 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ classifiers = [
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]
dynamic = ["version"]

Expand Down Expand Up @@ -56,8 +57,13 @@ doc = [
"sphinx-copybutton",
]

[tool.pdm.version]
source = "scm"

[tool.ruff]
show-fixes = true

[tool.ruff.lint]
select = [
"F", # Pyflakes
"E", # Pycodestyle
Expand All @@ -69,17 +75,14 @@ select = [
]
ignore = ["D100", "D103"]

[tool.pdm.version]
source = "scm"
[tool.ruff.lint.pydocstyle]
convention = "pep257"

[tool.ruff.per-file-ignores]
[tool.ruff.lint.per-file-ignores]
"tests/**/*" = ["D", "PLR2004"]

[tool.ruff.pydocstyle]
convention = "pep257"

[tool.mypy]
python_version = "3.7"
python_version = "3.9"
warn_redundant_casts = true
warn_unused_configs = true
pretty = true
Expand Down
46 changes: 27 additions & 19 deletions src/kedro_init/build_config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import tempfile
import typing as t
import zipfile
Expand All @@ -9,14 +11,17 @@
try:
from importlib.metadata import PackageNotFoundError, version
except ModuleNotFoundError:
from importlib_metadata import PackageNotFoundError, version
from importlib_metadata import PackageNotFoundError, version # type: ignore

import tomlkit
from pygetimportables import _simple_build_wheel, get_top_importables_from_wheel
from validate_pyproject import api, errors, plugins
from validate_pyproject.types import Schema


def _get_importables_and_project_name(project_root, outdir):
def _get_importables_and_project_name(
project_root: str | Path, outdir: str | Path
) -> tuple[set[str], str]:
wheel_path = _simple_build_wheel(project_root, outdir)

with zipfile.ZipFile(wheel_path, "r") as zf:
Expand All @@ -28,20 +33,22 @@ def _get_importables_and_project_name(project_root, outdir):
return package_names, project_name


def kedro_pyproject(tool_name: str) -> dict:
return {
"$id": "https://docs.kedro.org/en/latest/",
"type": "object",
"description": "Kedro project metadata",
"properties": {
"package_name": {"type": "string"},
"project_name": {"type": "string", "format": "pep508-identifier"},
"kedro_init_version": {"type": "string", "format": "pep440"},
"source_dir": {"type": "string"},
},
"required": ["package_name", "project_name", "kedro_init_version"],
"additionalProperties": False,
}
def kedro_pyproject(tool_name: str) -> Schema:
return Schema(
{
"$id": "https://docs.kedro.org/en/latest/",
"type": "object",
"description": "Kedro project metadata",
"properties": {
"package_name": {"type": "string"},
"project_name": {"type": "string", "format": "pep508-identifier"},
"kedro_init_version": {"type": "string", "format": "pep440"},
"source_dir": {"type": "string"},
},
"required": ["package_name", "project_name", "kedro_init_version"],
"additionalProperties": False,
}
)


def get_or_create_build_config(project_root: Path) -> tuple[bool, t.Any]:
Expand Down Expand Up @@ -77,10 +84,11 @@ def get_or_create_build_config(project_root: Path) -> tuple[bool, t.Any]:
if (project_root / package_name).is_dir():
kedro_config["source_dir"] = ""
else:
# FIXME: What happens if package_dir is None? (See type: ignore below)
package_dir = next(project_root.glob(f"*/{package_name}"), None)
source_dir = package_dir.parent.name if package_dir is not None else None
if package_dir is not None and source_dir != "src":
kedro_config["source_dir"] = source_dir
kedro_config["source_dir"] = source_dir # type: ignore
return False, kedro_config

# Kedro build config might be present, return it if valid
Expand All @@ -89,10 +97,10 @@ def get_or_create_build_config(project_root: Path) -> tuple[bool, t.Any]:
except errors.ValidationError as exc:
raise ValueError("Kedro build configuration is invalid") from exc
else:
return True, pyproject_toml["tool"]["kedro"]
return True, pyproject_toml["tool"]["kedro"] # type: ignore


def init_build_config(project_root: Path, *, build_config: dict[str, str]):
def init_build_config(project_root: Path, *, build_config: dict[str, str]) -> None:
with (project_root / "pyproject.toml").open("r") as fh:
pyproject_toml = tomlkit.load(fh)

Expand Down
2 changes: 1 addition & 1 deletion src/kedro_init/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

@click.command()
@click.argument("project_root", type=click.Path(exists=False))
def cli(project_root: str):
def cli(project_root: str) -> None:
project_root_path = Path(project_root)
if rich_available:
console = Console()
Expand Down
8 changes: 6 additions & 2 deletions src/kedro_init/config_dirs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from pathlib import Path

CONFIG_DIRS = [
Expand All @@ -6,7 +8,9 @@
]


def get_or_create_config_dirs(project_root: Path, *, expected_config_dirs=None):
def get_or_create_config_dirs(
project_root: Path, *, expected_config_dirs: list[Path] | None = None
) -> dict[Path, tuple[bool, Path]]:
if not expected_config_dirs:
expected_config_dirs = CONFIG_DIRS

Expand All @@ -21,5 +25,5 @@ def get_or_create_config_dirs(project_root: Path, *, expected_config_dirs=None):
return config_dirs


def init_config_dir(project_root: Path, *, target_config_dir: Path):
def init_config_dir(project_root: Path, *, target_config_dir: Path) -> None:
target_config_dir.mkdir(parents=True, exist_ok=True)
5 changes: 3 additions & 2 deletions src/kedro_init/init.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import typing as t
from pathlib import Path

from .build_config import get_or_create_build_config, init_build_config
from .config_dirs import get_or_create_config_dirs, init_config_dir
from .modules import get_or_create_modules, init_module


def init_steps(project_root: Path):
def init_steps(project_root: Path) -> t.Generator[str, None, None]:
yield "Looking for existing package directories"

existing, build_config = get_or_create_build_config(project_root)
Expand All @@ -31,6 +32,6 @@ def init_steps(project_root: Path):
)


def init(project_root: Path):
def init(project_root: Path) -> None:
for _ in init_steps(project_root):
pass
25 changes: 17 additions & 8 deletions src/kedro_init/modules.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from pathlib import Path

TEMPLATES_PATH = Path(__file__).parent / "templates"
Expand All @@ -11,29 +13,36 @@ def get_or_create_modules(
project_root: Path,
*,
build_config: dict[str, str],
module_templates: dict[str, str] = None,
):
if not module_templates:
module_templates = MODULE_TEMPLATES
module_templates: dict[str, Path] | None = None,
) -> dict[str, tuple[bool, Path, Path]]:
if module_templates:
module_templates_l = module_templates
else:
module_templates_l = MODULE_TEMPLATES

package_name = build_config["package_name"]
package_dir = project_root / package_name
if not package_dir.is_dir():
package_dir = next(project_root.glob(f"*/{package_name}"), None)
package_dir = next(project_root.glob(f"*/{package_name}"), None) # type: ignore

if package_dir is None:
raise ValueError(
f"No suitable directory found for package name '{package_name}'"
)

modules = {}
for module_name in module_templates:
for module_name in module_templates_l:
target_module = package_dir / module_name
if target_module.exists():
modules[module_name] = True, target_module, target_module
else:
modules[module_name] = False, target_module, module_templates[module_name]
modules[module_name] = False, target_module, module_templates_l[module_name]

return modules


def init_module(
project_root: Path, *, target_module_path: Path, module_contents_path: Path
):
) -> None:
with target_module_path.open("w") as fh:
fh.write(module_contents_path.read_text())
3 changes: 2 additions & 1 deletion src/kedro_init/templates/pipeline_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ def register_pipelines() -> dict[str, Pipeline]:
A mapping from pipeline names to ``Pipeline`` objects.
"""
pipelines = find_pipelines()
pipelines["__default__"] = sum(pipelines.values())
# https://github.com/kedro-org/kedro/issues/2526
pipelines["__default__"] = sum(pipelines.values(), start=Pipeline([]))
return pipelines
1 change: 0 additions & 1 deletion tests/test_import.py

This file was deleted.

Loading

0 comments on commit c412c37

Please sign in to comment.