prefect.server.utilities.schemas
¶
Utilities for creating and working with Prefect REST API schemas.
SubclassMethodMixin
¶
Source code in /home/runner/work/docs/docs/prefect_source/src/prefect/server/utilities/schemas.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
subclass
classmethod
¶
Creates a subclass of this model containing only the specified fields.
See pydantic_subclass()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
a name for the subclass |
None
|
include_fields |
List[str]
|
fields to include |
None
|
exclude_fields |
List[str]
|
fields to exclude |
None
|
Returns:
Name | Type | Description |
---|---|---|
BaseModel |
Type[B]
|
a subclass of this class |
Source code in /home/runner/work/docs/docs/prefect_source/src/prefect/server/utilities/schemas.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
pydantic_subclass
¶
Creates a subclass of a Pydantic model that excludes certain fields.
Pydantic models use the fields attribute of their parent class to
determine inherited fields, so to create a subclass without fields, we
temporarily remove those fields from the parent fields and use
create_model
to dynamically generate a new subclass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base |
BaseModel
|
a Pydantic BaseModel |
required |
name |
str
|
a name for the subclass. If not provided it will have the same name as the base class. |
None
|
include_fields |
List[str]
|
a set of field names to include.
If |
None
|
exclude_fields |
List[str]
|
a list of field names to exclude.
If |
None
|
Returns:
Type | Description |
---|---|
Type[B]
|
pydantic.BaseModel: a new model subclass that contains only the specified fields. |
Example
To subclass a model with a subset of fields:
class Parent(pydantic.BaseModel):
x: int = 1
y: int = 2
Child = pydantic_subclass(Parent, 'Child', exclude_fields=['y'])
assert hasattr(Child(), 'x')
assert not hasattr(Child(), 'y')
To subclass a model with a subset of fields but include a new field:
class Child(pydantic_subclass(Parent, exclude_fields=['y'])):
z: int = 3
assert hasattr(Child(), 'x')
assert not hasattr(Child(), 'y')
assert hasattr(Child(), 'z')
Source code in /home/runner/work/docs/docs/prefect_source/src/prefect/server/utilities/schemas.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
|