-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MyPy Fixes for
Worker
, Workflow
metaclass, Context
, and some ot…
…hers (#273) * fix: lint * feat: fixing mypy for the worker class * cleanup: print statements, unused imports * feat: exclude examples explicitly * chore: version * MyPy, Part II or III: `Workflow` metaclass (#274) * feat: fix mypy for Workflow * fix: type stub * fix: remove any in a couple places * fix: attempting using `Union` * fix: try with `.Value` * fix: name attr * fix: remove more type: ignore * MyPy for Context, etc. (#276) * fix: types for config, etc. * fix: queue type hint * fix: typeddict items * fix: install all the deps * fix: backoff factors * Feat: First Pass at Pydantic (#275) * fix: types for config, etc. * fix: typeddict items * feat: initial pass at pydantic support * fix: types * fix: test * fix: more types * debug: adding prints, fixing validator not present * fix: function sigs * fix: typed dict key * fix: comment * feat: clean up validators * cleanup: import * cleanup: type hint improvements * feat: add simple example * feat: type casts * fix: tests and example * fix: worker fixture * fix: deprecation warning for overrides * feat: try out using a validator registry, part I * feat: validator registry, part ii * feat: clean up pydantic example * fix: tests and lint * fix: one more type cast * cleanup: cruft and print statements * fix: worker input val and validator key * feat: expand tests a little * fix: more typing * feat: documentation updates for Pydantic * cleanup: cruft
- Loading branch information
1 parent
e504fda
commit 960846a
Showing
24 changed files
with
578 additions
and
245 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import pytest | ||
|
||
from hatchet_sdk import Hatchet | ||
|
||
|
||
# requires scope module or higher for shared event loop | ||
@pytest.mark.asyncio(scope="session") | ||
@pytest.mark.parametrize("worker", ["pydantic"], indirect=True) | ||
async def test_run_validation_error(hatchet: Hatchet, worker): | ||
run = hatchet.admin.run_workflow( | ||
"Parent", | ||
{}, | ||
) | ||
|
||
with pytest.raises(Exception, match="1 validation error for ParentInput"): | ||
await run.result() | ||
|
||
|
||
# requires scope module or higher for shared event loop | ||
@pytest.mark.asyncio(scope="session") | ||
@pytest.mark.parametrize("worker", ["pydantic"], indirect=True) | ||
async def test_run(hatchet: Hatchet, worker): | ||
run = hatchet.admin.run_workflow( | ||
"Parent", | ||
{"x": "foobar"}, | ||
) | ||
|
||
result = await run.result() | ||
|
||
assert len(result["spawn"]) == 3 |
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,19 @@ | ||
import asyncio | ||
|
||
from dotenv import load_dotenv | ||
|
||
from hatchet_sdk import new_client | ||
|
||
|
||
async def main(): | ||
load_dotenv() | ||
hatchet = new_client() | ||
|
||
hatchet.admin.run_workflow( | ||
"Parent", | ||
{"x": "foo bar baz"}, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
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,82 @@ | ||
from typing import cast | ||
|
||
from dotenv import load_dotenv | ||
from pydantic import BaseModel | ||
|
||
from hatchet_sdk import Context, Hatchet | ||
|
||
load_dotenv() | ||
|
||
hatchet = Hatchet(debug=True) | ||
|
||
|
||
# ❓ Pydantic | ||
# This workflow shows example usage of Pydantic within Hatchet | ||
class ParentInput(BaseModel): | ||
x: str | ||
|
||
|
||
@hatchet.workflow(input_validator=ParentInput) | ||
class Parent: | ||
@hatchet.step(timeout="5m") | ||
async def spawn(self, context: Context): | ||
## Use `typing.cast` to cast your `workflow_input` | ||
## to the type of your `input_validator` | ||
input = cast(ParentInput, context.workflow_input()) ## This is a `ParentInput` | ||
|
||
child = await context.aio.spawn_workflow( | ||
"Child", | ||
{"a": 1, "b": "10"}, | ||
) | ||
|
||
return await child.result() | ||
|
||
|
||
class ChildInput(BaseModel): | ||
a: int | ||
b: int | ||
|
||
|
||
class StepResponse(BaseModel): | ||
status: str | ||
|
||
|
||
@hatchet.workflow(input_validator=ChildInput) | ||
class Child: | ||
@hatchet.step() | ||
def process(self, context: Context) -> StepResponse: | ||
## This is an instance `ChildInput` | ||
input = cast(ChildInput, context.workflow_input()) | ||
|
||
return StepResponse(status="success") | ||
|
||
@hatchet.step(parents=["process"]) | ||
def process2(self, context: Context) -> StepResponse: | ||
## This is an instance of `StepResponse` | ||
process_output = cast(StepResponse, context.step_output("process")) | ||
|
||
return {"status": "step 2 - success"} | ||
|
||
@hatchet.step(parents=["process2"]) | ||
def process3(self, context: Context) -> StepResponse: | ||
## This is an instance of `StepResponse`, even though the | ||
## response of `process2` was a dictionary. Note that | ||
## Hatchet will attempt to parse that dictionary into | ||
## an object of type `StepResponse` | ||
process_2_output = cast(StepResponse, context.step_output("process2")) | ||
|
||
return StepResponse(status="step 3 - success") | ||
|
||
|
||
# ‼️ | ||
|
||
|
||
def main(): | ||
worker = hatchet.worker("pydantic-worker") | ||
worker.register_workflow(Parent()) | ||
worker.register_workflow(Child()) | ||
worker.start() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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
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
Oops, something went wrong.