Skip to content

Commit

Permalink
chore: Upgrade python/pytest + migrate to ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
ducdetronquito committed Nov 12, 2024
1 parent 66f6767 commit 1f40780
Show file tree
Hide file tree
Showing 14 changed files with 388 additions and 245 deletions.
7 changes: 7 additions & 0 deletions .config/mise/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[tools]
python = "3.11"
poetry = "latest"

[env]
_.file = '../../.env'
_.python.venv = { path = "../../.venv", create = false }
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.venv
.pytest_cache
.ruff_cache
4 changes: 0 additions & 4 deletions .flake8

This file was deleted.

39 changes: 24 additions & 15 deletions .github/workflows/pullrequest-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,56 @@ on:
- "master"

jobs:

releasetag:
uses: ZeroGachis/.github/.github/workflows/releasetag.yml@v4

build:
uses: ZeroGachis/.github/.github/workflows/build-image.yml@v1
needs: releasetag
uses: ZeroGachis/.github/.github/workflows/build-image.yml@v4
with:
image_name: nurse
dockerfile_context: .
vault_enabled: false
tailscale_enabled: false
regitry_url: ghcr.io
build-args: |
REQUIREMENTS=dev
secrets: inherit

static-metrics:
needs:
needs:
[
build,
build,
]
uses: ZeroGachis/.github/.github/workflows/run-docker.yml@v1
uses: ZeroGachis/.github/.github/workflows/run-docker.yml@v4
with:
image_url: ${{ needs.build.outputs.image-url }}
workdir: /home/src/nurse
tailscale_enabled: false
vault_enabled: false
workdir: /app
run_command: |
flake8 .
ruff check --diff ./
ruff format --check --diff ./
secrets: inherit


unit-tests:
needs:
needs:
[
build,
build,
]
uses: ZeroGachis/.github/.github/workflows/run-docker-with-db.yml@v1
uses: ZeroGachis/.github/.github/workflows/run-pytest.yml@v4
with:
image_url: ${{ needs.build.outputs.image-url }}
workdir: /home/src/nurse
run_command: |
pytest --verbose
enable_test_report: false
workdir: /app
directory_to_test: tests/
env_django_settings_module: todo-make-this-optional
secrets: inherit

detroy:
if: always()
needs:
[
releasetag,
build,
unit-tests,
static-metrics,
Expand Down
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ env/
dist/
nurse.egg-info/
pip-wheel-metadata/
.venv/

# Python
__pycache__/
Expand All @@ -23,3 +24,10 @@ __pycache__/

# Asdf
.tool-versions

# Dev env variables
.env

# Coverage
.coverage
reports/
15 changes: 15 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
fail_fast: true
repos:
- repo: local
hooks:
- id: ruff-check
name: Ruff check
entry: poetry run ruff check
args: [--fix]
language: system
types: [file, python]
- id: ruff-format
name: Ruff format
entry: poetry run ruff format
language: system
types: [file, python]
28 changes: 8 additions & 20 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
FROM python:3.11-bookworm
FROM 007065811408.dkr.ecr.eu-west-3.amazonaws.com/python-3-11:1

RUN \
apt-get -y update \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app

RUN pip3 install --upgrade pip \
&& pip3 install -U setuptools
COPY pyproject.toml poetry.lock poetry.toml ./

RUN pip install poetry
# Poetry complains if no README.md exists
RUN touch README.md

COPY poetry.lock /home/src/nurse/poetry.lock
COPY pyproject.toml /home/src/nurse/pyproject.toml
RUN POETRY_NO_INTERACTION=1 poetry install

WORKDIR /home/src/nurse
COPY . .

ARG REQUIREMENTS=common
ENV REQUIREMENTS $REQUIREMENTS
RUN mkdir requirements
RUN poetry export -f requirements.txt --without-hashes > requirements/$REQUIREMENTS.txt
RUN poetry export -f requirements.txt --without-hashes --with dev > requirements/dev.txt

RUN pip3 install -r requirements/$REQUIREMENTS.txt

COPY . /home/src/nurse
CMD ["bash"]
CMD ["bash"]
9 changes: 3 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
version: '3.0'

services:

nurse:
build:
context: .
args:
- REQUIREMENTS=dev
tty: true
working_dir: /home/src/nurse
working_dir: /app
volumes:
- ./.:/home/src/nurse
- ./.:/app
- /app/.venv
2 changes: 1 addition & 1 deletion nurse/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def get_service(service_catalog: ServiceCatalog, decorated_obj, param_to_inject:
service = service_catalog._services.get(service_type)
if not service:
raise DependencyError(
f"Dependency `{service_type}` for `{param_to_inject}` was not found."
f"Dependency `{service_type.__name__}` for `{param_to_inject}` was not found."
)

return service
459 changes: 278 additions & 181 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions poetry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
virtualenvs.create = true
virtualenvs.in-project = true
30 changes: 24 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "nurse"
version = "0.5.1"
description = "A thoughtful dependency injection framework 💉"
description = "A thoughtful dependency injection framework"
authors = ["ducdetronquito <[email protected]>"]

readme = "README.rst"
Expand All @@ -13,11 +13,29 @@ keywords = ["dependency", "injection"]
[tool.poetry.dependencies]
python = "^3.9"


[tool.poetry.group.dev.dependencies]
flake8 = "^6.1.0"
pytest = "^3.0"
black = { version = "*", allow-prereleases = true }
pytest = "^8.0"
ruff = "^0.7.3"
pre-commit = "^4.0.1"
pytest-cov = "^6.0.0"

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.pytest.ini_options]
addopts = ["--import-mode=importlib"]
pythonpath = "./"

[tool.coverage.run]
branch = true
relative_files = true
# source = ["nurse"]

[tool.ruff]
# Use black default line length to avoid to many changes at first
line-length = 88
# It would be preferable to use python's standard "project.requires-python"
# but poetry does not comply with it.
target-version = "py311"
Empty file removed tests/__init__.py
Empty file.
27 changes: 15 additions & 12 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from unittest import TestCase
import nurse
import pytest

from nurse.exceptions import DependencyError
import asyncio


class TestServe:
def setup(self):
class TestServe(TestCase):
def tearDown(self):
super().tearDown()
nurse.clear()

def test_can_inject_a_dependency(self):
Expand Down Expand Up @@ -44,7 +46,9 @@ class Game:
game = Game()
assert game.player.name == "Igor"

def test_cannot_serve_a_dependency_if_it_does_not_subclass_the_provided_interface(self):
def test_cannot_serve_a_dependency_if_it_does_not_subclass_the_provided_interface(
self,
):
class User:
@property
def name(self):
Expand All @@ -67,8 +71,9 @@ def get_name(self):
return self.name


class TestInjectMethod:
def setup(self):
class TestInjectMethod(TestCase):
def tearDown(self):
super().tearDown()
nurse.clear()

def test_methods_with_only_dependency_args(self):
Expand All @@ -95,8 +100,7 @@ def foo(service):
pass

with pytest.raises(
DependencyError,
message="Args `service` must be typed to be injected."
DependencyError, match="Args `service` must be typed to be injected."
):
foo()

Expand All @@ -107,12 +111,11 @@ def foo(service: ServiceDependency):

with pytest.raises(
DependencyError,
message="Dependency `ServiceDependency` for `service` arg was not found."
match="Dependency `ServiceDependency` for `service` was not found.",
):
foo()

def test_async_method(self):

@nurse.inject("service")
async def foo(service: ServiceDependency):
return service.get_name()
Expand All @@ -124,9 +127,9 @@ async def foo(service: ServiceDependency):
assert res == "Leroy Jenkins"


class TestGet:

def setup(self):
class TestGet(TestCase):
def tearDown(self):
super().tearDown()
nurse.clear()

def test_retrieve_service(self):
Expand Down

0 comments on commit 1f40780

Please sign in to comment.