Skip to content

Commit

Permalink
馃帹 Fix type annotations for Python 3.7
Browse files Browse the repository at this point in the history
  • Loading branch information
tiangolo committed Dec 4, 2023
1 parent f1ab6a6 commit f68e93e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
11 changes: 5 additions & 6 deletions sqlmodel/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
Type,
TypeVar,
Union,
get_args,
get_origin,
)

from pydantic import VERSION as PYDANTIC_VERSION
from pydantic.fields import FieldInfo
from typing_extensions import get_args, get_origin

IS_PYDANTIC_V2 = PYDANTIC_VERSION.startswith("2.")

Expand Down Expand Up @@ -99,11 +98,11 @@ def get_model_fields(model: InstanceOrType["SQLModel"]) -> Dict[str, "FieldInfo"
return model.model_fields

def set_fields_set(
new_object: InstanceOrType["SQLModel"], fields: set["FieldInfo"]
new_object: InstanceOrType["SQLModel"], fields: Set["FieldInfo"]
) -> None:
object.__setattr__(new_object, "__pydantic_fields_set__", fields)

def get_annotations(class_dict: dict[str, Any]) -> dict[str, Any]:
def get_annotations(class_dict: Dict[str, Any]) -> Dict[str, Any]:
return class_dict.get("__annotations__", {})

def is_table_model_class(cls: Type[Any]) -> bool:
Expand Down Expand Up @@ -167,7 +166,7 @@ def is_field_noneable(field: "FieldInfo") -> bool:
return False

def get_type_from_field(field: Any) -> type:
type_: type | None = field.annotation
type_: Any = field.annotation
# Resolve Optional fields
if type_ is None:
raise ValueError("Missing field type")
Expand Down Expand Up @@ -240,7 +239,7 @@ def sqlmodel_table_construct(
_fields_set = set(fields_values.keys())
fields_values.update(defaults)

_extra: dict[str, Any] | None = None
_extra: Union[Dict[str, Any], None] = None
if cls.model_config.get("extra") == "allow":
_extra = {}
for k, v in values.items():
Expand Down
4 changes: 2 additions & 2 deletions sqlmodel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@

_T = TypeVar("_T")
NoArgAnyCallable = Callable[[], Any]
IncEx = Set[int] | Set[str] | Dict[int, Any] | Dict[str, Any] | None
IncEx = Union[Set[int], Set[str], Dict[int, Any], Dict[str, Any], None]


def __dataclass_transform__(
Expand Down Expand Up @@ -762,7 +762,7 @@ def model_validate(
def model_dump(
self,
*,
mode: Literal["json", "python"] | str = "python",
mode: Union[Literal["json", "python"], str] = "python",
include: IncEx = None,
exclude: IncEx = None,
by_alias: bool = False,
Expand Down

0 comments on commit f68e93e

Please sign in to comment.