Skip to main content

inspect-function

PyPI version Python 3.11+ License: MIT CI

Inspect Python functions and get structured metadata as Pydantic models.

Links:

Installation

pip install inspect-function

Quick Start

from inspect_function import inspect_function

def greet(name: str, age: int = 25) -> str:
    return f"Hello {name}, you are {age} years old"

# Inspect the function
inspection = inspect_function(greet)

print(inspection.awaitable)  # False
print(len(inspection.parameters))  # 2
print(inspection.return_annotation)  # "<class 'str'>"

Basic Usage

Analyze Function Signatures

from inspect_function import inspect_function

def example_func(a: int, b: str = "default", *, c: bool, **kwargs):
    pass

inspection = inspect_function(example_func)

# Check function type
print(inspection.is_function)  # True
print(inspection.is_method)    # False
print(inspection.awaitable)   # False

# Access parameters
for param in inspection.parameters:
    print(f"{param.name}: {param.annotation} (required: {not param.is_optional})")

Transform Parameters for Function Calls

from inspect_function import inspect_parameters

def api_endpoint(user_id: int, limit: int = 10, *, include_deleted: bool = False):
    return f"Fetching {limit} items for user {user_id}"

# Convert dict to proper args/kwargs
params = {"user_id": 123, "limit": 20, "include_deleted": True}
args, kwargs = inspect_parameters(api_endpoint, params)

# Call the function
result = api_endpoint(*args, **kwargs)
print(result)  # "Fetching 20 items for user 123"

Generate JSON Schema

from inspect_function import inspect_function

def create_user(name: str, email: str, age: int = 18):
    pass

inspection = inspect_function(create_user)
schema = inspection.json_schema

print(schema)
# {
#   "type": "object",
#   "properties": {
#     "name": {"type": "string", "description": "Parameter 'name' of kind positional_or_keyword"},
#     "email": {"type": "string", "description": "Parameter 'email' of kind positional_or_keyword"},
#     "age": {"type": "integer", "default": "18", "description": "Parameter 'age' of kind positional_or_keyword"}
#   },
#   "required": ["name", "email"],
#   "additionalProperties": false
# }

Advanced Examples

Async Functions

import asyncio
from inspect_function import inspect_function

async def fetch_data(url: str, timeout: float = 30.0) -> dict:
    await asyncio.sleep(0.1)
    return {"url": url, "status": "ok"}

inspection = inspect_function(fetch_data)
print(inspection.awaitable)  # True
print(inspection.is_coroutine_function)  # True

Class Methods

from inspect_function import inspect_function

class DataProcessor:
    def process(self, data: list) -> list:
        return data

    @classmethod
    def from_config(cls, config: dict) -> 'DataProcessor':
        return cls()

    @staticmethod
    def validate(data: str) -> bool:
        return bool(data)

# Instance method
inspection = inspect_function(DataProcessor.process)
print(inspection.is_method)  # True

# Class method
inspection = inspect_function(DataProcessor.from_config)
print(inspection.is_classmethod)  # True

# Static method
inspection = inspect_function(DataProcessor.validate)
print(inspection.is_function)  # True (static methods are just functions)

Complex Parameter Types

from typing import List, Dict, Optional, Union
from inspect_function import inspect_function

def complex_func(
    items: List[str],
    mapping: Dict[str, int],
    optional_data: Optional[str] = None,
    *args: float,
    flag: bool,
    **kwargs: Union[str, int]
) -> None:
    pass

inspection = inspect_function(complex_func)

# Access different parameter types
print(f"Positional/keyword: {len(inspection.positional_or_keyword_params)}")
print(f"Keyword-only: {len(inspection.keyword_only_params)}")
print(f"*args param: {inspection.var_positional_param.name if inspection.var_positional_param else None}")
print(f"**kwargs param: {inspection.var_keyword_param.name if inspection.var_keyword_param else None}")
print(f"Required params: {[p.name for p in inspection.required_params]}")

API Reference

inspect_function(func) -> FunctionInspection

Returns a FunctionInspection object with detailed information about the function.

Properties:

  • awaitable: bool - Whether the function is async
  • parameters: List[Parameter] - List of all parameters
  • return_annotation: str - Return type annotation
  • is_method: bool - Instance method detection
  • is_classmethod: bool - Class method detection
  • is_function: bool - Regular function detection

inspect_parameters(func, params: dict) -> tuple[tuple, dict]

Transforms a parameter dictionary into properly ordered args and kwargs for function calls.

Parameter Model

  • name: str - Parameter name
  • kind: ParameterKind - Parameter type (positional, keyword, etc.)
  • annotation: str - Type annotation
  • has_default: bool - Whether parameter has default value
  • is_optional: bool - Whether parameter is optional
  • position: int | None - Position in signature

License

MIT License - see LICENSE file for details.

Metadata

Release files for inspect-function 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for inspect-function 0.2.1
File Size Uploaded
inspect_function-0.2.1.tar.gz 12.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for inspect-function 0.2.1
File Interpreter ABI Platform
inspect_function-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 25.8 kB

Release files / inspect_function-0.2.1.tar.gz

Download URL inspect_function-0.2.1.tar.gz
Size 12.9 kB
Tags Source
SHA-256 checksum
How to use checksums
2114f17a7f858e070582ea57057c9f8630946de98778e877e7830379a5e606b6
BLAKE2b-256 checksum
How to use checksums
f1043da94e5e31edf180923cdf9e212fb61000f4e8baa03945cc94985b4454b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.3 CPython/3.11.13 Darwin/24.5.0

Release files / inspect_function-0.2.1-py3-none-any.whl

Download URL inspect_function-0.2.1-py3-none-any.whl
Size 12.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9ee62ca8d2d36eb4a8d66e543830c664ff721db51cb6b87492936effb0edcb3e
BLAKE2b-256 checksum
How to use checksums
1b651ddd94b2ec590c99b115ab356aaea47b65aad997efa40a126fb07bfb6b7b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.3 CPython/3.11.13 Darwin/24.5.0

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page