zmievsa / cadwyn

Production-ready community-driven modern Stripe-like API versioning in FastAPI
https://docs.cadwyn.dev/
MIT License
212 stars 28 forks source link

Pydantic error when using `svcs` as a dependency parameter #192

Closed ianbuss closed 3 months ago

ianbuss commented 3 months ago

Describe the bug There is a Pydantic issue when using a svcs dependency in a router function signature. For example, this kind of function:

import svcs

@app.get("/foo")
async def foo(services: svcs.fastapi.DepContainer):
    pass

produces the following error in a Cadwyn app:

Traceback (most recent call last):
  File "/path/to/venv/lib/python3.11/site-packages/pydantic/_internal/_generate_schema.py", line 691, in _resolve_forward_ref
    obj = _typing_extra.eval_type_backport(obj, globalns=self._types_namespace)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/venv/lib/python3.11/site-packages/pydantic/_internal/_typing_extra.py", line 254, in eval_type_backport
    return typing._eval_type(  # type: ignore
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/typing.py", line 395, in _eval_type
    return t._evaluate(globalns, localns, recursive_guard)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/typing.py", line 905, in _evaluate
    eval(self.__forward_code__, globalns, localns),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
NameError: name 'Request' is not defined

This seems to be something to do with the dependency function being defined in an external package I think, as if I copy the underlying function (https://github.com/hynek/svcs/blob/9cca7dde04a318c9f876dd12226b9b488d9721bc/src/svcs/fastapi.py#L78) into my code, it works fine.

To Reproduce

  1. Add a services: svcs.fastapi.DepContainer dependency to a router funtion
  2. Try to run the app

Expected behavior Cadwyn should be able to treat the svcs dependency as a standard FastAPI Depends.

zmievsa commented 3 months ago

Thanks for reporting this! I am not sure how to solve this yet but I'll take a close look today.

ianbuss commented 3 months ago

Thanks for reporting this! I am not sure how to solve this yet but I'll take a close look today.

Thanks @zmievsa! I'll look into the code as well just for my understanding of how Cadwyn is doing things here.

zmievsa commented 3 months ago

@ianbuss Please, check whether your problem was fixed in 3.15.6. I added a test case specifically with svcs.

Looks like a strange pydantic bug to me but sadly I have no time to fix it in its entirety right now. I'll try to come up with some approach that makes such errors impossible. However, the part that affected svcs is gone.

194 might be able to solve this entire class of problems.

ianbuss commented 3 months ago

Thanks @zmievsa ! Will try it out and let you know.

zmievsa commented 3 months ago

Hi! I found a way to fix the core of this bug, not just the "Request" part of it. Now it's fully gone and I am fairly certain that it will not come back in any other way, shape or form.

Fix in 3.15.7

ianbuss commented 3 months ago

Upgraded and it's working fine, thanks for the fix(es) @zmievsa !

zmievsa commented 3 months ago

Thank you for reporting this bug!