Skip to content

General Class Helper

Helper utilities for introspecting and analyzing class type annotations.

This module provides utilities for extracting and analyzing type information from Python classes, particularly for generating input specifications and field metadata. It's designed to work with classes that use type annotations to define their structure.

Key functionality: - Extract field names from class type annotations - Generate structured input specifications with type and requirement information - Detect optional fields (Union with None, Optional types, etc.) - Support both traditional Union syntax and Python 3.10+ union syntax (X | None) - Handle Annotated types by unwrapping them - Support Pydantic BaseModel field metadata for accurate required/optional detection

flatten_dict(d, parent_key='', sep='.')

Flatten a nested dictionary into a single-level dictionary.

Parameters:

Name Type Description Default
d dict[str, Any]

Dictionary to flatten.

required
parent_key str

Prefix used for nested keys.

''
sep str

Separator between parent and child keys.

'.'

Returns:

Type Description
dict[str, Any]

dict[str, Any]: Flattened dictionary.

Example

flatten_dict({"a": {"b": 1, "c": 2}, "d": 3})

get_input_fields(input_type)

Extract field names from a class's type annotations.

This function introspects a class to extract all field names that have type annotations. It's useful for dynamically discovering the structure of a class without instantiating it.

Parameters:

Name Type Description Default
input_type type | None

The class type to analyze. If None, returns None.

required

Returns:

Type Description
list[str] | None

list[str] | None: List of field names with type annotations, or None if input_type is None.

Examples:

>>> class MyClass:
...     name: str
...     age: int
...     email: str | None
>>> get_input_fields(MyClass)
['name', 'age', 'email']

get_input_spec(input_type)

Generate a structured specification for a class's input fields.

This function creates a detailed specification of all fields in a class, including their names, types, and whether they are required or optional. The specification is useful for API documentation, form generation, or validation systems.

For Pydantic models, uses field metadata to accurately determine if fields are required (respects defaults). For other types, uses type annotations.

Parameters:

Name Type Description Default
input_type type | None

The class type to analyze. If None, returns None.

required

Returns:

Type Description
list[dict[str, Any]] | None

list[dict[str, Any]] | None: List of field specifications, each containing: - name: The field name - type: String representation of the field type - required: Boolean indicating if the field is required (not optional)

list[dict[str, Any]] | None

Returns None if input_type is None.

Examples:

>>> class UserConfig:
...     username: str
...     email: str | None
...     age: int
>>> get_input_spec(UserConfig)
[
    {'name': 'username', 'type': 'str', 'required': True},
    {'name': 'email', 'type': 'str | None', 'required': False},
    {'name': 'age', 'type': 'int', 'required': True}
]

is_numeric(value)

Check whether a value can be converted to float.