Skip to content

ForwardRef annotations and non-global context breaks models resolution #26

Closed
@Nnonexistent

Description

@Nnonexistent

If both of the following conditions are met, the PydanticUserError exception is raised during function call:

  1. ForwardRef annotations for arguments (e.g. with from __future__ import annotations or, even, just with string annotations)
  2. Usage of non-global context. For example in attempt to dynamically create inject in wrapped function.

My particular case was from usage of faststream, where I needed dynamically attach broker to different handlers. It all works well, but I have to be careful not to switch forward refs by accident.

Reproduction:

from __future__ import annotations  # all is working if you comment this line

import asyncio
from pydantic import BaseModel

from fast_depends import inject


def container_function():
    class Model(BaseModel):
        num: int

    @inject
    async def main(a: Model):
        pass

    return main


main = container_function()
asyncio.run(main({"num": 1}))

PS. Nice libraries, keep up great work!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions