Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PEP 593 support for Requirement annotations #1192

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions sqlmodel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@
from sqlalchemy.orm.instrumentation import is_instrumented
from sqlalchemy.sql.schema import MetaData
from sqlalchemy.sql.sqltypes import LargeBinary, Time, Uuid
from typing_extensions import Literal, TypeAlias, deprecated, get_origin
from typing_extensions import (
Annotated,
Literal,
TypeAlias,
deprecated,
get_args,
get_origin,
)

from ._compat import ( # type: ignore[attr-defined]
IS_PYDANTIC_V2,
Expand Down Expand Up @@ -475,6 +482,16 @@ def Relationship(
return relationship_info


def get_annotated_relationshipinfo(t: Any) -> Optional[RelationshipInfo]:
"""Get the first RelationshipInfo from Annotated or None if not Annotated with RelationshipInfo."""
if get_origin(t) is not Annotated:
return None
for a in get_args(t):
if isinstance(a, RelationshipInfo):
return a
return None


@__dataclass_transform__(kw_only_default=True, field_descriptors=(Field, FieldInfo))
class SQLModelMetaclass(ModelMetaclass, DeclarativeMeta):
__sqlmodel_relationships__: Dict[str, RelationshipInfo]
Expand Down Expand Up @@ -515,7 +532,12 @@ def __new__(
else:
dict_for_pydantic[k] = v
for k, v in original_annotations.items():
if k in relationships:
# check for `field: Annotated[Any, Relationship()]`
t = get_annotated_relationshipinfo(v)
if t:
relationships[k] = t
relationship_annotations[k] = get_args(v)[0]
elif k in relationships:
relationship_annotations[k] = v
else:
pydantic_annotations[k] = v
Expand Down
Loading