Kamilly Victória Ruseler
10/09/2024, 7:27 PMMarvin
10/09/2024, 7:27 PMKamilly Victória Ruseler
10/09/2024, 7:28 PMMarvin
10/09/2024, 7:28 PMBlock
. This is illustrated in the example below:
python
from typing import Optional
from prefect.blocks.core import Block
class MyConfig(Block):
my_default_field: Optional[str] = "default text"
another_field: Optional[str] = None
# Usage
config = MyConfig()
print(config.my_default_field) # Output: "default text"
In this example, the field my_default_field
is set with a default value of "default text". When you create an instance of MyConfig
and don't provide a value for my_default_field
, it defaults to this text.
You can find more details on how to define custom blocks and fields in the Prefect 2 documentation on Blocks.
Let me know if you need further assistance!Nate
10/09/2024, 7:29 PMField
from pydantic?Kamilly Victória Ruseler
10/09/2024, 7:30 PMNate
10/09/2024, 7:33 PMcan i set a default text into my dict in the Field?you can set default values for fields in 1 of 2 main ways • the
default
kwarg
class Foo(BaseModel):
d: dict = Field(default=dict(some_key=42))
• default factories (a function that returns your default)
class Foo(BaseModel):
d: dict = Field(default_factory=lambda: dict(some_key=42))
if this is not what you're trying to do, please feel free to give a small example!Kamilly Victória Ruseler
10/09/2024, 7:53 PMNate
10/09/2024, 7:57 PMOptional
tells pydantic that "`None` is a valid value for `hyperparams`"
i.e. Optional
does not exactly mean that a user can "optionally" pass the value of hyperparams
so if you just want hyperparams
to be a dict
that defaults to the return of your lambda, you would have
class Model(BaseModel):
hyperparams: Dict[str, Any] = Field(default_factory=lambda: dict(...))
Kamilly Victória Ruseler
10/10/2024, 8:00 PM