fix some code smells

This commit is contained in:
collerek
2021-09-26 14:20:47 +02:00
parent 1da9111dbd
commit ca87e74b2e
2 changed files with 28 additions and 9 deletions

View File

@ -234,9 +234,8 @@ def get_pydantic_example_repr(type_: Any) -> Any:
""" """
if issubclass(type_, (numbers.Number, decimal.Decimal)): if issubclass(type_, (numbers.Number, decimal.Decimal)):
return 0 return 0
elif issubclass(type_, pydantic.BaseModel): if issubclass(type_, pydantic.BaseModel):
return generate_pydantic_example(pydantic_model=type_) return generate_pydantic_example(pydantic_model=type_)
else:
return "string" return "string"

View File

@ -252,12 +252,9 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
for field_name in self.extract_through_names(): for field_name in self.extract_through_names():
through_tmp_dict[field_name] = kwargs.pop(field_name, None) through_tmp_dict[field_name] = kwargs.pop(field_name, None)
if self.Meta.extra == Extra.ignore: kwargs = self._remove_extra_parameters_if_they_should_be_ignored(
kwargs = { kwargs=kwargs, model_fields=model_fields, pydantic_fields=pydantic_fields
k: v )
for k, v in kwargs.items()
if k in model_fields or k in pydantic_fields
}
try: try:
new_kwargs: Dict[str, Any] = { new_kwargs: Dict[str, Any] = {
k: self._convert_to_bytes( k: self._convert_to_bytes(
@ -283,6 +280,29 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
return new_kwargs, through_tmp_dict return new_kwargs, through_tmp_dict
def _remove_extra_parameters_if_they_should_be_ignored(
self, kwargs: Dict, model_fields: Dict, pydantic_fields: Set
) -> Dict:
"""
Removes the extra fields from kwargs if they should be ignored.
:param kwargs: passed arguments
:type kwargs: Dict
:param model_fields: dictionary of model fields
:type model_fields: Dict
:param pydantic_fields: set of pydantic fields names
:type pydantic_fields: Set
:return: dict without extra fields
:rtype: Dict
"""
if self.Meta.extra == Extra.ignore:
kwargs = {
k: v
for k, v in kwargs.items()
if k in model_fields or k in pydantic_fields
}
return kwargs
def _initialize_internal_attributes(self) -> None: def _initialize_internal_attributes(self) -> None:
""" """
Initializes internal attributes during __init__() Initializes internal attributes during __init__()