From e7d1bdb6b5fcf6dc6142242f64c342f19af7bfa9 Mon Sep 17 00:00:00 2001 From: collerek Date: Sun, 25 Feb 2024 19:03:24 +0100 Subject: [PATCH] remove leftovers after property_fields, keep only enough to exclude them in initialization --- ormar/exceptions.py | 1 - ormar/models/descriptors/__init__.py | 2 -- ormar/models/descriptors/descriptors.py | 22 ----------------- ormar/models/metaclass.py | 15 +++--------- ormar/models/newbasemodel.py | 32 ------------------------- 5 files changed, 3 insertions(+), 69 deletions(-) diff --git a/ormar/exceptions.py b/ormar/exceptions.py index 47172000b..d7e56d1fc 100644 --- a/ormar/exceptions.py +++ b/ormar/exceptions.py @@ -15,7 +15,6 @@ class ModelDefinitionError(AsyncOrmException): """ Raised for errors related to the model definition itself: - * setting @property_field on method with arguments other than func(self) * defining a Field without required parameters * defining a model with more than one primary_key * defining a model without primary_key diff --git a/ormar/models/descriptors/__init__.py b/ormar/models/descriptors/__init__.py index 459e48db6..58e29e9ab 100644 --- a/ormar/models/descriptors/__init__.py +++ b/ormar/models/descriptors/__init__.py @@ -2,7 +2,6 @@ BytesDescriptor, JsonDescriptor, PkDescriptor, - PropertyDescriptor, PydanticDescriptor, RelationDescriptor, ) @@ -10,7 +9,6 @@ __all__ = [ "PydanticDescriptor", "RelationDescriptor", - "PropertyDescriptor", "PkDescriptor", "JsonDescriptor", "BytesDescriptor", diff --git a/ormar/models/descriptors/descriptors.py b/ormar/models/descriptors/descriptors.py index 33e6a69da..40e96d72e 100644 --- a/ormar/models/descriptors/descriptors.py +++ b/ormar/models/descriptors/descriptors.py @@ -112,25 +112,3 @@ def __set__(self, instance: "Model", value: Any) -> None: if not isinstance(instance.__dict__.get(self.name), list): instance.set_save_status(False) - - -class PropertyDescriptor: - """ - Property descriptor handles methods decorated with @property_field decorator. - They are read only. - """ - - def __init__(self, name: str, function: Any) -> None: - self.name = name - self.function = function - - def __get__(self, instance: "Model", owner: Type["Model"]) -> Any: - if instance is None: - return self - if instance is not None and self.function is not None: - bound = self.function.__get__(instance, instance.__class__) - return bound() if callable(bound) else bound - - def __set__(self, instance: "Model", value: Any) -> None: # pragma: no cover - # kept here so it's a data-descriptor and precedes __dict__ lookup - pass diff --git a/ormar/models/metaclass.py b/ormar/models/metaclass.py index 0d19db475..64dfbeb12 100644 --- a/ormar/models/metaclass.py +++ b/ormar/models/metaclass.py @@ -35,7 +35,6 @@ from ormar.models.descriptors import ( JsonDescriptor, PkDescriptor, - PropertyDescriptor, PydanticDescriptor, RelationDescriptor, ) @@ -92,8 +91,8 @@ def add_cached_properties(new_model: Type["Model"]) -> None: def add_property_fields(new_model: Type["Model"], attrs: Dict) -> None: # noqa: CCR001 """ - Checks class namespace for properties or functions with __property_field__. - If attribute have __property_field__ it was decorated with @property_field. + Checks class namespace for properties or functions with computed_field. + If attribute have decorator_info it was decorated with @computed_field. Functions like this are exposed in dict() (therefore also fastapi result). Names of property fields are cached for quicker access / extraction. @@ -597,7 +596,7 @@ def __new__( # type: ignore # noqa: CCR001 updates class namespace. Extracts settings and fields from parent classes. - Fetches methods decorated with @property_field decorator + Fetches methods decorated with @computed_field decorator to expose them later in dict(). Construct parent pydantic Metaclass/ Model. @@ -682,14 +681,6 @@ def __new__( # type: ignore # noqa: CCR001 ) new_model.model_rebuild(force=True) - for item in new_model.ormar_config.property_fields: - function = getattr(new_model, item) - setattr( - new_model, - item, - PropertyDescriptor(name=item, function=function), - ) - new_model.pk = PkDescriptor(name=new_model.ormar_config.pkname) remove_excluded_parent_fields(new_model) diff --git a/ormar/models/newbasemodel.py b/ormar/models/newbasemodel.py index 41d180f25..3e269b932 100644 --- a/ormar/models/newbasemodel.py +++ b/ormar/models/newbasemodel.py @@ -482,32 +482,6 @@ def set_save_status(self, status: bool) -> None: """Sets value of the save status""" object.__setattr__(self, "_orm_saved", status) - @classmethod - def get_properties( - cls, include: Union[Set, Dict, None], exclude: Union[Set, Dict, None] - ) -> Set[str]: - """ - Returns a set of names of functions/fields decorated with - @property_field decorator. - - They are added to dictionary when called directly and therefore also are - present in fastapi responses. - - :param include: fields to include - :type include: Union[Set, Dict, None] - :param exclude: fields to exclude - :type exclude: Union[Set, Dict, None] - :return: set of property fields names - :rtype: Set[str] - """ - - props = cls.ormar_config.property_fields - if include: - props = {prop for prop in props if prop in include} - if exclude: - props = {prop for prop in props if prop not in exclude} - return props - @classmethod def update_forward_refs(cls, **localns: Any) -> None: """ @@ -923,12 +897,6 @@ def model_dump( # type: ignore # noqa A003 exclude_list=exclude_list, ) - # include model properties as fields in dict - if object.__getattribute__(self, "ormar_config").property_fields: - props = self.get_properties(include=include_dict, exclude=exclude_dict) - if props: - dict_instance.update({prop: getattr(self, prop) for prop in props}) - return dict_instance @typing_extensions.deprecated(