Replaced hasattr in dumps with supports_argument

This commit is contained in:
2024-05-26 21:39:55 +02:00
parent 15f9274050
commit 64ba9efa34
4 changed files with 50 additions and 2 deletions

22
src/libbot/_utils.py Normal file
View File

@@ -0,0 +1,22 @@
import inspect
from typing import Callable
def supports_argument(func: Callable, arg_name: str) -> bool:
"""Check whether a function has a specific argument
### Args:
* func (`Callable`): Function to be inspected
* arg_name (`str`): Argument to be checked
### Returns:
* `bool`: `True` if argument is supported and `False` if not
"""
if hasattr(func, "__code__"):
return arg_name in inspect.signature(func).parameters
elif hasattr(func, "__doc__"):
if doc := func.__doc__:
first_line = doc.splitlines()[0]
return arg_name in first_line
return False