Skip to content

Commit

Permalink
Merge pull request #53 from febus982/bootstrap-implementation-update
Browse files Browse the repository at this point in the history
Bootstrap implementation update
  • Loading branch information
febus982 authored Mar 21, 2024
2 parents d89843e + d7e0f57 commit 5cc9c01
Show file tree
Hide file tree
Showing 28 changed files with 224 additions and 542 deletions.
404 changes: 0 additions & 404 deletions .bandit.yml

This file was deleted.

2 changes: 0 additions & 2 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
version: "2"
plugins:
bandit:
enabled: true
sonar-python:
enabled: true
5 changes: 2 additions & 3 deletions .github/workflows/python-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install poetry
poetry config virtualenvs.create false
poetry install --no-root --with dev
python -m pip install poetry tox
make poetry-export
- name: Lint with ruff
run: make lint
3 changes: 0 additions & 3 deletions .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,3 @@ jobs:
- name: Test with pytest
run: |
make ci-test
- name: Check typing
run: |
make typing
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Bandit checks
name: Python typing

on:
push:
Expand All @@ -12,21 +12,19 @@ on:
workflow_dispatch:

jobs:
bandit:
typing:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Security check - Bandit
uses: ioggstream/[email protected]
with:
project_path: .
config_file: .bandit.yml

# This is optional
- name: Security check report artifacts
uses: actions/upload-artifact@v4
- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
name: Security report
path: output/security_report.txt
python-version: "3.12"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install poetry tox
make poetry-export
- name: Check typing
run: make typing
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ jobs:
# REMOVE CUSTOM REPOSITORY TO #
# PUBLISH ON OFFICIAL PYPI #
################################
with:
repository-url: https://test.pypi.org/legacy/
# with:
# repository-url: https://test.pypi.org/legacy/

site:
needs: build
Expand Down
1 change: 1 addition & 0 deletions .idea/sqlalchemy-bind-manager.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 35 additions & 26 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,40 +1,49 @@
.PHONY: docs
.PHONY: dev-dependencies update-dependencies test docs fix check typing lint format ci-test ci-coverage poetry-export

#########################
###### dev commands #####
#########################
dev-dependencies:
poetry install --with dev --no-root

update-dependencies:
poetry update --with dev

test:
poetry run pytest -n auto --cov

ci-test:
poetry run pytest

ci-coverage:
poetry run pytest --cov --cov-report lcov
docs:
poetry run mkdocs serve

typing:
poetry run mypy
fix:
poetry run ruff . --fix
poetry run ruff format .

format:
poetry run black --check .
check: poetry-export
tox

lint:
poetry run ruff .
typing: poetry-export
tox -e typing

bandit:
poetry run bandit -c .bandit.yml -r .
lint: poetry-export
tox -e lint

format-fix:
poetry run black .
format: poetry-export
tox -e format

lint-fix:
poetry run ruff . --fix

dev-dependencies:
poetry install --with dev --no-root
#########################
#### Helper commands ####
#########################
poetry-export:
poetry export -f requirements.txt --output /tmp/requirements.txt --with dev

update-dependencies:
poetry update --with dev

fix: format-fix lint-fix
check: typing format lint test bandit
#########################
###### CI commands ######
#########################
ci-test:
poetry run pytest

docs:
poetry run mkdocs serve
ci-coverage:
poetry run pytest --cov --cov-report lcov
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
[![stability-beta](https://img.shields.io/badge/stability-beta-33bbff.svg)](https://github.com/mkenney/software-guides/blob/master/STABILITY-BADGES.md#beta)

[![Python tests](https://github.com/febus982/sqlalchemy-bind-manager/actions/workflows/python-tests.yml/badge.svg?branch=main)](https://github.com/febus982/sqlalchemy-bind-manager/actions/workflows/python-tests.yml)
[![Bandit checks](https://github.com/febus982/sqlalchemy-bind-manager/actions/workflows/python-bandit.yml/badge.svg?branch=main)](https://github.com/febus982/sqlalchemy-bind-manager/actions/workflows/python-bandit.yml)
[![Maintainability](https://api.codeclimate.com/v1/badges/0140f7f4e559ae806887/maintainability)](https://codeclimate.com/github/febus982/sqlalchemy-bind-manager/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/0140f7f4e559ae806887/test_coverage)](https://codeclimate.com/github/febus982/sqlalchemy-bind-manager/test_coverage)

Expand Down
69 changes: 47 additions & 22 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,32 +47,26 @@ optional = true

[tool.poetry.group.dev.dependencies]
aiosqlite = ">=0.18.0"
bandit = ">=1.7.7"
coverage = ">=6.5.0"
black = ">=22.10.0"
mike = ">=2.0.0"
mkdocs = ">=1.4.3"
mkdocstrings = { version = ">=0.24.0", extras = ["python"] }
mkdocs-gen-files = ">=0.5.0"
mkdocs-material = ">=9.1.16"
mypy = ">=0.990"
pymdown-extensions = ">=10.0.1"
pytest = ">=7.2.0"
pytest = "^8.0.0"
pytest-asyncio = ">=0.20.3"
pytest-cov = ">=4.0.0"
pytest-factoryboy = ">=2.5.0"
pytest-xdist = ">=3.0.2"
ruff = ">=0.0.263"
tox = "^4.14.1"
mkdocs-awesome-pages-plugin = "^2.9.2"

[tool.pytest.ini_options]
asyncio_mode = "auto"
minversion = "6.0"
addopts = "-n auto --cov-report=term-missing"
testpaths = [
"tests",
]

############################
### Tools configuration ###
############################
[tool.coverage.run]
branch = true
source = ["sqlalchemy_bind_manager"]
Expand All @@ -89,20 +83,51 @@ exclude_also = [

[tool.mypy]
files = "sqlalchemy_bind_manager"
python_version = "3.8"
plugins = "pydantic.mypy"

[tool.pytest.ini_options]
asyncio_mode = "auto"
minversion = "6.0"
addopts = "-n auto --cov-report=term-missing"
testpaths = [
"tests",
]

[tool.ruff]
select = ["E", "F", "I"]
extend-exclude = ["docs"]
extend-exclude = ["docs", ".tox"]
target-version = "py38"

[tool.ruff.per-file-ignores]
[tool.ruff.lint]
select = [
"E", # pycodestyle
"W", # pycodestyle
"F", # pyflakes
"I", # isort
"N", # pep8-naming
"S", # flake8-bandit
"RUF", # ruff-specific-rules
]
# Ignoring rules problematic with formatter
# https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
ignore = [
"W191",
"E111",
"E114",
"E117",
"D206",
"D300",
"Q000",
"Q001",
"Q002",
"Q003",
"COM812",
"COM819",
"ISC001",
"ISC002",
]

[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["F401"]
"repository.py" = ["F401"]

[tool.black]
target-version = ["py38", "py39", "py310", "py311", "py312"]
extend-exclude = '''
(
/docs
)
'''
"tests/**/*.py" = ["S101"] # Allow assert usage on tests
2 changes: 1 addition & 1 deletion scripts/gen_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
full_doc_path = Path(nav_pages_path, doc_path)

# Handle edge cases
parts = (src_dir,) + tuple(module_path.parts)
parts = (src_dir, *tuple(module_path.parts))
if parts[-1] == "__init__":
parts = parts[:-1]
doc_path = doc_path.with_name("index.md")
Expand Down
8 changes: 4 additions & 4 deletions sqlalchemy_bind_manager/_bind_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
from sqlalchemy.orm.decl_api import DeclarativeMeta, registry

from sqlalchemy_bind_manager.exceptions import (
InvalidConfig,
NotInitializedBind,
InvalidConfigError,
NotInitializedBindError,
)


Expand Down Expand Up @@ -99,7 +99,7 @@ def __init_bind(
isinstance(config, SQLAlchemyAsyncConfig),
]
):
raise InvalidConfig(
raise InvalidConfigError(
f"Config for bind `{name}` is not a SQLAlchemyConfig"
f" or SQLAlchemyAsyncConfig object"
)
Expand Down Expand Up @@ -181,7 +181,7 @@ def get_bind(
try:
return self.__binds[bind_name]
except KeyError:
raise NotInitializedBind("Bind not initialized")
raise NotInitializedBindError("Bind not initialized")

def get_session(
self, bind_name: str = DEFAULT_BIND_NAME
Expand Down
8 changes: 5 additions & 3 deletions sqlalchemy_bind_manager/_repository/async_.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

from .._bind_manager import SQLAlchemyAsyncBind
from .._session_handler import AsyncSessionHandler
from ..exceptions import InvalidConfig, ModelNotFound
from ..exceptions import InvalidConfigError, ModelNotFoundError
from .base_repository import (
BaseRepository,
)
Expand Down Expand Up @@ -72,7 +72,9 @@ def __init__(
"""
super().__init__(model_class=model_class)
if not (bool(bind) ^ bool(session)):
raise InvalidConfig("Either `bind` or `session` have to be used, not both")
raise InvalidConfigError(
"Either `bind` or `session` have to be used, not both"
)
self._external_session = session
if bind:
self._session_handler = AsyncSessionHandler(bind)
Expand Down Expand Up @@ -104,7 +106,7 @@ async def get(self, identifier: PRIMARY_KEY) -> MODEL:
async with self._get_session(commit=False) as session:
model = await session.get(self._model, identifier)
if model is None:
raise ModelNotFound("No rows found for provided primary key.")
raise ModelNotFoundError("No rows found for provided primary key.")
return model

async def get_many(self, identifiers: Iterable[PRIMARY_KEY]) -> List[MODEL]:
Expand Down
12 changes: 7 additions & 5 deletions sqlalchemy_bind_manager/_repository/base_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from sqlalchemy.orm.exc import UnmappedClassError
from sqlalchemy.sql import Select

from sqlalchemy_bind_manager.exceptions import InvalidModel, UnmappedProperty
from sqlalchemy_bind_manager.exceptions import InvalidModelError, UnmappedPropertyError

from .common import (
MODEL,
Expand All @@ -54,7 +54,7 @@ def __init__(self, model_class: Union[Type[MODEL], None] = None) -> None:
if getattr(self, "_model", None) is None or not self._is_mapped_class(
self._model
):
raise InvalidModel(
raise InvalidModelError(
"You need to supply a valid model class"
" either in the `model_class` parameter"
" or in the `_model` class property."
Expand All @@ -78,11 +78,11 @@ def _validate_mapped_property(self, property_name: str) -> None:
:param property_name: The name of the property to be evaluated.
:type property_name: str
:raises UnmappedProperty: When the property is not mapped.
:raises UnmappedPropertyError: When the property is not mapped.
"""
m: Mapper = class_mapper(self._model)
if property_name not in m.column_attrs:
raise UnmappedProperty(
raise UnmappedPropertyError(
f"Property `{property_name}` is not mapped"
f" in the ORM for model `{self._model}`"
)
Expand Down Expand Up @@ -341,4 +341,6 @@ def _model_pk(self) -> str:

def _fail_if_invalid_models(self, objects: Iterable[MODEL]) -> None:
if [x for x in objects if not isinstance(x, self._model)]:
raise InvalidModel("Cannot handle models not belonging to this repository")
raise InvalidModelError(
"Cannot handle models not belonging to this repository"
)
Loading

0 comments on commit 5cc9c01

Please sign in to comment.