Skip to content

Commit

Permalink
fixes based on PR review, + ruff formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
roman2git committed Nov 19, 2024
1 parent c4025a9 commit 437bc54
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 53 deletions.
18 changes: 0 additions & 18 deletions .coveragerc

This file was deleted.

18 changes: 18 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,21 @@ exclude = ["tests/*"]

[tool.ruff.format]
docstring-code-format = true

[tool.coverage.paths]
source = [
"src/arta",
"*/.tox/*/lib/python*/site-packages/arta",
"*/.tox/pypy*/site-packages/arta",
"*/.tox\\*\\Lib\\site-packages\\arta",
"*/src/arta",
"*\\src\\arta"]

[tool.coverage.run]
branch = true
parallel = false
source = ["arta"]
omit = ["*/tests/*"]

[tool.coverage.report]
omit = ["tests"]
44 changes: 17 additions & 27 deletions src/arta/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
Note: Having no "from __future__ import annotations" here is wanted (pydantic compatibility).
"""

from typing import Any, Callable, Dict, List, Optional, Annotated
from typing import Annotated, Any, Callable, Dict, List, Optional

import pydantic
from pydantic.version import VERSION

from arta.utils import ParsingErrorStrategy


if not VERSION.startswith('1.'):
if not VERSION.startswith("1."):
# ----------------------------------
# For instantiation using rules_dict
class RuleRaw(pydantic.BaseModel):
Expand All @@ -22,21 +21,18 @@ class RuleRaw(pydantic.BaseModel):
action: Callable
action_parameters: Optional[Dict[str, Any]]

model_config = pydantic.ConfigDict(extra='forbid')

model_config = pydantic.ConfigDict(extra="forbid")

class RulesGroup(pydantic.RootModel): # noqa
class RulesGroup(pydantic.RootModel): # noqa
"""Pydantic model for validating a rules group."""

root: Dict[str, RuleRaw]


class RulesDict(pydantic.RootModel): # noqa
class RulesDict(pydantic.RootModel): # noqa
"""Pydantic model for validating rules dict instanciation."""

root: Dict[str, RulesGroup]


# ----------------------------------
# For instantiation using config_path
class Condition(pydantic.BaseModel):
Expand All @@ -46,7 +42,6 @@ class Condition(pydantic.BaseModel):
validation_function: str
condition_parameters: Optional[Dict[str, Any]] = None


class RulesConfig(pydantic.BaseModel):
"""Pydantic model for validating a rule group from config file."""

Expand All @@ -55,7 +50,7 @@ class RulesConfig(pydantic.BaseModel):
action: Annotated[str, pydantic.StringConstraints(to_lower=True)] # type: ignore
action_parameters: Optional[Any] = None

model_config = pydantic.ConfigDict(extra='allow')
model_config = pydantic.ConfigDict(extra="allow")

class Configuration(pydantic.BaseModel):
"""Pydantic model for validating configuration files."""
Expand All @@ -68,11 +63,11 @@ class Configuration(pydantic.BaseModel):
rules: Dict[str, Dict[str, Dict[str, RulesConfig]]]
parsing_error_strategy: Optional[ParsingErrorStrategy] = None

model_config = pydantic.ConfigDict(extra='ignore')
model_config = pydantic.ConfigDict(extra="ignore")

@pydantic.field_validator('rules', mode='before') # noqa
def upper_key(cls, vl): # noqa
""" Validate and uppercase keys for RulesConfig """
@pydantic.field_validator("rules", mode="before") # noqa
def upper_key(cls, vl): # noqa
"""Validate and uppercase keys for RulesConfig"""
for k, v in vl.items():
for kk, vv in v.items():
for key, rules in [*vv.items()]:
Expand All @@ -86,15 +81,15 @@ def upper_key(cls, vl): # noqa
class BaseModelV2(pydantic.BaseModel):
"""Wrapper to expose missed methods used elsewhere in the code"""

model_dump = pydantic.BaseModel.dict # noqa
model_dump: Callable = pydantic.BaseModel.dict # noqa

@classmethod
def model_validate(cls, obj): # noqa
return cls.parse_obj(obj) # noqa

# ----------------------------------
# For instantiation using rules_dict
class RuleRaw(BaseModelV2):
class RuleRaw(BaseModelV2): # type: ignore[no-redef]
"""Pydantic model for validating a rule."""

condition: Optional[Callable]
Expand All @@ -105,30 +100,26 @@ class RuleRaw(BaseModelV2):
class Config:
extra = "forbid"


class RulesGroup(pydantic.BaseModel): # noqa
class RulesGroup(pydantic.BaseModel): # type: ignore[no-redef] # noqa
"""Pydantic model for validating a rules group."""

__root__: Dict[str, RuleRaw] # noqa


class RulesDict(BaseModelV2): # noqa
class RulesDict(BaseModelV2): # type: ignore[no-redef] # noqa
"""Pydantic model for validating rules dict instanciation."""

__root__: Dict[str, RulesGroup] # noqa


# ----------------------------------
# For instantiation using config_path
class Condition(BaseModelV2):
class Condition(BaseModelV2): # type: ignore[no-redef]
"""Pydantic model for validating a condition."""

description: str
validation_function: str
condition_parameters: Optional[Dict[str, Any]]


class RulesConfig(BaseModelV2):
class RulesConfig(BaseModelV2): # type: ignore[no-redef]
"""Pydantic model for validating a rule group from config file."""

condition: Optional[str]
Expand All @@ -139,8 +130,7 @@ class RulesConfig(BaseModelV2):
class Config:
extra = "allow"


class Configuration(BaseModelV2):
class Configuration(BaseModelV2): # type: ignore[no-redef]
"""Pydantic model for validating configuration files."""

conditions: Optional[Dict[str, Condition]]
Expand Down
8 changes: 0 additions & 8 deletions src/arta/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ class Rule:
action: Function to perform when the conditions are valid (action function).
action_parameters: Parameters of the action function.
"""
set_id: str
group_id: str
rule_id: str
condition_exprs: dict[str, str | None]
condition_factory_mapping: dict[str, type[BaseCondition]]
action: Callable
std_condition_instances: dict[str, StandardCondition]
action_parameters: dict[str, Any] | None = None

def __init__(
self,
Expand Down

0 comments on commit 437bc54

Please sign in to comment.