@@ -77,7 +77,7 @@ class Reporter(Base):
77
77
favorite_article = relationship ("Article" , uselist = False )
78
78
79
79
@hybrid_property
80
- def hybrid_prop_untyped (self ):
80
+ def hybrid_prop (self ):
81
81
return self .first_name
82
82
83
83
@hybrid_property
@@ -97,48 +97,104 @@ def hybrid_prop_bool(self) -> bool:
97
97
return True
98
98
99
99
@hybrid_property
100
- def hybrid_prop_list_int (self ) -> List [int ]:
100
+ def hybrid_prop_list (self ) -> List [int ]:
101
101
return [1 , 2 , 3 ]
102
102
103
+ column_prop = column_property (
104
+ select ([func .cast (func .count (id ), Integer )]), doc = "Column property"
105
+ )
106
+
107
+ composite_prop = composite (CompositeFullName , first_name , last_name , doc = "Composite" )
108
+
109
+
110
+ class ReflectedEditor (type ):
111
+ """Same as Editor, but using reflected table."""
112
+
113
+ @classmethod
114
+ def __subclasses__ (cls ):
115
+ return []
116
+
117
+
118
+ editor_table = Table ("editors" , Base .metadata , autoload = True )
119
+
120
+ mapper (ReflectedEditor , editor_table )
121
+
122
+
123
+ ############################################
124
+ # The models below are mainly used in the
125
+ # @hybrid_property type inference scenarios
126
+ ############################################
127
+
128
+
129
+ class ShoppingCartItem (Base ):
130
+ __tablename__ = "shopping_cart_items"
131
+
132
+ id = Column (Integer (), primary_key = True )
133
+
134
+
135
+ class ShoppingCart (Base ):
136
+ __tablename__ = "shopping_carts"
137
+
138
+ id = Column (Integer (), primary_key = True )
139
+
140
+ # Standard Library types
141
+
103
142
@hybrid_property
104
- def hybrid_prop_list_date (self ) -> List [ datetime . date ] :
105
- return [ self .hybrid_prop_date , self . hybrid_prop_date , self . hybrid_prop_date ]
143
+ def hybrid_prop_str (self ) -> str :
144
+ return self .first_name
106
145
107
146
@hybrid_property
108
- def hybrid_prop_date (self ) -> datetime . date :
109
- return datetime . datetime . now (). date ()
147
+ def hybrid_prop_int (self ) -> int :
148
+ return 42
110
149
111
150
@hybrid_property
112
- def hybrid_prop_time (self ) -> datetime . time :
113
- return datetime . datetime . now (). time ()
151
+ def hybrid_prop_float (self ) -> float :
152
+ return 42.3
114
153
115
154
@hybrid_property
116
- def hybrid_prop_datetime (self ) -> datetime . datetime :
117
- return datetime . datetime . now ()
155
+ def hybrid_prop_bool (self ) -> bool :
156
+ return True
118
157
119
158
@hybrid_property
120
159
def hybrid_prop_decimal (self ) -> Decimal :
121
160
return Decimal ("3.14" )
122
161
123
162
@hybrid_property
124
- def hybrid_prop_first_article (self ) -> Article :
125
- return self . articles [ 0 ]
163
+ def hybrid_prop_date (self ) -> datetime . date :
164
+ return datetime . datetime . now (). date ()
126
165
127
- column_prop = column_property (
128
- select ([ func . cast ( func . count ( id ), Integer )]), doc = "Column property"
129
- )
166
+ @ hybrid_property
167
+ def hybrid_prop_time ( self ) -> datetime . time :
168
+ return datetime . datetime . now (). time ( )
130
169
131
- composite_prop = composite (CompositeFullName , first_name , last_name , doc = "Composite" )
170
+ @hybrid_property
171
+ def hybrid_prop_datetime (self ) -> datetime .datetime :
172
+ return datetime .datetime .now ()
132
173
174
+ # Lists and Nested Lists
133
175
134
- class ReflectedEditor (type ):
135
- """Same as Editor, but using reflected table."""
176
+ @hybrid_property
177
+ def hybrid_prop_list_int (self ) -> List [int ]:
178
+ return [1 , 2 , 3 ]
136
179
137
- @classmethod
138
- def __subclasses__ ( cls ) :
139
- return []
180
+ @hybrid_property
181
+ def hybrid_prop_list_date ( self ) -> List [ datetime . date ] :
182
+ return [self . hybrid_prop_date , self . hybrid_prop_date , self . hybrid_prop_date ]
140
183
184
+ @hybrid_property
185
+ def hybrid_prop_nested_list_int (self ) -> List [List [int ]]:
186
+ return [self .hybrid_prop_list_int , ]
141
187
142
- editor_table = Table ("editors" , Base .metadata , autoload = True )
188
+ @hybrid_property
189
+ def hybrid_prop_deeply_nested_list_int (self ) -> List [List [List [int ]]]:
190
+ return [[self .hybrid_prop_list_int , ], ]
143
191
144
- mapper (ReflectedEditor , editor_table )
192
+ # Other SQLAlchemy Instances
193
+ @hybrid_property
194
+ def hybrid_prop_first_shopping_cart_item (self ) -> ShoppingCartItem :
195
+ return ShoppingCartItem (id = 1 )
196
+
197
+ # Other SQLAlchemy Instances
198
+ @hybrid_property
199
+ def hybrid_prop_shopping_cart_item_list (self ) -> List [ShoppingCartItem ]:
200
+ return [ShoppingCartItem (id = 1 ), ShoppingCartItem (id = 2 )]
0 commit comments