Skip to content

Commit

Permalink
types adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
benedikt-bartscher committed Mar 5, 2024
1 parent 8e692cd commit 95f9b12
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions reflex/utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
)

import sqlalchemy
from pydantic.fields import ModelField
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import DeclarativeBase, Mapped, QueryableAttribute, Relationship

Expand Down Expand Up @@ -165,13 +164,11 @@ def get_attribute_access_type(cls: GenericType, name: str) -> GenericType | None
attr = getattr(cls, name, None)
if hint := get_property_hint(attr):
return hint
if hasattr(cls, "__fields__") and name in cls.__fields__:
if hasattr(cls, "model_fields") and name in cls.model_fields:
# pydantic models
field = cls.__fields__[name]
type_ = field.outer_type_
if isinstance(type_, ModelField):
type_ = type_.type_
if not field.required and field.default is None:
field = cls.model_fields[name]
type_ = field.annotation
if not field.is_required() and field.default is None and field.default_factory is None:
# Ensure frontend uses null coalescing when accessing.
type_ = Optional[type_]
return type_
Expand Down Expand Up @@ -201,8 +198,9 @@ def get_attribute_access_type(cls: GenericType, name: str) -> GenericType | None
type_origin = get_origin(type_)
if isinstance(type_origin, type) and issubclass(type_origin, Mapped):
return get_args(type_)[0] # SQLAlchemy v2
if isinstance(type_, ModelField):
return type_.type_ # SQLAlchemy v1.4
# TODO: pydantic v2
# if isinstance(type_, ModelField):
# return type_.type_ # SQLAlchemy v1.4
return type_
elif is_union(cls):
# Check in each arg of the annotation.
Expand Down

0 comments on commit 95f9b12

Please sign in to comment.