File tree Expand file tree Collapse file tree 2 files changed +13
-9
lines changed Expand file tree Collapse file tree 2 files changed +13
-9
lines changed Original file line number Diff line number Diff line change 10
10
Dict ,
11
11
ForwardRef ,
12
12
Generator ,
13
+ Literal ,
13
14
Mapping ,
14
15
Optional ,
15
16
Set ,
22
23
from pydantic import BaseModel
23
24
from pydantic .fields import FieldInfo
24
25
from typing_extensions import Annotated , get_args , get_origin
26
+ from .sql .sqltypes import AutoString
25
27
26
28
# Reassign variable to make it reexported for mypy
27
29
PYDANTIC_VERSION = P_VERSION
@@ -458,10 +460,12 @@ def is_field_noneable(field: "FieldInfo") -> bool:
458
460
)
459
461
return field .allow_none # type: ignore[no-any-return, attr-defined]
460
462
461
- def get_sa_type_from_field (field : Any ) -> Any :
462
- if isinstance (field .type_ , type ) and field .shape == SHAPE_SINGLETON :
463
- return field .type_
464
- raise ValueError (f"The field { field .name } has no matching SQLAlchemy type" )
463
+ # def get_sa_type_from_field(field: Any) -> Any:
464
+ # if field is Literal:
465
+ # return AutoString
466
+ # elif isinstance(field.type_, type) and field.shape == SHAPE_SINGLETON:
467
+ # return field.type_
468
+ # raise ValueError(f"The field {field.name} has no matching SQLAlchemy type")
465
469
466
470
def get_field_metadata (field : Any ) -> Any :
467
471
metadata = FakeMetadata ()
Original file line number Diff line number Diff line change @@ -135,15 +135,15 @@ class Hero(SQLModel, table=True):
135
135
name : str = Field (unique = True )
136
136
weakness : Literal ["Kryptonite" , "Dehydration" , "Munchies" ]
137
137
138
- superman = Hero (name = "Superman " , weakness = "Kryptonite" )
138
+ superguy = Hero (name = "Superguy " , weakness = "Kryptonite" )
139
139
140
140
engine = create_engine ("sqlite://" , echo = True )
141
141
142
142
SQLModel .metadata .create_all (engine )
143
143
144
144
with Session (engine ) as session :
145
- session .add (superman )
145
+ session .add (superguy )
146
146
session .commit ()
147
- session .refresh (superman )
148
- assert superman .weakness == "Kryptonite"
149
- assert isinstance (superman .weakness , str )
147
+ session .refresh (superguy )
148
+ assert superguy .weakness == "Kryptonite"
149
+ assert isinstance (superguy .weakness , str )
You can’t perform that action at this time.
0 commit comments