|
22 | 22 | TypeVar,
|
23 | 23 | Union,
|
24 | 24 | cast,
|
| 25 | + overload, |
25 | 26 | )
|
26 | 27 |
|
27 | 28 | from pydantic import BaseConfig, BaseModel
|
@@ -87,6 +88,28 @@ def __init__(self, default: Any = Undefined, **kwargs: Any) -> None:
|
87 | 88 | "Passing sa_column_kwargs is not supported when "
|
88 | 89 | "also passing a sa_column"
|
89 | 90 | )
|
| 91 | + if primary_key is not Undefined: |
| 92 | + raise RuntimeError( |
| 93 | + "Passing primary_key is not supported when " |
| 94 | + "also passing a sa_column" |
| 95 | + ) |
| 96 | + if nullable is not Undefined: |
| 97 | + raise RuntimeError( |
| 98 | + "Passing nullable is not supported when " "also passing a sa_column" |
| 99 | + ) |
| 100 | + if foreign_key is not Undefined: |
| 101 | + raise RuntimeError( |
| 102 | + "Passing foreign_key is not supported when " |
| 103 | + "also passing a sa_column" |
| 104 | + ) |
| 105 | + if unique is not Undefined: |
| 106 | + raise RuntimeError( |
| 107 | + "Passing unique is not supported when " "also passing a sa_column" |
| 108 | + ) |
| 109 | + if index is not Undefined: |
| 110 | + raise RuntimeError( |
| 111 | + "Passing index is not supported when " "also passing a sa_column" |
| 112 | + ) |
90 | 113 | super().__init__(default=default, **kwargs)
|
91 | 114 | self.primary_key = primary_key
|
92 | 115 | self.nullable = nullable
|
@@ -126,6 +149,86 @@ def __init__(
|
126 | 149 | self.sa_relationship_kwargs = sa_relationship_kwargs
|
127 | 150 |
|
128 | 151 |
|
| 152 | +@overload |
| 153 | +def Field( |
| 154 | + default: Any = Undefined, |
| 155 | + *, |
| 156 | + default_factory: Optional[NoArgAnyCallable] = None, |
| 157 | + alias: Optional[str] = None, |
| 158 | + title: Optional[str] = None, |
| 159 | + description: Optional[str] = None, |
| 160 | + exclude: Union[ |
| 161 | + AbstractSet[Union[int, str]], Mapping[Union[int, str], Any], Any |
| 162 | + ] = None, |
| 163 | + include: Union[ |
| 164 | + AbstractSet[Union[int, str]], Mapping[Union[int, str], Any], Any |
| 165 | + ] = None, |
| 166 | + const: Optional[bool] = None, |
| 167 | + gt: Optional[float] = None, |
| 168 | + ge: Optional[float] = None, |
| 169 | + lt: Optional[float] = None, |
| 170 | + le: Optional[float] = None, |
| 171 | + multiple_of: Optional[float] = None, |
| 172 | + max_digits: Optional[int] = None, |
| 173 | + decimal_places: Optional[int] = None, |
| 174 | + min_items: Optional[int] = None, |
| 175 | + max_items: Optional[int] = None, |
| 176 | + unique_items: Optional[bool] = None, |
| 177 | + min_length: Optional[int] = None, |
| 178 | + max_length: Optional[int] = None, |
| 179 | + allow_mutation: bool = True, |
| 180 | + regex: Optional[str] = None, |
| 181 | + discriminator: Optional[str] = None, |
| 182 | + repr: bool = True, |
| 183 | + primary_key: Union[bool, UndefinedType] = Undefined, |
| 184 | + foreign_key: Any = Undefined, |
| 185 | + unique: Union[bool, UndefinedType] = Undefined, |
| 186 | + nullable: Union[bool, UndefinedType] = Undefined, |
| 187 | + index: Union[bool, UndefinedType] = Undefined, |
| 188 | + sa_column_args: Union[Sequence[Any], UndefinedType] = Undefined, |
| 189 | + sa_column_kwargs: Union[Mapping[str, Any], UndefinedType] = Undefined, |
| 190 | + schema_extra: Optional[Dict[str, Any]] = None, |
| 191 | +) -> Any: |
| 192 | + ... |
| 193 | + |
| 194 | + |
| 195 | +@overload |
| 196 | +def Field( |
| 197 | + default: Any = Undefined, |
| 198 | + *, |
| 199 | + default_factory: Optional[NoArgAnyCallable] = None, |
| 200 | + alias: Optional[str] = None, |
| 201 | + title: Optional[str] = None, |
| 202 | + description: Optional[str] = None, |
| 203 | + exclude: Union[ |
| 204 | + AbstractSet[Union[int, str]], Mapping[Union[int, str], Any], Any |
| 205 | + ] = None, |
| 206 | + include: Union[ |
| 207 | + AbstractSet[Union[int, str]], Mapping[Union[int, str], Any], Any |
| 208 | + ] = None, |
| 209 | + const: Optional[bool] = None, |
| 210 | + gt: Optional[float] = None, |
| 211 | + ge: Optional[float] = None, |
| 212 | + lt: Optional[float] = None, |
| 213 | + le: Optional[float] = None, |
| 214 | + multiple_of: Optional[float] = None, |
| 215 | + max_digits: Optional[int] = None, |
| 216 | + decimal_places: Optional[int] = None, |
| 217 | + min_items: Optional[int] = None, |
| 218 | + max_items: Optional[int] = None, |
| 219 | + unique_items: Optional[bool] = None, |
| 220 | + min_length: Optional[int] = None, |
| 221 | + max_length: Optional[int] = None, |
| 222 | + allow_mutation: bool = True, |
| 223 | + regex: Optional[str] = None, |
| 224 | + discriminator: Optional[str] = None, |
| 225 | + repr: bool = True, |
| 226 | + sa_column: Union[Column, UndefinedType] = Undefined, # type: ignore |
| 227 | + schema_extra: Optional[Dict[str, Any]] = None, |
| 228 | +) -> Any: |
| 229 | + ... |
| 230 | + |
| 231 | + |
129 | 232 | def Field(
|
130 | 233 | default: Any = Undefined,
|
131 | 234 | *,
|
@@ -156,9 +259,9 @@ def Field(
|
156 | 259 | regex: Optional[str] = None,
|
157 | 260 | discriminator: Optional[str] = None,
|
158 | 261 | repr: bool = True,
|
159 |
| - primary_key: bool = False, |
160 |
| - foreign_key: Optional[Any] = None, |
161 |
| - unique: bool = False, |
| 262 | + primary_key: Union[bool, UndefinedType] = Undefined, |
| 263 | + foreign_key: Any = Undefined, |
| 264 | + unique: Union[bool, UndefinedType] = Undefined, |
162 | 265 | nullable: Union[bool, UndefinedType] = Undefined,
|
163 | 266 | index: Union[bool, UndefinedType] = Undefined,
|
164 | 267 | sa_column: Union[Column, UndefinedType] = Undefined, # type: ignore
|
@@ -206,6 +309,27 @@ def Field(
|
206 | 309 | return field_info
|
207 | 310 |
|
208 | 311 |
|
| 312 | +@overload |
| 313 | +def Relationship( |
| 314 | + *, |
| 315 | + back_populates: Optional[str] = None, |
| 316 | + link_model: Optional[Any] = None, |
| 317 | + sa_relationship_args: Optional[Sequence[Any]] = None, |
| 318 | + sa_relationship_kwargs: Optional[Mapping[str, Any]] = None, |
| 319 | +) -> Any: |
| 320 | + ... |
| 321 | + |
| 322 | + |
| 323 | +@overload |
| 324 | +def Relationship( |
| 325 | + *, |
| 326 | + back_populates: Optional[str] = None, |
| 327 | + link_model: Optional[Any] = None, |
| 328 | + sa_relationship: Optional[RelationshipProperty] = None, # type: ignore |
| 329 | +) -> Any: |
| 330 | + ... |
| 331 | + |
| 332 | + |
209 | 333 | def Relationship(
|
210 | 334 | *,
|
211 | 335 | back_populates: Optional[str] = None,
|
@@ -440,21 +564,28 @@ def get_column_from_field(field: ModelField) -> Column: # type: ignore
|
440 | 564 | if isinstance(sa_column, Column):
|
441 | 565 | return sa_column
|
442 | 566 | sa_type = get_sqlalchemy_type(field)
|
443 |
| - primary_key = getattr(field.field_info, "primary_key", False) |
| 567 | + primary_key = getattr(field.field_info, "primary_key", Undefined) |
| 568 | + if primary_key is Undefined: |
| 569 | + primary_key = False |
444 | 570 | index = getattr(field.field_info, "index", Undefined)
|
445 | 571 | if index is Undefined:
|
446 | 572 | index = False
|
447 | 573 | nullable = not primary_key and _is_field_noneable(field)
|
448 | 574 | # Override derived nullability if the nullable property is set explicitly
|
449 | 575 | # on the field
|
450 |
| - if hasattr(field.field_info, "nullable"): |
451 |
| - field_nullable = getattr(field.field_info, "nullable") # noqa: B009 |
452 |
| - if field_nullable != Undefined: |
453 |
| - nullable = field_nullable |
| 576 | + field_nullable = getattr(field.field_info, "nullable", Undefined) # noqa: B009 |
| 577 | + if field_nullable != Undefined: |
| 578 | + assert not isinstance(field_nullable, UndefinedType) |
| 579 | + nullable = field_nullable |
454 | 580 | args = []
|
455 |
| - foreign_key = getattr(field.field_info, "foreign_key", None) |
456 |
| - unique = getattr(field.field_info, "unique", False) |
| 581 | + foreign_key = getattr(field.field_info, "foreign_key", Undefined) |
| 582 | + if foreign_key is Undefined: |
| 583 | + foreign_key = None |
| 584 | + unique = getattr(field.field_info, "unique", Undefined) |
| 585 | + if unique is Undefined: |
| 586 | + unique = False |
457 | 587 | if foreign_key:
|
| 588 | + assert isinstance(foreign_key, str) |
458 | 589 | args.append(ForeignKey(foreign_key))
|
459 | 590 | kwargs = {
|
460 | 591 | "primary_key": primary_key,
|
|
0 commit comments