in bowler/query.py [0:0]
def remove_argument(self, name: str) -> "Query":
transform = self.current
if transform.selector not in ("function", "method"):
raise ValueError(f"modifier must follow select_function or select_method")
# determine correct position (excluding self/cls) to add new argument
stop_at = -1
if "source" not in transform.kwargs:
raise ValueError("remove_argument requires passing original function")
signature = inspect.signature(transform.kwargs["source"])
if name not in signature.parameters:
raise ValueError(f"{name} does not exist in original function")
if signature.parameters[name].kind in (
inspect.Parameter.VAR_KEYWORD,
inspect.Parameter.VAR_POSITIONAL,
):
raise ValueError("can't remove *args or **kwargs")
positional = signature.parameters[name].kind in (
inspect.Parameter.POSITIONAL_ONLY,
inspect.Parameter.POSITIONAL_OR_KEYWORD,
)
names = list(signature.parameters)
stop_at = names.index(name)
if names[0] in ("self", "cls", "meta"):
stop_at -= 1
def remove_argument_transform(
node: Node, capture: Capture, filename: Filename
) -> None:
if "function_def" not in capture and "function_call" not in capture:
return
spec = FunctionSpec.build(node, capture)
if spec.is_def or not positional:
for argument in spec.arguments:
if argument.name == name:
spec.arguments.remove(argument)
break
else:
for index, argument in reversed(list(enumerate(spec.arguments))):
if argument.name == name:
spec.arguments.pop(index)
break
if index == stop_at and not argument.name and not argument.star:
spec.arguments.pop(index)
break
spec.explode()
transform.callbacks.append(remove_argument_transform)
return self