Closed gcalmettes closed 3 years ago
Thanks @gcalmettes ! I am not very sure that this exception is a bug from makefun
. It seems rather that the default value used is something that raises an exception when evaluated.
Does Body(default="")
run correctly outside of the signature ?
Hi @smarie ,
Yes, Body(default="")
runs correctly if not applying the @wraps
from makefun
.
Actually, using @wraps
from functools
works without error. Below an even smaller exemple.
This won't raise a TypeError:
from fastapi import Body
from functools import wraps
def my_function(a: str, b: str = Body(default="")) -> str:
return "hello"
@wraps(my_function)
def wrapped_function(
*args,
**kwargs,
):
return True
This will raise a TypeError:
from fastapi import Body
from makefun import wraps
def my_function(a: str, b: str = Body(default="")) -> str:
return "hello"
@wraps(my_function)
def wrapped_function(
*args,
**kwargs,
):
return True
I do not have a local fastapi environment, can you please confirm that eval(repr(Body(default="")))
raises this same error ?
I confirm indeed
Python 3.8.6 (default, Oct 25 2020, 09:07:46)
[Clang 12.0.0 (clang-1200.0.31.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from fastapi import Body
>>> eval(repr(Body(default="")))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
TypeError: Body() missing 1 required positional argument: 'default'
>>>
Ok then. I think that we need to extend the scope here to catching general Exception
, so that other such issues do not appear in the future.
Done, thanks !
Thanks @gcalmettes, I'll make a release now
I created a test for future reference
This is now fixed in 1.11.3, released now. Thanks again @gcalmettes !
Thanks for your reactivity @smarie !
Hi,
First of all, thanks for
makefun
, very useful ! I came into the situation where I had to modify the signature of a FastAPI endpoint havingBody(default="")
as default value for one of the parameter. Any value other than the empty string for the default value ofBody
would work, but somehow having an empty string leads to a TypeError in the checking for proctection need of the symbol.line 370, in _signature_symbol_needs_protection return eval(repr(symbol), evaldict) != symbol File "<string>", line 1, in <module> TypeError: Body() missing 1 required positional argument: 'default'
This PR fixes this by allowing the handling of TypeError in the
_signature_symbol_needs_protection
function.Minimal example of breaking code before the PR: