prefect icon indicating copy to clipboard operation
prefect copied to clipboard

Dependency on import order in user-code

Open cicdw opened this issue 1 year ago • 0 comments

Bug summary

The following code errors out:

from prefect.states import State
State(type="CANCELLED").to_state_create()

with traceback:

---------------------------------------------------------------------------
PydanticUserError                         Traceback (most recent call last)
Cell In[1], line 2
      1 from prefect.states import State
----> 2 State(type="CANCELLED").to_state_create()

File ~/dev/prefect/src/prefect/client/schemas/objects.py:335, in State.to_state_create(self)
    332 else:
    333     data = None
--> 335 return StateCreate(
    336     type=self.type,
    337     name=self.name,
    338     message=self.message,
    339     data=data,
    340     state_details=self.state_details,
    341 )

    [... skipping hidden 1 frame]

File ~/miniconda3/envs/prefect/lib/python3.10/site-packages/pydantic/_internal/_mock_val_ser.py:47, in MockValSer.__getattr__(self, item)
     45 # raise an AttributeError if `item` doesn't exist
     46 getattr(self._val_or_ser, item)
---> 47 raise PydanticUserError(self._error_message, code=self._code)

PydanticUserError: `StateCreate` is not fully defined; you should define all referenced types, then call `StateCreate.model_rebuild()`.

For further information visit https://errors.pydantic.dev/2.7/u/class-not-fully-defined

It can be resolved by importing anything from the main prefect module:

from prefect import State # any other object would work too

State(type="CANCELLED").to_state_create() # ✅

Version info

Version:             3.1.1.dev6+1.g2624409ffc
API version:         0.8.4
Python version:      3.10.13
Git commit:          2624409f
Built:               Fri, Nov 8, 2024 8:12 AM
OS/Arch:             darwin/arm64
Profile:             ephemeral
Server type:         ephemeral
Pydantic version:    2.7.1
Server:
  Database:          sqlite
  SQLite version:    3.41.2
Integrations:
  prefect-aws:       0.5.0

Additional context

This is related to our known circular import problems; I'm opening this for other users who may be searching with this error message.

cicdw avatar Nov 08 '24 18:11 cicdw