Skip to content

Commit

Permalink
refactor: make globalns parameter optional
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftinv committed Oct 8, 2023
1 parent d0bb39d commit 84ea3f8
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions disnake/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1212,22 +1212,41 @@ def unwrap_function(function: Callable[..., Any]) -> Callable[..., Any]:
return function


def _get_function_globals(function: Callable[..., Any]) -> Dict[str, Any]:
unwrap = unwrap_function(function)
try:
return unwrap.__globals__
except AttributeError:
return {}


_inspect_empty = inspect.Parameter.empty


def get_signature_parameters(
function: Callable[..., Any], globalns: Dict[str, Any]
function: Callable[..., Any], globalns: Optional[Dict[str, Any]] = None
) -> Dict[str, inspect.Parameter]:
signature = inspect.signature(function)
params = {}
# if no globalns provided, unwrap (where needed) and get global namespace from there
if globalns is None:
globalns = _get_function_globals(function)

params: Dict[str, inspect.Parameter] = {}
cache: Dict[str, Any] = {}

signature = inspect.signature(function)

# eval all parameter annotations
for name, parameter in signature.parameters.items():
annotation = parameter.annotation
if annotation is parameter.empty:
if annotation is _inspect_empty:
params[name] = parameter
continue

if annotation is None:
params[name] = parameter.replace(annotation=type(None))
continue
annotation = type(None)
else:
annotation = evaluate_annotation(annotation, globalns, globalns, cache)

annotation = evaluate_annotation(annotation, globalns, globalns, cache)
params[name] = parameter.replace(annotation=annotation)

return params
Expand Down

0 comments on commit 84ea3f8

Please sign in to comment.