1- from typing import Any , Dict , List , Optional , Type , TypeVar , Union , cast , get_args
1+ from typing import (
2+ Any ,
3+ Dict ,
4+ List ,
5+ Optional ,
6+ Type ,
7+ TypeVar ,
8+ Union ,
9+ cast ,
10+ get_args ,
11+ get_origin ,
12+ )
213
314from pydantic import BaseModel
415from sqlalchemy import types
@@ -56,7 +67,10 @@ def process_bind_param(
5667 k : v .model_dump (mode = "json" ) if isinstance (v , BaseModel ) else v
5768 for k , v in value .items ()
5869 }
59- return value
70+
71+ raise TypeError (
72+ f"Unsupported type for PydanticJSONB: { type (value )} . Expected a Pydantic model, a list of Pydantic models, or a dictionary of Pydantic models."
73+ )
6074
6175 def process_result_value (
6276 self , value : Any , dialect : Any
@@ -65,10 +79,8 @@ def process_result_value(
6579 return None
6680 if isinstance (value , dict ):
6781 # If model_class is a Dict type hint, handle key-value pairs
68- if (
69- hasattr (self .model_class , "__origin__" )
70- and self .model_class .__origin__ is dict
71- ):
82+ origin = get_origin (self .model_class )
83+ if origin is dict :
7284 model_class = get_args (self .model_class )[
7385 1
7486 ] # Get the value type (the model)
@@ -77,12 +89,13 @@ def process_result_value(
7789 return self .model_class .model_validate (value ) # type: ignore
7890 if isinstance (value , list ):
7991 # If model_class is a List type hint
80- if (
81- hasattr (self .model_class , "__origin__" )
82- and self .model_class .__origin__ is list
83- ):
92+ origin = get_origin (self .model_class )
93+ if origin is list :
8494 model_class = get_args (self .model_class )[0 ]
8595 return [model_class .model_validate (v ) for v in value ]
8696 # Fallback case (though this shouldn't happen given our __init__ types)
8797 return [self .model_class .model_validate (v ) for v in value ] # type: ignore
88- return value
98+
99+ raise TypeError (
100+ f"Unsupported type for PydanticJSONB from database: { type (value )} . Expected a dictionary or list."
101+ )
0 commit comments