|
23 | 23 | # SOFTWARE. |
24 | 24 | # |
25 | 25 | ############################################################################### |
26 | | -from pydantic import BaseModel |
| 26 | +from typing import Any |
| 27 | + |
| 28 | +from pydantic import BaseModel, model_validator |
27 | 29 |
|
28 | 30 |
|
29 | 31 | class AnalyzerArgs(BaseModel): |
| 32 | + """Base class for all analyzer arguments. |
| 33 | +
|
| 34 | + This class provides automatic string stripping for all string values |
| 35 | + in analyzer args. All analyzer args classes should inherit from this |
| 36 | + instead of BaseModel directly. |
| 37 | +
|
| 38 | + The strip_string_values validator runs in mode="before", which means |
| 39 | + it executes before any field validators in child classes. This ensures |
| 40 | + all string inputs are cleaned of leading/trailing whitespace before |
| 41 | + any further processing. |
| 42 | + """ |
| 43 | + |
30 | 44 | model_config = {"extra": "forbid", "exclude_none": True} |
31 | 45 |
|
| 46 | + @model_validator(mode="before") |
| 47 | + @classmethod |
| 48 | + def strip_string_values(cls, data: Any) -> Any: |
| 49 | + """Strip whitespace from all string values in analyzer args. |
| 50 | +
|
| 51 | + This validator recursively processes: |
| 52 | + - String values: strips whitespace |
| 53 | + - Lists: strips strings in lists |
| 54 | + - Dicts: strips string values in dicts |
| 55 | + - Other types: left unchanged |
| 56 | +
|
| 57 | + Runs in mode="before" to ensure stripping happens before any |
| 58 | + field validators execute in child classes. |
| 59 | +
|
| 60 | + Args: |
| 61 | + data: The input data to validate |
| 62 | +
|
| 63 | + Returns: |
| 64 | + The data with all string values stripped |
| 65 | + """ |
| 66 | + if isinstance(data, dict): |
| 67 | + return {k: cls._strip_value(v) for k, v in data.items()} |
| 68 | + return data |
| 69 | + |
| 70 | + @classmethod |
| 71 | + def _strip_value(cls, value: Any) -> Any: |
| 72 | + """Recursively strip string values. |
| 73 | +
|
| 74 | + Args: |
| 75 | + value: The value to process |
| 76 | +
|
| 77 | + Returns: |
| 78 | + The processed value |
| 79 | + """ |
| 80 | + if isinstance(value, str): |
| 81 | + return value.strip() |
| 82 | + elif isinstance(value, list): |
| 83 | + return [cls._strip_value(item) for item in value] |
| 84 | + elif isinstance(value, dict): |
| 85 | + return {k: cls._strip_value(v) for k, v in value.items()} |
| 86 | + return value |
| 87 | + |
32 | 88 | @classmethod |
33 | 89 | def build_from_model(cls, datamodel): |
34 | 90 | """Build analyzer args instance from data model object |
|
0 commit comments