|
10 | 10 | # ************************************************************* |
11 | 11 |
|
12 | 12 | ### Standard packages ### |
13 | | -from typing import Any, ClassVar, Callable, Literal, Optional, Sequence, Set, Tuple, Union, cast |
| 13 | +from typing import Any, ClassVar, Callable, Literal, Optional, Sequence, Set, Union |
14 | 14 |
|
15 | 15 | ### Third-party packages ### |
16 | | -from pydantic import ValidationError |
17 | 16 | from pydantic_settings import BaseSettings |
18 | 17 |
|
19 | 18 | ### Local modules ### |
@@ -42,29 +41,25 @@ class CsrfConfig(object): |
42 | 41 |
|
43 | 42 | @classmethod |
44 | 43 | def load_config( |
45 | | - cls, settings: Callable[..., Union[Sequence[Tuple[str, Any]], BaseSettings]] |
| 44 | + cls, settings: Callable[..., Union[Sequence[tuple[str, Any]], BaseSettings]] |
46 | 45 | ) -> None: |
47 | | - try: |
48 | | - config = LoadConfig(**{key.lower(): value for key, value in settings()}) |
49 | | - cls._cookie_key = config.cookie_key or cls._cookie_key |
50 | | - cls._cookie_path = config.cookie_path or cls._cookie_path |
51 | | - cls._cookie_domain = config.cookie_domain |
52 | | - if config.cookie_samesite in {"lax", "none", "strict"}: |
53 | | - cls._cookie_samesite = cast(Literal["lax", "none", "strict"], config.cookie_samesite) |
54 | | - cls._cookie_secure = False if config.cookie_secure is None else config.cookie_secure |
55 | | - cls._header_name = config.header_name or cls._header_name |
56 | | - cls._header_type = config.header_type |
57 | | - cls._httponly = True if config.httponly is None else config.httponly |
58 | | - cls._max_age = config.max_age or cls._max_age |
59 | | - cls._methods = config.methods or cls._methods |
60 | | - cls._secret_key = config.secret_key |
61 | | - cls._token_location = config.token_location or cls._token_location |
62 | | - cls._token_key = config.token_key or cls._token_key |
63 | | - except ValidationError: |
64 | | - raise |
65 | | - except Exception as err: |
66 | | - print(err) |
67 | | - raise TypeError('CsrfConfig must be pydantic "BaseSettings" or list of tuple') |
| 46 | + """ |
| 47 | + :raises pydantic_core.ValidationError: in case of settings' attribute type mismatched |
| 48 | + """ |
| 49 | + config = LoadConfig(**{key.lower(): value for key, value in settings()}) |
| 50 | + cls._cookie_key = config.cookie_key or cls._cookie_key |
| 51 | + cls._cookie_path = config.cookie_path or cls._cookie_path |
| 52 | + cls._cookie_domain = config.cookie_domain |
| 53 | + cls._cookie_samesite = config.cookie_samesite |
| 54 | + cls._cookie_secure = False if config.cookie_secure is None else config.cookie_secure |
| 55 | + cls._header_name = config.header_name or cls._header_name |
| 56 | + cls._header_type = config.header_type |
| 57 | + cls._httponly = True if config.httponly is None else config.httponly |
| 58 | + cls._max_age = config.max_age or cls._max_age |
| 59 | + cls._methods = config.methods or cls._methods |
| 60 | + cls._secret_key = config.secret_key |
| 61 | + cls._token_location = config.token_location or cls._token_location |
| 62 | + cls._token_key = config.token_key or cls._token_key |
68 | 63 |
|
69 | 64 |
|
70 | | -__all__: Tuple[str, ...] = ("CsrfConfig",) |
| 65 | +__all__: tuple[str, ...] = ("CsrfConfig",) |
0 commit comments