From fcda8f12f7ebb45c6dec01c8c77d6d70d69ceca2 Mon Sep 17 00:00:00 2001 From: Monique Rio Date: Fri, 20 Sep 2024 14:56:55 +0000 Subject: [PATCH] chore: add ruff linting --- .devcontainer/devcontainer.json | 4 +-- .github/workflows/tests.yaml | 2 ++ .gitignore | 1 + aim/digifeeds/database/crud.py | 37 +++++++++++++++------- aim/digifeeds/database/main.py | 4 +-- aim/digifeeds/database/migrations/env.py | 9 +++--- aim/digifeeds/database/models.py | 2 +- poetry.lock | 39 ++++++++++++------------ pyproject.toml | 2 +- tests/conftest.py | 2 +- tests/digifeeds/database/test_crud.py | 4 +-- tests/digifeeds/database/test_main.py | 2 +- 12 files changed, 63 insertions(+), 45 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 3c0b18e..34349ee 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -19,8 +19,8 @@ "workspaceFolder": "/app", "features": { "ghcr.io/devcontainers/features/git:1": {}, - "ghcr.io/devcontainers-contrib/features/ruff:1": {}, - "ghcr.io/devcontainers/features/sshd:1": {} + "ghcr.io/devcontainers/features/sshd:1": {}, + "ghcr.io/devcontainers/features/python:1": {} } // Features to add to the dev container. More info: https://containers.dev/features. diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 722ab9f..fa904d2 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -21,6 +21,8 @@ jobs: - run: poetry install - name: setup pytest.ini run: mv .github/pytest.ini pytest.ini + - name: Linting + run: poetry run ruff check - name: Run tests env: CI: "true" diff --git a/.gitignore b/.gitignore index cf363fd..ae5d474 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ __pycache__ .ssh htmlcov/ .coverage +.gnupg \ No newline at end of file diff --git a/aim/digifeeds/database/crud.py b/aim/digifeeds/database/crud.py index 4d8dd02..8f58cce 100644 --- a/aim/digifeeds/database/crud.py +++ b/aim/digifeeds/database/crud.py @@ -2,16 +2,28 @@ from aim.digifeeds.database import schemas from aim.digifeeds.database import models + def get_item(db: Session, barcode: str): return db.query(models.Item).filter(models.Item.barcode == barcode).first() + def get_items(db: Session, in_zephir: bool | None): - if in_zephir == True: - #this is working - return db.query(models.Item).filter(models.Item.statuses.any(models.ItemStatus.status_name == "in_zephir")).all() - elif in_zephir == False: - #this is not working - return db.query(models.Item).filter(~models.Item.statuses.any(models.ItemStatus.status_name == "in_zephir")).all() + if in_zephir is True: + return ( + db.query(models.Item) + .filter( + models.Item.statuses.any(models.ItemStatus.status_name == "in_zephir") + ) + .all() + ) + elif in_zephir is False: + return ( + db.query(models.Item) + .filter( + ~models.Item.statuses.any(models.ItemStatus.status_name == "in_zephir") + ) + .all() + ) return db.query(models.Item).all() @@ -23,15 +35,18 @@ def add_item(db: Session, item: schemas.ItemCreate): db.refresh(db_item) return db_item + def get_status(db: Session, name: str): return db.query(models.Status).filter(models.Status.name == name).first() + def get_statuses(db: Session): return db.query(models.Status).all() + def add_item_status(db: Session, item: models.Item, status: models.Status): - db_item_status = models.ItemStatus(item=item, status=status) - db.add(db_item_status) - db.commit() - db.refresh(item) - return item + db_item_status = models.ItemStatus(item=item, status=status) + db.add(db_item_status) + db.commit() + db.refresh(item) + return item diff --git a/aim/digifeeds/database/main.py b/aim/digifeeds/database/main.py index e796412..774bf5f 100644 --- a/aim/digifeeds/database/main.py +++ b/aim/digifeeds/database/main.py @@ -1,10 +1,10 @@ from fastapi import Depends, FastAPI, HTTPException from sqlalchemy import create_engine from sqlalchemy.orm import Session, sessionmaker -from aim.digifeeds.database import crud, models, schemas +from aim.digifeeds.database import crud, schemas from aim.services import S -if S.ci_on == None: # pragma: no cover +if S.ci_on is None: # pragma: no cover engine = create_engine(S.mysql_database) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) # models.Base.metadata.create_all(bind=engine) diff --git a/aim/digifeeds/database/migrations/env.py b/aim/digifeeds/database/migrations/env.py index 8738815..aa2ba63 100644 --- a/aim/digifeeds/database/migrations/env.py +++ b/aim/digifeeds/database/migrations/env.py @@ -5,6 +5,7 @@ from alembic import context +from aim.digifeeds.database.models import Base from aim.services import S # this is the Alembic Config object, which provides @@ -12,7 +13,7 @@ config = context.config # Set url from environment variable -config.set_main_option('sqlalchemy.url', S.mysql_database) +config.set_main_option("sqlalchemy.url", S.mysql_database) # Interpret the config file for Python logging. # This line sets up loggers basically. @@ -21,7 +22,7 @@ # add your model's MetaData object here # for 'autogenerate' support -from aim.digifeeds.database.models import Base + target_metadata = Base.metadata # other values from the config, defined by the needs of env.py, @@ -68,9 +69,7 @@ def run_migrations_online() -> None: ) with connectable.connect() as connection: - context.configure( - connection=connection, target_metadata=target_metadata - ) + context.configure(connection=connection, target_metadata=target_metadata) with context.begin_transaction(): context.run_migrations() diff --git a/aim/digifeeds/database/models.py b/aim/digifeeds/database/models.py index 8f432a6..3049d8e 100644 --- a/aim/digifeeds/database/models.py +++ b/aim/digifeeds/database/models.py @@ -53,7 +53,7 @@ def load_statuses(session: Session): objects = [] for status in statuses: sts = session.query(Status).filter_by(name = status["name"]).first() - if sts == None: + if sts is None: objects.append(Status(**status)) print(f"Statuses to load: {objects}") diff --git a/poetry.lock b/poetry.lock index f5aee2f..89afb01 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1048,28 +1048,29 @@ jupyter = ["ipywidgets (>=7.5.1,<9)"] [[package]] name = "ruff" -version = "0.2.2" +version = "0.6.6" description = "An extremely fast Python linter and code formatter, written in Rust." optional = false python-versions = ">=3.7" files = [ - {file = "ruff-0.2.2-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl", hash = "sha256:0a9efb032855ffb3c21f6405751d5e147b0c6b631e3ca3f6b20f917572b97eb6"}, - {file = "ruff-0.2.2-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:d450b7fbff85913f866a5384d8912710936e2b96da74541c82c1b458472ddb39"}, - {file = "ruff-0.2.2-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ecd46e3106850a5c26aee114e562c329f9a1fbe9e4821b008c4404f64ff9ce73"}, - {file = "ruff-0.2.2-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:5e22676a5b875bd72acd3d11d5fa9075d3a5f53b877fe7b4793e4673499318ba"}, - {file = "ruff-0.2.2-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:1695700d1e25a99d28f7a1636d85bafcc5030bba9d0578c0781ba1790dbcf51c"}, - {file = "ruff-0.2.2-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:b0c232af3d0bd8f521806223723456ffebf8e323bd1e4e82b0befb20ba18388e"}, - {file = "ruff-0.2.2-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:f63d96494eeec2fc70d909393bcd76c69f35334cdbd9e20d089fb3f0640216ca"}, - {file = "ruff-0.2.2-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:6a61ea0ff048e06de273b2e45bd72629f470f5da8f71daf09fe481278b175001"}, - {file = "ruff-0.2.2-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:5e1439c8f407e4f356470e54cdecdca1bd5439a0673792dbe34a2b0a551a2fe3"}, - {file = "ruff-0.2.2-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:940de32dc8853eba0f67f7198b3e79bc6ba95c2edbfdfac2144c8235114d6726"}, - {file = "ruff-0.2.2-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:0c126da55c38dd917621552ab430213bdb3273bb10ddb67bc4b761989210eb6e"}, - {file = "ruff-0.2.2-py3-none-musllinux_1_2_i686.whl", hash = "sha256:3b65494f7e4bed2e74110dac1f0d17dc8e1f42faaa784e7c58a98e335ec83d7e"}, - {file = "ruff-0.2.2-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:1ec49be4fe6ddac0503833f3ed8930528e26d1e60ad35c2446da372d16651ce9"}, - {file = "ruff-0.2.2-py3-none-win32.whl", hash = "sha256:d920499b576f6c68295bc04e7b17b6544d9d05f196bb3aac4358792ef6f34325"}, - {file = "ruff-0.2.2-py3-none-win_amd64.whl", hash = "sha256:cc9a91ae137d687f43a44c900e5d95e9617cb37d4c989e462980ba27039d239d"}, - {file = "ruff-0.2.2-py3-none-win_arm64.whl", hash = "sha256:c9d15fc41e6054bfc7200478720570078f0b41c9ae4f010bcc16bd6f4d1aacdd"}, - {file = "ruff-0.2.2.tar.gz", hash = "sha256:e62ed7f36b3068a30ba39193a14274cd706bc486fad521276458022f7bccb31d"}, + {file = "ruff-0.6.6-py3-none-linux_armv6l.whl", hash = "sha256:f5bc5398457484fc0374425b43b030e4668ed4d2da8ee7fdda0e926c9f11ccfb"}, + {file = "ruff-0.6.6-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:515a698254c9c47bb84335281a170213b3ee5eb47feebe903e1be10087a167ce"}, + {file = "ruff-0.6.6-py3-none-macosx_11_0_arm64.whl", hash = "sha256:6bb1b4995775f1837ab70f26698dd73852bbb82e8f70b175d2713c0354fe9182"}, + {file = "ruff-0.6.6-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:69c546f412dfae8bb9cc4f27f0e45cdd554e42fecbb34f03312b93368e1cd0a6"}, + {file = "ruff-0.6.6-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:59627e97364329e4eae7d86fa7980c10e2b129e2293d25c478ebcb861b3e3fd6"}, + {file = "ruff-0.6.6-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:94c3f78c3d32190aafbb6bc5410c96cfed0a88aadb49c3f852bbc2aa9783a7d8"}, + {file = "ruff-0.6.6-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:704da526c1e137f38c8a067a4a975fe6834b9f8ba7dbc5fd7503d58148851b8f"}, + {file = "ruff-0.6.6-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:efeede5815a24104579a0f6320660536c5ffc1c91ae94f8c65659af915fb9de9"}, + {file = "ruff-0.6.6-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:e368aef0cc02ca3593eae2fb8186b81c9c2b3f39acaaa1108eb6b4d04617e61f"}, + {file = "ruff-0.6.6-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:2653fc3b2a9315bd809725c88dd2446550099728d077a04191febb5ea79a4f79"}, + {file = "ruff-0.6.6-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:bb858cd9ce2d062503337c5b9784d7b583bcf9d1a43c4df6ccb5eab774fbafcb"}, + {file = "ruff-0.6.6-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:488f8e15c01ea9afb8c0ba35d55bd951f484d0c1b7c5fd746ce3c47ccdedce68"}, + {file = "ruff-0.6.6-py3-none-musllinux_1_2_i686.whl", hash = "sha256:aefb0bd15f1cfa4c9c227b6120573bb3d6c4ee3b29fb54a5ad58f03859bc43c6"}, + {file = "ruff-0.6.6-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:a4c0698cc780bcb2c61496cbd56b6a3ac0ad858c966652f7dbf4ceb029252fbe"}, + {file = "ruff-0.6.6-py3-none-win32.whl", hash = "sha256:aadf81ddc8ab5b62da7aae78a91ec933cbae9f8f1663ec0325dae2c364e4ad84"}, + {file = "ruff-0.6.6-py3-none-win_amd64.whl", hash = "sha256:0adb801771bc1f1b8cf4e0a6fdc30776e7c1894810ff3b344e50da82ef50eeb1"}, + {file = "ruff-0.6.6-py3-none-win_arm64.whl", hash = "sha256:4b4d32c137bc781c298964dd4e52f07d6f7d57c03eae97a72d97856844aa510a"}, + {file = "ruff-0.6.6.tar.gz", hash = "sha256:0fc030b6fd14814d69ac0196396f6761921bd20831725c7361e1b8100b818034"}, ] [[package]] @@ -1505,4 +1506,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "442afe890269062175fae9c2404d34a32d4249a83e7c57743822b9d2578f1fe7" +content-hash = "9bbea301b59c5368e45e7f10ab3322d608f1b200fb42c3148f4f92d5b21d8367" diff --git a/pyproject.toml b/pyproject.toml index 8bf417e..d21fc17 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,8 +18,8 @@ alembic = "^1.13.2" [tool.poetry.group.dev.dependencies] pytest = "^8.0.2" -ruff = "^0.2.2" pytest-cov = "^5.0.0" +ruff = "^0.6.6" [tool.pytest.ini_options] addopts = "--cov=aim --cov-report=html --cov-report=term:skip-covered" diff --git a/tests/conftest.py b/tests/conftest.py index 9fb7637..8e71b08 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,7 +4,7 @@ from sqlalchemy import StaticPool, create_engine from sqlalchemy.orm import sessionmaker from aim.digifeeds.database.main import app, get_db -from aim.digifeeds.database.models import Base, load_statuses, Status +from aim.digifeeds.database.models import Base, load_statuses from aim.services import S diff --git a/tests/digifeeds/database/test_crud.py b/tests/digifeeds/database/test_crud.py index bfe9b35..ae9de03 100644 --- a/tests/digifeeds/database/test_crud.py +++ b/tests/digifeeds/database/test_crud.py @@ -17,7 +17,7 @@ def test_get_item(self, db_session): def test_get_item_that_does_not_exist(self, db_session): item_in_db = get_item(barcode="does not exist", db=db_session) - assert(item_in_db) == None + assert(item_in_db) is None def test_get_items_all(self, db_session): item1 = add_item(db=db_session, item=ItemCreate(barcode="valid_barcode")) @@ -58,7 +58,7 @@ def test_get_status_that_exists(self, db_session): def test_get_status_that_does_not_exist(self, db_session): status = get_status(db=db_session, name="does_not_exist") - assert(status) == None + assert(status) is None def test_get_statuses(self, db_session): statuses = get_statuses(db=db_session) diff --git a/tests/digifeeds/database/test_main.py b/tests/digifeeds/database/test_main.py index 6c5da82..3996f6d 100644 --- a/tests/digifeeds/database/test_main.py +++ b/tests/digifeeds/database/test_main.py @@ -1,4 +1,4 @@ -from aim.digifeeds.database import crud, models +from aim.digifeeds.database import crud from aim.digifeeds.database.schemas import ItemCreate import pytest