Skip to content

Commit

Permalink
update to utilize rye
Browse files Browse the repository at this point in the history
  • Loading branch information
safurrier committed Apr 14, 2024
1 parent 1ad7fe8 commit d85325b
Show file tree
Hide file tree
Showing 14 changed files with 337 additions and 135 deletions.
39 changes: 27 additions & 12 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
# .github/workflows/tests.yml
name: Tests
on: push
name: Code Quality Checks

on:
push:
pull_request:

jobs:
tests:
precommit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v1
with:
python-version: 3.8
architecture: x64
- run: make setup
- run: make check
- run: bash <(curl -s https://codecov.io/bash)
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Rye
uses: eifinger/setup-rye@v2

- name: Sync Rye
run: rye sync

- name: Run MyPy
run: rye run mypy src

- name: Run Linter
run: rye lint src

- name: Run Formatter
run: rye format src

- name: Run Tests
run: rye run py.test tests --cov=src --cov-report=term-missing
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ index.rst
setup_mac.sh
docker/.env
.vs_code/*
.mypy_cache/*
.pytest_cache/*
.ruff_cache/*
Empty file added .pre-commit-config.yaml
Empty file.
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.8.2
3.12.2
38 changes: 22 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
install-rye:
curl -sSf https://rye-up.com/get | bash

setup:
python -m venv .venv && . .venv/bin/activate
pip install --upgrade pip
pip install -r requirements.dev
pip install -r requirements.prod
rye sync

clean-pyc:
find . -name '*.pyc' -exec rm -f {} +
Expand All @@ -16,21 +16,27 @@ clean-test:

clean: clean-pyc clean-test

test: clean
. .venv/bin/activate && py.test tests --cov=src --cov-report=term-missing --cov-fail-under 95
test: clean setup
rye run py.test tests --cov=src --cov-report=term-missing

mypy:
. .venv/bin/activate && mypy src
rye run mypy src

lint:
. .venv/bin/activate && pylint src -j 4 --reports=y
rye lint src

format:
rye format src

# TODO: Sphinx not working with Rye yet
# docs: FORCE
# cd docs; rye run sphinx-apidoc -o ./source ./src
# cd docs; rye run sphinx-build -b html ./source ./build

docs: FORCE
cd docs; . .venv/bin/activate && sphinx-apidoc -o ./source ./src
cd docs; . .venv/bin/activate && sphinx-build -b html ./source ./build
FORCE:

check: test lint mypy
check: setup test mypy format lint


# Docker
########
Expand All @@ -42,8 +48,8 @@ dev-env: refresh-containers
@docker compose -f docker/docker-compose.yml down
@docker compose -f docker/docker-compose.yml up -d dev
@docker exec -ti composed_dev /bin/bash
refresh-containers:

refresh-containers:
@echo "Rebuilding containers..."
@docker compose -f docker/docker-compose.yml build

Expand All @@ -54,9 +60,9 @@ rebuild-images:
build-image:
@echo Building dev image and tagging as ${IMAGE_NAME}:${IMAGE_TAG}
@docker compose -f docker/docker-compose.yml down
@docker compose -f docker/docker-compose.yml up -d dev
@docker compose -f docker/docker-compose.yml up -d dev
@docker tag dev ${IMAGE_NAME}:${IMAGE_TAG}

push-image: build-image
@echo Pushing image to container registry
@docker push ${IMAGE_NAME}:${IMAGE_TAG}
@docker push ${IMAGE_NAME}:${IMAGE_TAG}
86 changes: 26 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ Repository for [How to set up a Python Repo for Automation and Collaboration](ht
git clone [email protected]:eugeneyan/python-collab-template.git
cd python-collab-template
# Create python environment (-B might be needed to execute)
make setup -B
# Install [Rye](https://rye-up.com/guide/) if it's not available on your system
make install-rye
# Sync Rye to your project specifications (e.g. .python-verision, added dependencies)
# Run the suite of tests and checks
make check
```

Make a pull request to this repo to see the checks in action 😎
Make a pull request to this repo to see the checks in action 😎

Here's a [sample pull request](https://github.com/eugeneyan/python-collab-template/pull/1) which initially failed ❌ the checks, and then passed ✅.

Expand Down Expand Up @@ -48,7 +50,7 @@ def test_extract_title_with_replacement(lowercased_df):
```

```shell
$ pytest
$ rye run pytest
============================= test session starts ==============================
platform darwin -- Python 3.8.2, pytest-5.4.3, py-1.8.2, pluggy-0.13.1
rootdir: /Users/eugene/projects/python-collaboration-template/tests/data_prep
Expand All @@ -62,17 +64,18 @@ test_categorical.py::test_extract_title_with_replacement PASSED [100%]

### Code Coverage
```
$ pytest --cov=src
============================= test session starts ==============================
platform darwin -- Python 3.8.2, pytest-5.4.3, py-1.8.2, pluggy-0.13.1
rootdir: /Users/eugene/projects/python-collaboration-template
plugins: cov-2.10.0
collected 9 items
$ rye run pytest --cov=src
============================================================================================================= test session starts ==============================================================================================================
platform darwin -- Python 3.12.2, pytest-8.1.1, pluggy-1.4.0
rootdir: /Users/alex.furrier/git_repositories/python-collab-template
configfile: pyproject.toml
plugins: cov-5.0.0
collected 8 items
tests/data_prep/test_categorical.py .... [ 44%]
tests/data_prep/test_continuous.py ..... [100%]
tests/data_prep/test_categorical.py ... [ 37%]
tests/data_prep/test_continuous.py ..... [100%]
---------- coverage: platform darwin, python 3.8.2-final-0 -----------
---------- coverage: platform darwin, python 3.12.2-final-0 ----------
Name Stmts Miss Cover
--------------------------------------------------
src/__init__.py 0 0 100%
Expand All @@ -82,61 +85,24 @@ src/data_prep/continuous.py 11 0 100%
--------------------------------------------------
TOTAL 23 0 100%
============================== 9 passed in 0.49s ===============================
======================================= 8 passed in 0.75s ========================================================
```

### Linting
```
$ pylint src.data_prep.categorical --reports=y
************* Module src.data_prep.categorical
src/data_prep/categorical.py:20:0: C0330: Wrong continued indentation (add 9 spaces).
df[title_col].map(replace_dict),
^ | (bad-continuation)
src/data_prep/categorical.py:21:0: C0330: Wrong continued indentation (add 9 spaces).
df[title_col])
^ | (bad-continuation)
src/data_prep/categorical.py:16:12: W1401: Anomalous backslash in string: '\.'. String constant might be missing an r prefix. (anomalous-backslash-in-string)
src/data_prep/categorical.py:1:0: C0114: Missing module docstring (missing-module-docstring)
src/data_prep/categorical.py:5:0: C0116: Missing function or method docstring (missing-function-docstring)
src/data_prep/categorical.py:9:0: C0116: Missing function or method docstring (missing-function-docstring)
src/data_prep/categorical.py:14:0: C0116: Missing function or method docstring (missing-function-docstring)
Report
======
12 statements analysed.
...
Messages
--------
+------------------------------+------------+
|message id |occurrences |
+==============================+============+
|missing-function-docstring |3 |
+------------------------------+------------+
|bad-continuation |2 |
+------------------------------+------------+
|missing-module-docstring |1 |
+------------------------------+------------+
|anomalous-backslash-in-string |1 |
+------------------------------+------------+
-----------------------------------
Your code has been rated at 4.17/10
$ rye lint src/data_prep/categorical -v
```

### Type Checking
```
$ mypy src
src/data_prep/continuous.py:23: error: Incompatible types in assignment (expression has type "str", variable has type "float")
$ rye run mypy src
src/data_prep/categorical.py:34: error: Incompatible default for argument "replace_dict" (default has type "None", argument has type "dict[Any, Any]") [assignment]
src/data_prep/categorical.py:34: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
src/data_prep/categorical.py:34: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
Found 1 error in 1 file (checked 4 source files)
```

```
$ mypy src
Success: no issues found in 4 source files
```

### Wrapping it in a Makefile
```
clean-pyc:
Expand Down Expand Up @@ -181,7 +147,7 @@ This repo is setup to package things in a Docker image for this purpose.

Through the use of [Docker Compose](https://docs.docker.com/compose/) a dev environment can also be stood up and torn down quickly. Docker compose allows for better environment setup through connected services (e.g. databases, etc) for closer replication of a production environment.

The docker compose file `docker/docker-compose.yml` builds an image from `docker/Dockerfile` and runs a bash shell.
The docker compose file `docker/docker-compose.yml` builds an image from `docker/Dockerfile` and runs a bash shell.

Environment variables can be added in the relevant section of the `docker-compose.yml` if they are provided in a `.env` file within the `docker` directory. By default the `.env` file is excluded from the repo since it may contain secrets. Instead the file `docker/template.env` is provided which should provide non secret environment variables and the variable name for required secrets.

Expand All @@ -199,7 +165,7 @@ All other Make commands should still work as before.

All changes made to relevant files inside the container will be reflected outside the container as they are bound in the `volumes` section of the `docker-compose.yml` file. Any newly added directories or files will need to be added to the `docker/Dockerfile` with a `COPY` command and bound as a volume in the docker compose file.

### Production Image
### Production Image

Once development is finished and the project is ready to be deployed it can be built and tagged as a Docker image with:

Expand All @@ -214,7 +180,7 @@ The image name and tag are set in the Makefile variables `IMAGE_NAME` and `IMAGE
If the name of the image is a container registry, the image can be pushed to the registry with:

```bash
make push-image
make push-image
```

## Misc
Expand Down
5 changes: 0 additions & 5 deletions mypy.ini

This file was deleted.

2 changes: 0 additions & 2 deletions pylintrc

This file was deleted.

34 changes: 34 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[project]
name = "python-collab-template"
version = "0.1.0"
description = "Add your description here"
authors = [
{ name = "alex furrier", email = "[email protected]" }
]
dependencies = [
"codecov>=2.1.13",
"pre-commit>=3.7.0",
"mypy>=1.9.0",
"pytest>=8.1.1",
"pytest-cov>=5.0.0",
"sphinx>=7.2.6",
"pandas>=2.2.2",
"numpy>=1.26.4",
"pandas-stubs>=2.2.1.240316",
]
readme = "README.md"
requires-python = ">= 3.12"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.rye]
managed = true
dev-dependencies = []

[tool.hatch.metadata]
allow-direct-references = true

[tool.hatch.build.targets.wheel]
packages = ["src/python_collab_template"]
Loading

0 comments on commit d85325b

Please sign in to comment.