Skip to content

Commit 68c5594

Browse files
committed
Use less tpes of typing
1 parent aa8e17a commit 68c5594

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+341
-332
lines changed

docker-compose.yaml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
11
services:
22

3-
flask-inputfilter:
3+
env:
4+
build:
5+
context: .
6+
dockerfile: env_configs/env.Dockerfile
7+
container_name: flask-inputfilter-env
8+
volumes:
9+
- .:/app
10+
stdin_open: true
11+
tty: true
12+
13+
cython:
414
build:
515
context: .
616
dockerfile: env_configs/cython.Dockerfile
7-
container_name: flask-inputfilter
17+
container_name: flask-inputfilter-cython
18+
volumes:
19+
- .:/app
20+
stdin_open: true
21+
tty: true
22+
23+
pure:
24+
build:
25+
context: .
26+
dockerfile: env_configs/pure.Dockerfile
27+
container_name: flask-inputfilter-pure
828
volumes:
929
- .:/app
1030
ports:

docs/source/changelog.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ Changelog
33

44
All notable changes to this project will be documented in this file.
55

6+
[0.5.5] - 2025-06-30
7+
--------------------
8+
9+
Changed
10+
^^^^^^^
11+
- Updated ``InputFilter`` to increase performance.
12+
- Updated ``IsDataclassValidator`` to be more readable and maintainable.
13+
14+
615
[0.5.4] - 2025-05-24
716
--------------------
817

docs/source/guides/create_own_components.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Condition
1818
First off with conditions.
1919

2020
Their validation method is called ``check``.
21-
It expects a dict from the type ``Dict[str, Any]`` whereas the key (``str``) of the
21+
It expects a dict from the type ``dict[str, Any]`` whereas the key (``str``) of the
2222
dictionary represents the name of a field and the value (``Any``) the corresponding value.
2323
The dict represents the entirety of all fields present in the InputFilter it is called.
2424

docs/source/options/external_api.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Fields of `ExternalApiConfig`
3636
- ``str``
3737
- The HTTP method to use (e.g., ``GET``, ``POST``).
3838
* - ``params``
39-
- ``Optional[Dict[str, str]]``
39+
- ``Optional[dict[str, str]]``
4040
- Query parameters for the API, with placeholders allowed.
4141
* - ``data_key``
4242
- ``Optional[str]``

env_configs/cython.Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ COPY pyproject.toml /app
1010

1111
RUN python -m pip install .[dev]
1212

13-
COPY .. /app
14-
1513
COPY scripts /usr/local/bin
1614
RUN find /usr/local/bin -type f -name "*" -exec chmod +x {} \;
15+
16+
COPY .. /app

env_configs/pure.Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ COPY pyproject.toml /app
1010

1111
RUN python -m pip install .[dev]
1212

13-
COPY .. /app
14-
1513
COPY scripts /usr/local/bin
1614
RUN find /usr/local/bin -type f -name "*" -exec chmod +x {} \;
15+
16+
COPY .. /app

flask_inputfilter/_input_filter.pyx

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import json
88
import logging
99
import warnings
10-
from typing import Any, Dict, List, Optional, Type, TypeVar, Union
10+
from typing import Any, Optional, Type, TypeVar, Union
1111

1212
from flask import Response, g, request
1313

@@ -46,16 +46,16 @@ cdef class InputFilter:
4646

4747
def __cinit__(self) -> None:
4848
self.methods = make_default_methods()
49-
self.fields: Dict[str, FieldModel] = {}
50-
self.conditions: List[BaseCondition] = []
51-
self.global_filters: List[BaseFilter] = []
52-
self.global_validators: List[BaseValidator] = []
53-
self.data: Dict[str, Any] = {}
54-
self.validated_data: Dict[str, Any] = {}
55-
self.errors: Dict[str, str] = {}
49+
self.fields: dict[str, FieldModel] = {}
50+
self.conditions: list[BaseCondition] = []
51+
self.global_filters: list[BaseFilter] = []
52+
self.global_validators: list[BaseValidator] = []
53+
self.data: dict[str, Any] = {}
54+
self.validated_data: dict[str, Any] = {}
55+
self.errors: dict[str, str] = {}
5656
self.model_class: Optional[Type[T]] = None
5757

58-
def __init__(self, methods: Optional[List[str]] = None) -> None:
58+
def __init__(self, methods: Optional[list[str]] = None) -> None:
5959
if methods is not None:
6060
self.methods.clear()
6161
[self.methods.push_back(method.encode()) for method in methods]
@@ -111,7 +111,7 @@ cdef class InputFilter:
111111
f (Callable): The Flask route function to be decorated.
112112
113113
Returns:
114-
Callable[[Any, Any], Union[Response, tuple[Any, Dict[str, Any]]]]: The wrapped function with input validation.
114+
Callable[[Any, Any], Union[Response, tuple[Any, dict[str, Any]]]]: The wrapped function with input validation.
115115
"""
116116

117117
def wrapper(
@@ -126,7 +126,7 @@ cdef class InputFilter:
126126
**kwargs: Keyword arguments for the route function.
127127
128128
Returns:
129-
Union[Response, tuple[Any, Dict[str, Any]]]: The response from the route function or an error response.
129+
Union[Response, tuple[Any, dict[str, Any]]]: The response from the route function or an error response.
130130
"""
131131

132132
cdef InputFilter input_filter = cls()
@@ -172,7 +172,7 @@ cdef class InputFilter:
172172
return decorator
173173

174174
cpdef object validateData(
175-
self, data: Optional[Dict[str, Any]] = None
175+
self, data: Optional[dict[str, Any]] = None
176176
):
177177
warnings.warn(
178178
"validateData() is deprecated, use validate_data() instead",
@@ -182,7 +182,7 @@ cdef class InputFilter:
182182
return self.validate_data(data)
183183

184184
cpdef object validate_data(
185-
self, data: Optional[Dict[str, Any]] = None
185+
self, data: Optional[dict[str, Any]] = None
186186
):
187187
"""
188188
Validates input data against defined field rules, including applying
@@ -191,12 +191,12 @@ cdef class InputFilter:
191191
appropriately and conditions are checked after processing.
192192
193193
Args:
194-
data (Dict[str, Any]): A dictionary containing the input data to
194+
data (dict[str, Any]): A dictionary containing the input data to
195195
be validated where keys represent field names and values
196196
represent the corresponding data.
197197
198198
Returns:
199-
Union[Dict[str, Any], Type[T]]: A dictionary containing the validated data with
199+
Union[dict[str, Any], Type[T]]: A dictionary containing the validated data with
200200
any modifications, default values, or processed values as
201201
per the defined validation rules.
202202
@@ -325,27 +325,27 @@ cdef class InputFilter:
325325
is represented as an instance of the BaseCondition type.
326326
327327
Returns:
328-
List[BaseCondition]: A list containing all currently registered
328+
list[BaseCondition]: A list containing all currently registered
329329
instances of BaseCondition.
330330
"""
331331
return self.conditions
332332

333-
cpdef void setData(self, data: Dict[str, Any]):
333+
cpdef void setData(self, data: dict[str, Any]):
334334
warnings.warn(
335335
"setData() is deprecated, use set_data() instead",
336336
DeprecationWarning,
337337
stacklevel=2
338338
)
339339
self.set_data(data)
340340

341-
cpdef void set_data(self, data: Dict[str, Any]):
341+
cpdef void set_data(self, data: dict[str, Any]):
342342
"""
343343
Filters and sets the provided data into the object's internal
344344
storage, ensuring that only the specified fields are considered and
345345
their values are processed through defined filters.
346346
347347
Parameters:
348-
data (Dict[str, Any]):
348+
data (dict[str, Any]):
349349
The input dictionary containing key-value pairs where keys
350350
represent field names and values represent the associated
351351
data to be filtered and stored.
@@ -405,7 +405,7 @@ cdef class InputFilter:
405405
can be of various types depending on the object's design.
406406
407407
Returns:
408-
Dict[str, Any]: A dictionary containing string keys and their
408+
dict[str, Any]: A dictionary containing string keys and their
409409
corresponding values of any data type.
410410
"""
411411
return self.validated_data
@@ -455,7 +455,7 @@ cdef class InputFilter:
455455
the identifiers, and the values are of any data type.
456456
457457
Returns:
458-
Dict[str, Any]: A dictionary containing the raw values retrieved.
458+
dict[str, Any]: A dictionary containing the raw values retrieved.
459459
The keys are strings representing the identifiers, and the
460460
values can be of any type, depending on the source
461461
being accessed.
@@ -488,28 +488,28 @@ cdef class InputFilter:
488488
is required, as it does not perform any validations or checks.
489489
490490
Returns:
491-
Dict[str, Any]: The unfiltered, raw data retrieved from the
491+
dict[str, Any]: The unfiltered, raw data retrieved from the
492492
data source. The return type may vary based on the
493493
specific implementation of the data source.
494494
"""
495495
return self.data
496496

497-
cpdef void setUnfilteredData(self, data: Dict[str, Any]):
497+
cpdef void setUnfilteredData(self, data: dict[str, Any]):
498498
warnings.warn(
499499
"setUnfilteredData() is deprecated, use set_unfiltered_data() instead",
500500
DeprecationWarning,
501501
stacklevel=2
502502
)
503503
self.set_unfiltered_data(data)
504504

505-
cpdef void set_unfiltered_data(self, data: Dict[str, Any]):
505+
cpdef void set_unfiltered_data(self, data: dict[str, Any]):
506506
"""
507507
Sets unfiltered data for the current instance. This method assigns a
508508
given dictionary of data to the instance for further processing. It
509509
updates the internal state using the provided data.
510510
511511
Parameters:
512-
data (Dict[str, Any]): A dictionary containing the unfiltered
512+
data (dict[str, Any]): A dictionary containing the unfiltered
513513
data to be associated with the instance.
514514
"""
515515
self.data = data
@@ -585,7 +585,7 @@ cdef class InputFilter:
585585
respective error messages.
586586
587587
Returns:
588-
Dict[str, str]: A dictionary containing field names as keys and
588+
dict[str, str]: A dictionary containing field names as keys and
589589
their corresponding error messages as values.
590590
"""
591591
return self.errors
@@ -596,9 +596,9 @@ cdef class InputFilter:
596596
required: bool = False,
597597
default: Any = None,
598598
fallback: Any = None,
599-
filters: Optional[List[BaseFilter]] = None,
600-
validators: Optional[List[BaseValidator]] = None,
601-
steps: Optional[List[Union[BaseFilter, BaseValidator]]] = None,
599+
filters: Optional[list[BaseFilter]] = None,
600+
validators: Optional[list[BaseValidator]] = None,
601+
steps: Optional[list[Union[BaseFilter, BaseValidator]]] = None,
602602
external_api: Optional[ExternalApiConfig] = None,
603603
copy: Optional[str] = None,
604604
) except *:
@@ -615,13 +615,13 @@ cdef class InputFilter:
615615
fallback (Optional[Any]): The fallback value of the field, if
616616
validations fails or field None, although it is required.
617617
618-
filters (Optional[List[BaseFilter]]): The filters to apply to
618+
filters (Optional[list[BaseFilter]]): The filters to apply to
619619
the field value.
620620
621-
validators (Optional[List[BaseValidator]]): The validators to
621+
validators (Optional[list[BaseValidator]]): The validators to
622622
apply to the field value.
623623
624-
steps (Optional[List[Union[BaseFilter, BaseValidator]]]): Allows
624+
steps (Optional[list[Union[BaseFilter, BaseValidator]]]): Allows
625625
to apply multiple filters and validators in a specific order.
626626
627627
external_api (Optional[ExternalApiConfig]): Configuration for an
@@ -699,7 +699,7 @@ cdef class InputFilter:
699699
Retrieve the dictionary of input fields associated with the object.
700700
701701
Returns:
702-
Dict[str, FieldModel]: Dictionary containing field names as
702+
dict[str, FieldModel]: Dictionary containing field names as
703703
keys and their corresponding FieldModel instances as values
704704
"""
705705
return self.fields
@@ -740,9 +740,9 @@ cdef class InputFilter:
740740
required: bool = False,
741741
default: Any = None,
742742
fallback: Any = None,
743-
filters: Optional[List[BaseFilter]] = None,
744-
validators: Optional[List[BaseValidator]] = None,
745-
steps: Optional[List[Union[BaseFilter, BaseValidator]]] = None,
743+
filters: Optional[list[BaseFilter]] = None,
744+
validators: Optional[list[BaseValidator]] = None,
745+
steps: Optional[list[Union[BaseFilter, BaseValidator]]] = None,
746746
external_api: Optional[ExternalApiConfig] = None,
747747
copy: Optional[str] = None,
748748
):
@@ -759,13 +759,13 @@ cdef class InputFilter:
759759
fallback (Optional[Any]): The fallback value of the field, if
760760
validations fails or field None, although it is required.
761761
762-
filters (Optional[List[BaseFilter]]): The filters to apply to
762+
filters (Optional[list[BaseFilter]]): The filters to apply to
763763
the field value.
764764
765-
validators (Optional[List[BaseValidator]]): The validators to
765+
validators (Optional[list[BaseValidator]]): The validators to
766766
apply to the field value.
767767
768-
steps (Optional[List[Union[BaseFilter, BaseValidator]]]): Allows
768+
steps (Optional[list[Union[BaseFilter, BaseValidator]]]): Allows
769769
to apply multiple filters and validators in a specific order.
770770
771771
external_api (Optional[ExternalApiConfig]): Configuration for an
@@ -819,7 +819,7 @@ cdef class InputFilter:
819819
all fields during data processing.
820820
821821
Returns:
822-
List[BaseFilter]: A list of global filters.
822+
list[BaseFilter]: A list of global filters.
823823
"""
824824
return self.global_filters
825825

@@ -907,7 +907,7 @@ cdef class InputFilter:
907907
raw validated data.
908908
909909
Returns:
910-
Union[Dict[str, Any], T]: The serialized data.
910+
Union[dict[str, Any], T]: The serialized data.
911911
"""
912912
if self.model_class is None:
913913
return self.validated_data
@@ -949,6 +949,6 @@ cdef class InputFilter:
949949
to all fields during validation.
950950
951951
Returns:
952-
List[BaseValidator]: A list of global validators.
952+
list[BaseValidator]: A list of global validators.
953953
"""
954954
return self.global_validators

flask_inputfilter/conditions/array_length_equal_condition.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import Any, Dict
3+
from typing import Any
44

55
from flask_inputfilter.conditions import BaseCondition
66

@@ -54,7 +54,7 @@ def __init__(
5454
self.first_array_field = first_array_field
5555
self.second_array_field = second_array_field
5656

57-
def check(self, data: Dict[str, Any]) -> bool:
57+
def check(self, data: dict[str, Any]) -> bool:
5858
return len(data.get(self.first_array_field) or []) == len(
5959
data.get(self.second_array_field) or []
6060
)

flask_inputfilter/conditions/array_longer_than_condition.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import Any, Dict
3+
from typing import Any
44

55
from flask_inputfilter.conditions import BaseCondition
66

@@ -51,7 +51,7 @@ def __init__(self, longer_field: str, shorter_field: str) -> None:
5151
self.longer_field = longer_field
5252
self.shorter_field = shorter_field
5353

54-
def check(self, data: Dict[str, Any]) -> bool:
54+
def check(self, data: dict[str, Any]) -> bool:
5555
return len(data.get(self.longer_field) or []) > len(
5656
data.get(self.shorter_field) or []
5757
)

0 commit comments

Comments
 (0)