Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: extra `BaseConfig` and `Extra` in dedicated `config` module * refactor: clean useless `#noqa: F401` * refactor: clean useless `#noqa: F811` * refactor: replace enum check Error with 3.10 > DeprecationWarning: accessing one member from another is not supported * refactor: avoid using `distutils` directly error with python 3.10 > DeprecationWarning: The distutils package is deprecated and slated > for removal in Python 3.12. > Use setuptools or check PEP 632 for potential alternatives * fix: `__annotations__` always exists * fix: origin of `typing.Hashable` is not `None` * ci: add run with 3.10.0b2 * docs: add 3.10 * feat: support `|` union operator properly `|` operator has origin `types.Union` (and not `typing.Union`) * fix: enum repr is different with 3.10+ * fix: error message changed a bit change from basic `__init__` to `test_hashable_required.<locals>.MyDataclass.__init__()` (with `__qualname__`) * fix: always exists and is not inherited anymore * fix: avoid calling `asyncio.get_event_loop` directly With python 3.10, calling it results in > DeprecationWarning: There is no current event loop * fix(ci): do not run 3.10 on linux for now For now it can not be compiled. Let's just skip the check on linux for now instead of tuning the CI pipeline * fix(ci): ignore DeprecationWarning raised by `mypy` on windows * docs: add change file
- Loading branch information
1 parent
a4cb4ee
commit 4a54f39
Showing
28 changed files
with
242 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add python 3.10 support |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import json | ||
from enum import Enum | ||
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Tuple, Type, Union | ||
|
||
from .typing import AnyCallable | ||
from .utils import GetterDict | ||
|
||
if TYPE_CHECKING: | ||
from typing import overload | ||
|
||
import typing_extensions | ||
|
||
from .fields import ModelField | ||
from .main import BaseModel | ||
|
||
ConfigType = Type['BaseConfig'] | ||
|
||
class SchemaExtraCallable(typing_extensions.Protocol): | ||
@overload | ||
def __call__(self, schema: Dict[str, Any]) -> None: | ||
pass | ||
|
||
@overload | ||
def __call__(self, schema: Dict[str, Any], model_class: Type[BaseModel]) -> None: | ||
pass | ||
|
||
|
||
else: | ||
SchemaExtraCallable = Callable[..., None] | ||
|
||
__all__ = 'BaseConfig', 'Extra', 'inherit_config', 'prepare_config' | ||
|
||
|
||
class Extra(str, Enum): | ||
allow = 'allow' | ||
ignore = 'ignore' | ||
forbid = 'forbid' | ||
|
||
|
||
class BaseConfig: | ||
title = None | ||
anystr_lower = False | ||
anystr_strip_whitespace = False | ||
min_anystr_length = None | ||
max_anystr_length = None | ||
validate_all = False | ||
extra = Extra.ignore | ||
allow_mutation = True | ||
frozen = False | ||
allow_population_by_field_name = False | ||
use_enum_values = False | ||
fields: Dict[str, Union[str, Dict[str, str]]] = {} | ||
validate_assignment = False | ||
error_msg_templates: Dict[str, str] = {} | ||
arbitrary_types_allowed = False | ||
orm_mode: bool = False | ||
getter_dict: Type[GetterDict] = GetterDict | ||
alias_generator: Optional[Callable[[str], str]] = None | ||
keep_untouched: Tuple[type, ...] = () | ||
schema_extra: Union[Dict[str, Any], 'SchemaExtraCallable'] = {} | ||
json_loads: Callable[[str], Any] = json.loads | ||
json_dumps: Callable[..., str] = json.dumps | ||
json_encoders: Dict[Type[Any], AnyCallable] = {} | ||
underscore_attrs_are_private: bool = False | ||
|
||
# Whether or not inherited models as fields should be reconstructed as base model | ||
copy_on_model_validation: bool = True | ||
|
||
@classmethod | ||
def get_field_info(cls, name: str) -> Dict[str, Any]: | ||
""" | ||
Get properties of FieldInfo from the `fields` property of the config class. | ||
""" | ||
|
||
fields_value = cls.fields.get(name) | ||
|
||
if isinstance(fields_value, str): | ||
field_info: Dict[str, Any] = {'alias': fields_value} | ||
elif isinstance(fields_value, dict): | ||
field_info = fields_value | ||
else: | ||
field_info = {} | ||
|
||
if 'alias' in field_info: | ||
field_info.setdefault('alias_priority', 2) | ||
|
||
if field_info.get('alias_priority', 0) <= 1 and cls.alias_generator: | ||
alias = cls.alias_generator(name) | ||
if not isinstance(alias, str): | ||
raise TypeError(f'Config.alias_generator must return str, not {alias.__class__}') | ||
field_info.update(alias=alias, alias_priority=1) | ||
return field_info | ||
|
||
@classmethod | ||
def prepare_field(cls, field: 'ModelField') -> None: | ||
""" | ||
Optional hook to check or modify fields during model creation. | ||
""" | ||
pass | ||
|
||
|
||
def inherit_config(self_config: 'ConfigType', parent_config: 'ConfigType', **namespace: Any) -> 'ConfigType': | ||
if not self_config: | ||
base_classes: Tuple['ConfigType', ...] = (parent_config,) | ||
elif self_config == parent_config: | ||
base_classes = (self_config,) | ||
else: | ||
base_classes = self_config, parent_config | ||
|
||
namespace['json_encoders'] = { | ||
**getattr(parent_config, 'json_encoders', {}), | ||
**getattr(self_config, 'json_encoders', {}), | ||
**namespace.get('json_encoders', {}), | ||
} | ||
|
||
return type('Config', base_classes, namespace) | ||
|
||
|
||
def prepare_config(config: Type[BaseConfig], cls_name: str) -> None: | ||
if not isinstance(config.extra, Extra): | ||
try: | ||
config.extra = Extra(config.extra) | ||
except ValueError: | ||
raise ValueError(f'"{cls_name}": {config.extra} is not a valid value for "extra"') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.