Skip to content

Commit

Permalink
feat: add error_messages as class level configuration (#315)
Browse files Browse the repository at this point in the history
* feat: add `error_messages` as class level configuration

* fix: build updates

* feat: updates exclusion

* feat: one more change

* feat: updated coverage config for sonar

* fix: type correction for bind_key
  • Loading branch information
cofin authored Dec 16, 2024
1 parent ecc15b0 commit 4eec31b
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 209 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,6 @@ jobs:
with:
name: coverage-xml

- name: Fix coverage file for sonarcloud
run: sed -i "s/home\/runner\/work\/advanced-alchemy\/advanced-alchemy/github\/workspace/g" coverage.xml

- name: SonarCloud Scan
uses: sonarsource/sonarcloud-github-action@master
env:
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ repos:
- id: unasyncd
additional_dependencies: ["ruff"]
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.8.2"
rev: "v0.8.3"
hooks:
# Run the linter.
- id: ruff
Expand Down
2 changes: 1 addition & 1 deletion advanced_alchemy/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ class AdvancedDeclarativeBase(DeclarativeBase):
registry = orm_registry
__abstract__ = True
__metadata_registry__: MetadataRegistry = MetadataRegistry()
__bind_key__: Optional[None] = None # noqa: UP007
__bind_key__: Optional[str] = None # noqa: UP007

def __init_subclass__(cls, **kwargs: Any) -> None:
bind_key = getattr(cls, "__bind_key__", None)
Expand Down
22 changes: 13 additions & 9 deletions advanced_alchemy/repository/_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
Iterable,
List,
Literal,
Optional,
Protocol,
Sequence,
Tuple,
Expand Down Expand Up @@ -407,6 +406,8 @@ class SQLAlchemyAsyncRepository(SQLAlchemyAsyncRepositoryProtocol[ModelT], Filte
"""Name of the unique identifier for the model."""
loader_options: LoadSpec | None = None
"""Default loader options for the repository."""
error_messages: ErrorMessages | None = None
"""Default error messages for the repository."""
inherit_lazy_relationships: bool = True
"""Optionally ignore the default ``lazy`` configuration for model relationships. This is useful for when you want to
replace instead of merge the model's loaded relationships with the ones specified in the ``load`` or ``default_loader_options`` configuration."""
Expand Down Expand Up @@ -457,7 +458,9 @@ def __init__(
self.auto_commit = auto_commit
self.order_by = order_by
self.session = session
self.error_messages = self._get_error_messages(error_messages=error_messages)
self.error_messages = self._get_error_messages(
error_messages=error_messages, default_messages=self.error_messages
)
self._default_loader_options, self._loader_options_have_wildcards = get_abstract_loader_options(
loader_options=load if load is not None else self.loader_options,
inherit_lazy_relationships=self.inherit_lazy_relationships,
Expand All @@ -476,13 +479,14 @@ def _get_error_messages(
) -> ErrorMessages | None:
if error_messages == Empty:
error_messages = None
default_messages = cast(
"Optional[ErrorMessages]",
default_messages if default_messages != Empty else DEFAULT_ERROR_MESSAGE_TEMPLATES,
)
if error_messages is not None and default_messages is not None:
default_messages.update(cast("ErrorMessages", error_messages))
return default_messages
if default_messages == Empty:
default_messages = None
messages = DEFAULT_ERROR_MESSAGE_TEMPLATES
if default_messages and isinstance(default_messages, dict):
messages.update(default_messages)
if error_messages:
messages.update(cast("ErrorMessages", error_messages))
return messages

@classmethod
def get_id_attribute_value(
Expand Down
22 changes: 13 additions & 9 deletions advanced_alchemy/repository/_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
Iterable,
List,
Literal,
Optional,
Protocol,
Sequence,
Tuple,
Expand Down Expand Up @@ -408,6 +407,8 @@ class SQLAlchemySyncRepository(SQLAlchemySyncRepositoryProtocol[ModelT], Filtera
"""Name of the unique identifier for the model."""
loader_options: LoadSpec | None = None
"""Default loader options for the repository."""
error_messages: ErrorMessages | None = None
"""Default error messages for the repository."""
inherit_lazy_relationships: bool = True
"""Optionally ignore the default ``lazy`` configuration for model relationships. This is useful for when you want to
replace instead of merge the model's loaded relationships with the ones specified in the ``load`` or ``default_loader_options`` configuration."""
Expand Down Expand Up @@ -458,7 +459,9 @@ def __init__(
self.auto_commit = auto_commit
self.order_by = order_by
self.session = session
self.error_messages = self._get_error_messages(error_messages=error_messages)
self.error_messages = self._get_error_messages(
error_messages=error_messages, default_messages=self.error_messages
)
self._default_loader_options, self._loader_options_have_wildcards = get_abstract_loader_options(
loader_options=load if load is not None else self.loader_options,
inherit_lazy_relationships=self.inherit_lazy_relationships,
Expand All @@ -477,13 +480,14 @@ def _get_error_messages(
) -> ErrorMessages | None:
if error_messages == Empty:
error_messages = None
default_messages = cast(
"Optional[ErrorMessages]",
default_messages if default_messages != Empty else DEFAULT_ERROR_MESSAGE_TEMPLATES,
)
if error_messages is not None and default_messages is not None:
default_messages.update(cast("ErrorMessages", error_messages))
return default_messages
if default_messages == Empty:
default_messages = None
messages = DEFAULT_ERROR_MESSAGE_TEMPLATES
if default_messages and isinstance(default_messages, dict):
messages.update(default_messages)
if error_messages:
messages.update(cast("ErrorMessages", error_messages))
return messages

@classmethod
def get_id_attribute_value(
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ dev = [
doc = [
"auto-pytabs[sphinx]>=0.5.0",
"git-cliff>=2.6.1",
"litestar-sphinx-theme @ git+https://github.com/litestar-org/litestar-sphinx-theme.git@v3",
"litestar-sphinx-theme @ git+https://github.com/litestar-org/litestar-sphinx-theme.git@v3-uv",
"sphinx>=7.0.0; python_version <= \"3.9\"",
"sphinx>=8.0.0; python_version >= \"3.10\"",
"sphinx-autobuild>=2021.3.14",
Expand Down Expand Up @@ -193,6 +193,7 @@ markers = [
testpaths = ["tests"]

[tool.coverage.run]
branch = true
concurrency = ["multiprocessing"]
omit = [
"*/tests/*",
Expand All @@ -205,6 +206,7 @@ omit = [
"advanced_alchemy/service/*",
]
parallel = true
relative_files = true

[tool.coverage.report]
exclude_lines = [
Expand Down
10 changes: 3 additions & 7 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
sonar.organization=litestar-api
sonar.projectKey=litestar-org_advanced-alchemy
sonar.python.coverage.reportPaths=coverage.xml
sonar.python.version=3.8, 3.9, 3.10, 3.11, 3.12
sonar.python.version=3.9, 3.10, 3.11, 3.12
sonar.sourceEncoding=UTF-8
sonar.sources=advanced_alchemy
sonar.tests=tests
sonar.coverage.exclusions=\
**/__init__.py, \
examples/*.py, \
tests/*.py, \
tests/**/*.py, \
Expand All @@ -21,9 +20,7 @@ sonar.coverage.exclusions=\
advanced_alchemy/service/_sync.py, \
advanced_alchemy/service/_async.py, \
advanced_alchemy/service/pagination.py, \
advanced_alchemy/extensions/litestar/plugins/init/config/asyncio.py, \
advanced_alchemy/extensions/litestar/plugins/init/config/sync.py, \
advanced_alchemy/base.py
advanced_alchemy/extensions/litestar/plugins/init/config/*.py
sonar.cpd.exclusions=\
advanced_alchemy/repository/memory/_sync.py, \
advanced_alchemy/repository/memory/_async.py, \
Expand All @@ -36,6 +33,5 @@ sonar.cpd.exclusions=\
examples/*.py, \
examples/fastapi.py, \
tests/integration/conftest.py, \
advanced_alchemy/extensions/litestar/plugins/init/config/asyncio.py, \
advanced_alchemy/extensions/litestar/plugins/init/config/sync.py
advanced_alchemy/extensions/litestar/plugins/init/config/*.py
sonar.projectName=Advanced Alchemy
Loading

0 comments on commit 4eec31b

Please sign in to comment.