33from typing import Any
44
55
6- def accepts_request (func : Callable [..., Any ]) -> bool :
6+ def accepts_single_positional_arg (func : Callable [..., Any ]) -> bool :
77 """
8- True if the function accepts a cursor parameter call , otherwise false.
8+ True if the function accepts at least one positional argument , otherwise false.
99
10- `accepts_cursor` does not validate that the function will work. For
11- example, if `func` contains keyword-only arguments with no defaults,
12- then it will not work when used in the `lowlevel/server.py` code, but
13- this function will not raise an exception.
10+ This function intentionally does not define behavior for `func`s that
11+ contain more than one positional argument, or any required keyword
12+ arguments without defaults.
1413 """
1514 try :
1615 sig = inspect .signature (func )
@@ -20,26 +19,28 @@ def accepts_request(func: Callable[..., Any]) -> bool:
2019 params = dict (sig .parameters .items ())
2120
2221 if len (params ) == 0 :
23- # No parameters at all - can't accept cursor
22+ # No parameters at all - can't accept single argument
2423 return False
2524
2625 # Check if ALL remaining parameters are keyword-only
2726 all_keyword_only = all (param .kind == inspect .Parameter .KEYWORD_ONLY for param in params .values ())
2827
2928 if all_keyword_only :
3029 # If all params are keyword-only, check if they ALL have defaults
31- # If they do, the function can be called with no arguments -> no cursor
30+ # If they do, the function can be called with no arguments -> no argument
3231 all_have_defaults = all (param .default is not inspect .Parameter .empty for param in params .values ())
33- return not all_have_defaults # False if all have defaults (no cursor), True otherwise
32+ if all_have_defaults :
33+ return False
34+ # otherwise, undefined (doesn't accept a positional argument, and requires at least one keyword only)
3435
3536 # Check if the ONLY parameter is **kwargs (VAR_KEYWORD)
36- # A function with only **kwargs can't accept a positional cursor argument
37+ # A function with only **kwargs can't accept a positional argument
3738 if len (params ) == 1 :
3839 only_param = next (iter (params .values ()))
3940 if only_param .kind == inspect .Parameter .VAR_KEYWORD :
40- return False # Can't pass positional cursor to **kwargs
41+ return False # Can't pass positional argument to **kwargs
4142
42- # Has at least one positional or variadic parameter - can accept cursor
43+ # Has at least one positional or variadic parameter - can accept argument
4344 # Important note: this is designed to _not_ handle the situation where
4445 # there are multiple keyword only arguments with no defaults. In those
4546 # situations it's an invalid handler function, and will error. But it's
0 commit comments