Skip to content

Commit

Permalink
Passes gr.Request if type hint is Request | None (#9011)
Browse files Browse the repository at this point in the history
* changes

* fix for python 3.8, 3.9

* fix

* add changeset

---------

Co-authored-by: gradio-pr-bot <[email protected]>
  • Loading branch information
abidlabs and gradio-pr-bot authored Aug 7, 2024
1 parent 252f03a commit 0978de8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/salty-terms-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gradio": patch
---

fix:Passes `gr.Request` if type hint is `Request | None`
2 changes: 1 addition & 1 deletion gradio/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ def special_args(
progress_index = i
if inputs is not None:
inputs.insert(i, param.default)
elif type_hint == routes.Request:
elif type_hint in (routes.Request, Optional[routes.Request]):
if inputs is not None:
inputs.insert(i, request)
elif type_hint in (
Expand Down
14 changes: 11 additions & 3 deletions gradio/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,12 +923,20 @@ def get_type_hints(fn):
for name, param in sig.parameters.items():
if param.annotation is inspect.Parameter.empty:
continue
if param.annotation == "gr.OAuthProfile | None":
if param.annotation in ["gr.OAuthProfile | None", "None | gr.OAuthProfile"]:
# Special case: we want to inject the OAuthProfile value even on Python 3.9
type_hints[name] = Optional[OAuthProfile]
if param.annotation == "gr.OAuthToken | None":
if param.annotation == ["gr.OAuthToken | None", "None | gr.OAuthToken"]:
# Special case: we want to inject the OAuthToken value even on Python 3.9
type_hints[name] = Optional[OAuthToken]
if param.annotation in [
"gr.Request | None",
"Request | None",
"None | gr.Request",
"None | Request",
]:
# Special case: we want to inject the Request value even on Python 3.9
type_hints[name] = Optional[Request]
if "|" in str(param.annotation):
continue
# To convert the string annotation to a class, we use the
Expand All @@ -953,7 +961,7 @@ def is_special_typed_parameter(name, parameter_types):
hint = parameter_types.get(name)
if not hint:
return False
is_request = hint == Request
is_request = hint in (Request, Optional[Request])
is_oauth_arg = hint in (
OAuthProfile,
Optional[OAuthProfile],
Expand Down
8 changes: 7 additions & 1 deletion test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,15 @@ class GenericObject:
assert len(get_type_hints(GenericObject())) == 0

def test_is_special_typed_parameter(self):
def func(a: list[str], b: Literal["a", "b"], c, d: Request):
def func(a: list[str], b: Literal["a", "b"], c, d: Request, e: Request | None):
pass

hints = get_type_hints(func)
assert not is_special_typed_parameter("a", hints)
assert not is_special_typed_parameter("b", hints)
assert not is_special_typed_parameter("c", hints)
assert is_special_typed_parameter("d", hints)
assert is_special_typed_parameter("e", hints)

def test_is_special_typed_parameter_with_pipe(self):
def func(a: Request, b: str | int, c: list[str]):
Expand Down Expand Up @@ -502,6 +503,11 @@ def func(a, r: Request, b=10):

assert get_function_params(func) == [("a", False, None), ("b", True, 10)]

def func2(a, r: Request | None = None, b="abc"):
pass

assert get_function_params(func2) == [("a", False, None), ("b", True, "abc")]

def test_class_method_skip_first_param(self):
class MyClass:
def method(self, arg1, arg2=42):
Expand Down

0 comments on commit 0978de8

Please sign in to comment.