Skip to content

Commit 56914af

Browse files
authored
refactor(validation): add from __future__ import annotations (#4984)
* refactor(validation): add from __future__ import annotations and update code according to ruff rules TCH, UP006, UP007, UP037 and FA100. * Prefer absolute imports
1 parent 199f33e commit 56914af

File tree

3 files changed

+47
-43
lines changed

3 files changed

+47
-43
lines changed

aws_lambda_powertools/utilities/validation/base.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1+
from __future__ import annotations
2+
13
import logging
2-
from typing import Dict, Optional, Union
34

45
import fastjsonschema # type: ignore
56

6-
from .exceptions import InvalidSchemaFormatError, SchemaValidationError
7+
from aws_lambda_powertools.utilities.validation.exceptions import InvalidSchemaFormatError, SchemaValidationError
78

89
logger = logging.getLogger(__name__)
910

1011

11-
def validate_data_against_schema(data: Union[Dict, str], schema: Dict, formats: Optional[Dict] = None):
12+
def validate_data_against_schema(data: dict | str, schema: dict, formats: dict | None = None):
1213
"""Validate dict data against given JSON Schema
1314
1415
Parameters
1516
----------
16-
data : Dict
17+
data : dict
1718
Data set to be validated
18-
schema : Dict
19+
schema : dict
1920
JSON Schema to validate against
20-
formats: Dict
21+
formats: dict
2122
Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool
2223
2324
Raises

aws_lambda_powertools/utilities/validation/exceptions.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
from typing import Any, List, Optional
1+
from __future__ import annotations
22

3-
from ...exceptions import InvalidEnvelopeExpressionError
3+
from typing import Any
4+
5+
from aws_lambda_powertools.exceptions import InvalidEnvelopeExpressionError
46

57

68
class SchemaValidationError(Exception):
79
"""When serialization fail schema validation"""
810

911
def __init__(
1012
self,
11-
message: Optional[str] = None,
12-
validation_message: Optional[str] = None,
13-
name: Optional[str] = None,
14-
path: Optional[List] = None,
15-
value: Optional[Any] = None,
16-
definition: Optional[Any] = None,
17-
rule: Optional[str] = None,
18-
rule_definition: Optional[Any] = None,
13+
message: str | None = None,
14+
validation_message: str | None = None,
15+
name: str | None = None,
16+
path: list | None = None,
17+
value: Any | None = None,
18+
definition: Any | None = None,
19+
rule: str | None = None,
20+
rule_definition: Any | None = None,
1921
):
2022
"""
2123
@@ -29,7 +31,7 @@ def __init__(
2931
name : str, optional
3032
name of a path in the data structure
3133
(e.g. `data.property[index]`)
32-
path: List, optional
34+
path: list, optional
3335
`path` as an array in the data structure
3436
(e.g. `['data', 'property', 'index']`),
3537
value : Any, optional

aws_lambda_powertools/utilities/validation/validator.py

+27-26
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1+
from __future__ import annotations
2+
13
import logging
2-
from typing import Any, Callable, Dict, Optional, Union
4+
from typing import Any, Callable
35

6+
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
47
from aws_lambda_powertools.utilities import jmespath_utils
5-
6-
from ...middleware_factory import lambda_handler_decorator
7-
from .base import validate_data_against_schema
8+
from aws_lambda_powertools.utilities.validation.base import validate_data_against_schema
89

910
logger = logging.getLogger(__name__)
1011

1112

1213
@lambda_handler_decorator
1314
def validator(
1415
handler: Callable,
15-
event: Union[Dict, str],
16+
event: dict | str,
1617
context: Any,
17-
inbound_schema: Optional[Dict] = None,
18-
inbound_formats: Optional[Dict] = None,
19-
outbound_schema: Optional[Dict] = None,
20-
outbound_formats: Optional[Dict] = None,
18+
inbound_schema: dict | None = None,
19+
inbound_formats: dict | None = None,
20+
outbound_schema: dict | None = None,
21+
outbound_formats: dict | None = None,
2122
envelope: str = "",
22-
jmespath_options: Optional[Dict] = None,
23+
jmespath_options: dict | None = None,
2324
**kwargs: Any,
2425
) -> Any:
2526
"""Lambda handler decorator to validate incoming/outbound data using a JSON Schema
@@ -28,21 +29,21 @@ def validator(
2829
----------
2930
handler : Callable
3031
Method to annotate on
31-
event : Dict
32+
event : dict
3233
Lambda event to be validated
3334
context : Any
3435
Lambda context object
35-
inbound_schema : Dict
36+
inbound_schema : dict
3637
JSON Schema to validate incoming event
37-
outbound_schema : Dict
38+
outbound_schema : dict
3839
JSON Schema to validate outbound event
39-
envelope : Dict
40+
envelope : dict
4041
JMESPath expression to filter data against
41-
jmespath_options : Dict
42+
jmespath_options : dict
4243
Alternative JMESPath options to be included when filtering expr
43-
inbound_formats: Dict
44+
inbound_formats: dict
4445
Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool
45-
outbound_formats: Dict
46+
outbound_formats: dict
4647
Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool
4748
4849
Example
@@ -140,26 +141,26 @@ def handler(event, context):
140141

141142
def validate(
142143
event: Any,
143-
schema: Dict,
144-
formats: Optional[Dict] = None,
145-
envelope: Optional[str] = None,
146-
jmespath_options: Optional[Dict] = None,
144+
schema: dict,
145+
formats: dict | None = None,
146+
envelope: str | None = None,
147+
jmespath_options: dict | None = None,
147148
):
148149
"""Standalone function to validate event data using a JSON Schema
149150
150151
Typically used when you need more control over the validation process.
151152
152153
Parameters
153154
----------
154-
event : Dict
155+
event : dict
155156
Lambda event to be validated
156-
schema : Dict
157+
schema : dict
157158
JSON Schema to validate incoming event
158-
envelope : Dict
159+
envelope : dict
159160
JMESPath expression to filter data against
160-
jmespath_options : Dict
161+
jmespath_options : dict
161162
Alternative JMESPath options to be included when filtering expr
162-
formats: Dict
163+
formats: dict
163164
Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool
164165
165166
Example

0 commit comments

Comments
 (0)