Skip to content

Commit

Permalink
remove leftovers after property_fields, keep only enough to exclude t…
Browse files Browse the repository at this point in the history
…hem in initialization
  • Loading branch information
collerek committed Feb 25, 2024
1 parent 2d79866 commit e7d1bdb
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 69 deletions.
1 change: 0 additions & 1 deletion ormar/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions ormar/models/descriptors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
BytesDescriptor,
JsonDescriptor,
PkDescriptor,
PropertyDescriptor,
PydanticDescriptor,
RelationDescriptor,
)

__all__ = [
"PydanticDescriptor",
"RelationDescriptor",
"PropertyDescriptor",
"PkDescriptor",
"JsonDescriptor",
"BytesDescriptor",
Expand Down
22 changes: 0 additions & 22 deletions ormar/models/descriptors/descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 3 additions & 12 deletions ormar/models/metaclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
from ormar.models.descriptors import (
JsonDescriptor,
PkDescriptor,
PropertyDescriptor,
PydanticDescriptor,
RelationDescriptor,
)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)

Expand Down
32 changes: 0 additions & 32 deletions ormar/models/newbasemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit e7d1bdb

Please sign in to comment.