# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/30_insp.ipynb.

# %% auto 0
__all__ = ['issig', 'isspec', 'isp', 'isvarpos', 'isvarkwd', 'isself', 'isclsarg', 'ispself', 'ispcls', 'isempty', 'ispempty',
           'iskwonly', 'ispkwonly', 'isfunc', 'islambda']

# %% ../nbs/30_insp.ipynb 4
from inspect import Parameter, Signature, isfunction, FullArgSpec
from types import FunctionType, LambdaType, BuiltinFunctionType, BuiltinMethodType
from typing import TypeGuard

from typing import Literal

# %% ../nbs/30_insp.ipynb 6
def issig(x) -> TypeGuard[Signature]:
    '''Check if x is of type `Signature`.'''
    return isinstance(x, Signature)

def isspec(x) -> TypeGuard[FullArgSpec]:
    '''Check if x is of type `FullArgSpec`.'''
    return isinstance(x, FullArgSpec)

# %% ../nbs/30_insp.ipynb 8
def isp(p: Parameter) -> TypeGuard[Parameter]:
    '''Check if parameter is of type `Parameter`.'''
    return isinstance(p, Parameter)

# %% ../nbs/30_insp.ipynb 9
def isvarpos(p: Parameter) -> TypeGuard[Parameter.VAR_POSITIONAL]:
    '''Check if parameter is of type `Parameter.VAR_POSITIONAL`.'''
    return isp(p) and p.kind == Parameter.VAR_POSITIONAL

def isvarkwd(p: Parameter) -> TypeGuard[Parameter.VAR_KEYWORD]:
    '''Check if parameter is of type `Parameter.VAR_KEYWORD`.'''
    return isp(p) and p.kind == Parameter.VAR_KEYWORD

# %% ../nbs/30_insp.ipynb 11
# NOTE: not sure if this is the same as `isinstance(x, Self)`
def isself(x) -> TypeGuard[Literal['self']]:
    '''Check if `x` is `"self"`.'''
    return x == 'self' 

# %% ../nbs/30_insp.ipynb 13
def isclsarg(x) -> TypeGuard[Literal['cls']]:
    '''Check if `x` is `"cls"`.'''
    return x == 'cls' 

# %% ../nbs/30_insp.ipynb 15
def ispself(p: Parameter) -> TypeGuard[Parameter]:
    '''Check if `p: Parameter` has `name = "self"`.'''
    return isp(p) and isself(p.name)

def ispcls(p: Parameter) -> TypeGuard[Parameter]:
    '''Check if `p: Parameter` has `name = "cls"`.'''
    return isp(p) and p.name == 'cls'

# %% ../nbs/30_insp.ipynb 17
def isempty(x) -> TypeGuard[Parameter.empty]:
    '''Check if `x` is `Parameter.empty`.'''
    return x == Parameter.empty

def ispempty(p: Parameter) -> TypeGuard[Parameter.empty]:
    '''Check if `p: Parameter` is of type `Parameter.empty`.'''
    return isp(p) and isempty(p.default)

# %% ../nbs/30_insp.ipynb 19
def iskwonly(x) -> TypeGuard[Parameter.KEYWORD_ONLY]:
    '''Check if `x` is `Parameter.KEYWORD_ONLY`.'''
    return x == Parameter.KEYWORD_ONLY

def ispkwonly(p: Parameter) -> TypeGuard[Parameter.KEYWORD_ONLY]:
    '''Check if `p: Parameter` is of type `Parameter.KEYWORD_ONLY`.'''
    return isp(p) and iskwonly(p.kind)

# %% ../nbs/30_insp.ipynb 21
def isfunc(x) -> TypeGuard[FunctionType]:
    '''Check if `x` is a function.'''
    return isfunction(x)

def islambda(f) -> TypeGuard[LambdaType]:
    '''Check if `x` is a lambda function.'''
    return isfunction(f) and f.__name__ == '<lambda>'
