Closed peterschutt closed 2 years ago
Here's more context:
from uuid import UUID
from pydantic import Field
from app import core
from .types import EntitiesEnum
class Extra(core.Schema):
sub_entity: "Entity | None"
class Entity(core.Schema):
id: UUID
name: str
owner_id: UUID | None
provider_id: UUID
type: EntitiesEnum
extra: Extra = Field(default_factory=Extra)
Extra.update_forward_refs()
If I comment out Entity.extra
on the Entity
model, the error goes away.
I see, ok your recursion error happens because you have a self referncing field - extra, referencing entity, referning extra etc. This is a known issue with pydantic-factories, I had to work around it in the sqlalchemy stuff.
Is it something to be fixed or worked-around internally to Starlite? Or does the fix need to come from upstream?
the issue is upstream. its a complex fix to avoid recursion and the behaviour is not really clear - so Ill see if we can work around it locally.
I'm getting the same issue when using msgspec.Struct
:
from msgspec import Struct
class EntityV3(Struct):
name: str
associated_entities: list["EntityV3"]
The logs:
[my-app] ERROR - 2023-09-27 12:41:50,018 - litestar - config - exception raised on http connection to route /api/schema
[my-app]
[my-app] Traceback (most recent call last):
[my-app] File "/usr/local/lib/python3.11/site-packages/litestar/_openapi/schema_generation/schema.py", line 351, in for_object_type
[my-app] items = list(map(self.for_field_definition, field_definition.inner_types or ()))
[my-app] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[my-app] File "/usr/local/lib/python3.11/site-packages/litestar/_openapi/schema_generation/schema.py", line 269, in for_field_definition
[my-app] result = self.for_struct_class(field_definition.annotation)
[my-app] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[my-app] File "/usr/local/lib/python3.11/site-packages/litestar/_openapi/schema_generation/schema.py", line 464, in for_struct_class
[my-app] properties={
[my-app] ^
[my-app] File "/usr/local/lib/python3.11/site-packages/litestar/_openapi/schema_generation/schema.py", line 465, in <dictcomp>
[my-app] field.encode_name: self.for_field_definition(FieldDefinition.from_kwarg(field.type, field.encode_name))
[my-app] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[my-app] File "/usr/local/lib/python3.11/site-packages/litestar/_openapi/schema_generation/schema.py", line 265, in for_field_definition
[my-app] result = self.for_optional_field(field_definition)
[my-app] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[my-app] File "/usr/local/lib/python3.11/site-packages/litestar/_openapi/schema_generation/schema.py", line 304, in for_optional_field
[my-app] schema_or_reference = self.for_field_definition(
[my-app] ^^^^^^^^^^^^^^^^^^^^^^^^^^
[my-app] File "/usr/local/lib/python3.11/site-packages/litestar/_openapi/schema_generation/schema.py", line 269, in for_field_definition
[my-app] result = self.for_struct_class(field_definition.annotation)
[my-app] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[my-app] File "/usr/local/lib/python3.11/site-packages/litestar/_openapi/schema_generation/schema.py", line 464, in for_struct_class
[my-app] properties={
[my-app] ^
[my-app] File "/usr/local/lib/python3.11/site-packages/litestar/_openapi/schema_generation/schema.py", line 465, in <dictcomp>
[my-app] field.encode_name: self.for_field_definition(FieldDefinition.from_kwarg(field.type, field.encode_name))
[my-app] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[my-app] File "/usr/local/lib/python3.11/site-packages/litestar/_openapi/schema_generation/schema.py", line 269, in for_field_definition
[my-app] result = self.for_struct_class(field_definition.annotation)
[my-app] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[my-app] File "/usr/local/lib/python3.11/site-packages/litestar/_openapi/schema_generation/schema.py", line 464, in for_struct_class
[my-app] properties={
[my-app] ^
[my-app] File "/usr/local/lib/python3.11/site-packages/litestar/_openapi/schema_generation/schema.py", line 465, in <dictcomp>
[my-app] field.encode_name: self.for_field_definition(FieldDefinition.from_kwarg(field.type, field.encode_name))
[my-app] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[my-app] File "/usr/local/lib/python3.11/site-packages/litestar/typing.py", line 511, in from_kwarg
[my-app] return cls.from_annotation(
[my-app] ^^^^^^^^^^^^^^^^^^^^
[my-app] File "/usr/local/lib/python3.11/site-packages/litestar/typing.py", line 459, in from_annotation
[my-app] kwargs["kwarg_definition"], kwargs["extra"] = cls._extract_metadata(
[my-app] ^^^^^^^^^^^^^^^^^^^^^^
[my-app] File "/usr/local/lib/python3.11/site-packages/litestar/typing.py", line 234, in _extract_metadata
[my-app] if is_pydantic_constrained_field(annotation) or isinstance(annotation, AbstractDTO):
[my-app] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[my-app] File "/usr/local/lib/python3.11/site-packages/litestar/utils/predicates.py", line 342, in is_pydantic_constrained_field
[my-app] from pydantic import (
[my-app] File "<frozen importlib._bootstrap>", line 1231, in _handle_fromlist
[my-app] File "/usr/local/lib/python3.11/site-packages/pydantic/__init__.py", line 210, in __getattr__
[my-app] return _getattr_migration(attr_name)
[my-app] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[my-app] File "/usr/local/lib/python3.11/site-packages/pydantic/_migration.py", line 295, in wrapper
[my-app] raise PydanticImportError(f'`{import_path}` has been removed in V2.')
[my-app] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[my-app] RecursionError: maximum recursion depth exceeded
Is this still an upstream issue, even though I'm not using pydantic
?
I checked the issues on the msgspec
repo in Github, and there's no mention of the RecursionError
I haven't had a chance to debug this at all, as I only turned the feature on to test something unrelated.
This is at the bottom of the traceback:
This is the top: