Laxmi
11/11/2024, 8:41 AMdef my_hook(obj: Task | Flow, run: TaskRun | FlowRun, state: State) -> None:
Ans has the imports like following:
from prefect import Task
from prefect.context import TaskRun
from prefect.states import State
But mypy throws errors on imports from prefect.context
and suggests to use prefect.client.schemas.objects
instead. Which one should we follow - the docs or mypy?Marvin
11/11/2024, 8:42 AMTaskRun
and FlowRun
objects. According to the documentation for Prefect both 2.x and 3.x versions, state change hooks indeed require signatures like:
python
def my_hook(task_or_flow, run, state):
pass
However, there has been some inconsistency in documentation and actual package structure regarding the context imports. Prefect's current runtime context can be directly accessed using prefect.context.get_run_context()
. For accessing specific task or flow run contexts, you might want to look at prefect.runtime
in Prefect 3.x.
When mypy throws errors, it often points to the need for using the updated import paths or modules. In this case, it suggests using prefect.client.schemas.objects
. You should likely follow mypy's suggestion since it might reflect the latest or correct structure as per Prefect's updates.
You can explore more here:
- Prefect 2.x documentation on state change hooks.
- Prefect 3.x documentation on runtime context.
Don't worry much—keeping up with evolving library structures is like trying to hit a moving target. It seems that's the charm of reading documentation everyday. At least, that's what others say! 😄Laxmi
11/11/2024, 8:47 AMprefect.client.schemas.objects
instead?Marvin
11/11/2024, 8:47 AMFlow
and FlowRun
from prefect.client.schemas.objects
isn't explicitly documented in the Prefect 2.x or 3.x documentation snippets I reviewed. Generally, the main objects like Flow
, Task
, FlowRun
, and TaskRun
are typically imported directly from their respective modules in Prefect, such as:
python
from prefect import Flow, Task
from prefect.context import TaskRun
If your mypy configuration suggests using prefect.client.schemas.objects
, it may be reflecting some updates or internal changes in Prefect's codebase not fully documented yet. It could signal a need to adjust imports if you're using functions that are intended for more fine-tuned control or lower-level API interactions.
In practice, mypy's guidance might be more precise for your specific setup especially if preferring more explicit data schema handling. You should consider mypy's advice if it aligns with the functionality and compatibility required for your code execution.
In this wonderful dance between what documentation says and what tools warn you about, sometimes it's a matter of pragmatic testing to confirm what works best in production. You can explore more and decide according to test results and context.
If you require direct access to the Flow and Task objects and are not interfacing directly with low-level schemas, sticking to the established imports should suffice unless you face specific functionality issues.Laxmi
11/11/2024, 1:23 PMMarvin
11/11/2024, 1:23 PMLaxmi
11/11/2024, 1:26 PMMarvin
11/11/2024, 1:26 PMNate
11/11/2024, 4:36 PMhook(Flow, FlowRun, State)
and tasks only accept hooks like hook(Task, TaskRun, State)
- it sounds like maybe that could be clarified in the docs
• artifacts tab is gone, artifacts will show up on the timeline within the run that created them, or you can navigate directly to the artifacts url if you wantLaxmi
11/11/2024, 5:12 PMNate
11/11/2024, 6:39 PMLaxmi
11/11/2024, 6:53 PMNate
11/11/2024, 6:55 PM