diff --git a/ormar/fields/foreign_key.py b/ormar/fields/foreign_key.py index cfa2205ba..295bdaf93 100644 --- a/ormar/fields/foreign_key.py +++ b/ormar/fields/foreign_key.py @@ -298,8 +298,8 @@ def ForeignKey( # type: ignore # noqa CFQ002 skip_field=skip_field, ) - Field = type("ForeignKey", (ForeignKeyField, BaseField), {}) - return Field(**namespace) + field_class = type("ForeignKey", (ForeignKeyField, BaseField), {}) + return field_class(**namespace) class ForeignKeyField(BaseField): diff --git a/ormar/models/metaclass.py b/ormar/models/metaclass.py index 36a3e7488..a3ca8d00b 100644 --- a/ormar/models/metaclass.py +++ b/ormar/models/metaclass.py @@ -201,7 +201,7 @@ def get_constraint_copy( constraint: ColumnCollectionConstraint, ) -> Union[UniqueColumns, IndexColumns, CheckColumns]: """ - Copy the constraint and unpacking it's values + Copy the constraint and unpacking its values :raises ValueError: if non subclass of ColumnCollectionConstraint :param value: an instance of the ColumnCollectionConstraint class @@ -433,7 +433,7 @@ def extract_from_parents_definition( # noqa: CCR001 If the class is parsed first time annotations and field definition is parsed from the class.__dict__. - If the class is a ormar.Model it is skipped. + If the class is an ormar.Model it is skipped. :param base_class: one of the parent classes :type base_class: Model or model parent class @@ -537,7 +537,7 @@ def add_field_descriptor( :param field: model field to add descriptor for :type field: BaseField :param new_model: model with fields - :type new_model: Type["Model] + :type new_model: Type["Model"] """ if field.is_relation: setattr(new_model, name, RelationDescriptor(name=name)) diff --git a/ormar/queryset/queryset.py b/ormar/queryset/queryset.py index ae2f04039..e7d417241 100644 --- a/ormar/queryset/queryset.py +++ b/ormar/queryset/queryset.py @@ -893,8 +893,6 @@ async def first(self, *args: Any, **kwargs: Any) -> "T": """ Gets the first row from the db ordered by primary key column ascending. - :raises NoMatch: if no rows are returned - :raises MultipleMatches: if more than 1 row is returned. :param kwargs: fields names and proper value types :type kwargs: Any :return: returned model diff --git a/ormar/queryset/utils.py b/ormar/queryset/utils.py index 4ebe07c7f..31c39da32 100644 --- a/ormar/queryset/utils.py +++ b/ormar/queryset/utils.py @@ -12,6 +12,7 @@ Type, Union, ) +from functools import lru_cache if TYPE_CHECKING: # pragma no cover from ormar import Model, BaseField @@ -59,28 +60,32 @@ def translate_list_to_dict( # noqa: CCR001 :return: converted to dictionary input list :rtype: Dict """ - new_dict: Dict = dict() - for path in list_to_trans: - current_level = new_dict - parts = path.split("__") - def_val: Any = ... - if is_order: - if parts[0][0] == "-": - def_val = "desc" - parts[0] = parts[0][1:] - else: - def_val = "asc" - - for ind, part in enumerate(parts): - is_last = ind == len(parts) - 1 - if check_node_not_dict_or_not_last_node( - part=part, is_last=is_last, current_level=current_level - ): - current_level[part] = dict() - elif part not in current_level: - current_level[part] = def_val - current_level = current_level[part] - return new_dict + @lru_cache(maxsize=128) + def _translate(translate_set: str) -> Dict: + new_dict: Dict = dict() + for path in translate_set.split(":"): + current_level = new_dict + parts = path.split("__") + def_val: Any = ... + if is_order: + if parts[0][0] == "-": + def_val = "desc" + parts[0] = parts[0][1:] + else: + def_val = "asc" + + for ind, part in enumerate(parts): + is_last = ind == len(parts) - 1 + if check_node_not_dict_or_not_last_node( + part=part, is_last=is_last, current_level=current_level + ): + current_level[part] = dict() + elif part not in current_level: + current_level[part] = def_val + current_level = current_level[part] + return new_dict + + return dict() if not list_to_trans else _translate(":".join(sorted(list_to_trans))) def convert_set_to_required_dict(set_to_convert: set) -> Dict: diff --git a/ormar/signals/signal.py b/ormar/signals/signal.py index 868b494ec..dcbf85efa 100644 --- a/ormar/signals/signal.py +++ b/ormar/signals/signal.py @@ -99,7 +99,7 @@ async def send(self, sender: Type["Model"], **kwargs: Any) -> None: class SignalEmitter(dict): """ Emitter that registers the signals in internal dictionary. - If signal with given name does not exist it's auto added on access. + If signal with given name does not exist its auto added on access. """ def __getattr__(self, item: str) -> Signal: