-
-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix default for annotated field in pydantic v2 (#1498)
* Fix default for annotated field in pydantic v2 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Move v2 logic to v2 model --------- Co-authored-by: ferris <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Koudai Aono <[email protected]>
- Loading branch information
1 parent
5ccc441
commit a46fe94
Showing
5 changed files
with
127 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
tests/data/expected/main/main_use_annotated_with_field_constraints_pydantic_v2/output.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# generated by datamodel-codegen: | ||
# filename: api_constrained.yaml | ||
# timestamp: 2019-07-26T00:00:00+00:00 | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Annotated, List, Optional, Union | ||
|
||
from pydantic import AnyUrl, BaseModel, Field, RootModel | ||
|
||
|
||
class Pet(BaseModel): | ||
id: Annotated[int, Field(ge=0, le=9223372036854775807)] | ||
name: Annotated[str, Field(max_length=256)] | ||
tag: Annotated[Optional[str], Field(None, max_length=64)] | ||
|
||
|
||
class Pets(RootModel[List[Pet]]): | ||
root: Annotated[List[Pet], Field(max_length=10, min_length=1)] | ||
|
||
|
||
class UID(RootModel[int]): | ||
root: Annotated[int, Field(ge=0)] | ||
|
||
|
||
class Phone(RootModel[str]): | ||
root: Annotated[str, Field(min_length=3)] | ||
|
||
|
||
class FaxItem(RootModel[str]): | ||
root: Annotated[str, Field(min_length=3)] | ||
|
||
|
||
class User(BaseModel): | ||
id: Annotated[int, Field(ge=0)] | ||
name: Annotated[str, Field(max_length=256)] | ||
tag: Annotated[Optional[str], Field(None, max_length=64)] | ||
uid: UID | ||
phones: Annotated[Optional[List[Phone]], Field(None, max_length=10)] | ||
fax: Optional[List[FaxItem]] = None | ||
height: Annotated[Optional[Union[int, float]], Field(None, ge=1.0, le=300.0)] | ||
weight: Annotated[Optional[Union[float, int]], Field(None, ge=1.0, le=1000.0)] | ||
age: Annotated[Optional[int], Field(None, gt=0, le=200)] | ||
rating: Annotated[Optional[float], Field(None, gt=0.0, le=5.0)] | ||
|
||
|
||
class Users(RootModel[List[User]]): | ||
root: List[User] | ||
|
||
|
||
class Id(RootModel[str]): | ||
root: str | ||
|
||
|
||
class Rules(RootModel[List[str]]): | ||
root: List[str] | ||
|
||
|
||
class Error(BaseModel): | ||
code: int | ||
message: str | ||
|
||
|
||
class Api(BaseModel): | ||
apiKey: Annotated[ | ||
Optional[str], | ||
Field(None, description='To be used as a dataset parameter value'), | ||
] | ||
apiVersionNumber: Annotated[ | ||
Optional[str], | ||
Field(None, description='To be used as a version parameter value'), | ||
] | ||
apiUrl: Annotated[ | ||
Optional[AnyUrl], | ||
Field(None, description="The URL describing the dataset's fields"), | ||
] | ||
apiDocumentationUrl: Annotated[ | ||
Optional[AnyUrl], | ||
Field(None, description='A URL to the API console for each API'), | ||
] | ||
|
||
|
||
class Apis(RootModel[List[Api]]): | ||
root: List[Api] | ||
|
||
|
||
class Event(BaseModel): | ||
name: Optional[str] = None | ||
|
||
|
||
class Result(BaseModel): | ||
event: Optional[Event] = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters