Skip to content

Commit

Permalink
Update model_rebuild in main.py to explicitly pass namespace types (
Browse files Browse the repository at this point in the history
  • Loading branch information
desertaxle authored Aug 28, 2024
1 parent ace5a37 commit 0ca0bf6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
12 changes: 8 additions & 4 deletions src/prefect/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@
# Perform any forward-ref updates needed for Pydantic models
import prefect.client.schemas

prefect.context.FlowRunContext.model_rebuild()
prefect.context.TaskRunContext.model_rebuild()
prefect.client.schemas.State.model_rebuild()
prefect.client.schemas.StateCreate.model_rebuild()
prefect.context.FlowRunContext.model_rebuild(
_types_namespace={"Flow": Flow, "BaseResult": BaseResult}
)
prefect.context.TaskRunContext.model_rebuild(_types_namespace={"Task": Task})
prefect.client.schemas.State.model_rebuild(_types_namespace={"BaseResult": BaseResult})
prefect.client.schemas.StateCreate.model_rebuild(
_types_namespace={"BaseResult": BaseResult}
)
Transaction.model_rebuild()

# Configure logging
Expand Down
27 changes: 16 additions & 11 deletions src/prefect/utilities/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,22 @@ def register_type(cls: T) -> T:
key = get_dispatch_key(cls)
existing_value = registry.get(key)
if existing_value is not None and id(existing_value) != id(cls):
# Get line numbers for debugging
file = inspect.getsourcefile(cls)
line_number = inspect.getsourcelines(cls)[1]
existing_file = inspect.getsourcefile(existing_value)
existing_line_number = inspect.getsourcelines(existing_value)[1]
warnings.warn(
f"Type {cls.__name__!r} at {file}:{line_number} has key {key!r} that "
f"matches existing registered type {existing_value.__name__!r} from "
f"{existing_file}:{existing_line_number}. The existing type will be "
"overridden."
)
try:
# Get line numbers for debugging
file = inspect.getsourcefile(cls)
line_number = inspect.getsourcelines(cls)[1]
existing_file = inspect.getsourcefile(existing_value)
existing_line_number = inspect.getsourcelines(existing_value)[1]
warnings.warn(
f"Type {cls.__name__!r} at {file}:{line_number} has key {key!r} that "
f"matches existing registered type {existing_value.__name__!r} from "
f"{existing_file}:{existing_line_number}. The existing type will be "
"overridden."
)
except OSError:
# If we can't get the source, another actor is loading this class via eval
# and we shouldn't update the registry
return cls

# Add to the registry
registry[key] = cls
Expand Down

0 comments on commit 0ca0bf6

Please sign in to comment.