Skip to content

Parameter

Parameter

Parameter(name: str, kind: ParameterKind, annotation: TypeAnnotation = EMPTY, default: RuntimeValue = EMPTY, description: str | None = None, infer_type: bool = True, **legacy_kwargs: TypeAnnotation)

A Parameter instance contains information about a function or method parameter, including its name, type, default value, and description.

Initialize a Parameter instance.

Parameters:

Name Type Description Default
name str

The name of the parameter.

required
kind ParameterKind

The kind of the parameter (e.g. POSITIONAL_ONLY, VAR_POSITIONAL).

required
annotation object

The type annotation of the parameter.

EMPTY
default object

The default value of the parameter.

EMPTY
description str | None

The description of the parameter.

None
infer_type bool

Infer the type of the parameter based on its default value.

True
**legacy_kwargs object

Backward-compatible keyword support for type=.

{}
Source code in objinspect/parameter.py
def __init__(
    self,
    name: str,
    kind: ParameterKind,
    annotation: TypeAnnotation = EMPTY,
    default: RuntimeValue = EMPTY,
    description: str | None = None,
    infer_type: bool = True,
    **legacy_kwargs: TypeAnnotation,
) -> None:
    """
    Initialize a `Parameter` instance.

    Args:
        name (str): The name of the parameter.
        kind: The kind of the parameter (e.g. POSITIONAL_ONLY, VAR_POSITIONAL).
        annotation (object): The type annotation of the parameter.
        default (object): The default value of the parameter.
        description (str | None): The description of the parameter.
        infer_type (bool): Infer the type of the parameter based on its default value.
        **legacy_kwargs (object): Backward-compatible keyword support for `type=`.
    """
    if "type" in legacy_kwargs:
        if annotation is not EMPTY:
            raise TypeError("'annotation' and legacy 'type' cannot both be provided.")

        annotation = legacy_kwargs.pop("type")

    if legacy_kwargs:
        unknown = ", ".join(sorted(legacy_kwargs))
        raise TypeError(f"Unexpected keyword arguments: {unknown}")

    self.name = name
    self.type = annotation
    self.default = default
    self.description = description
    self.kind = kind
    if infer_type and not self.is_typed:
        self.type = self.get_infered_type()

is_typed property

is_typed: bool

Whether the parameter has an explicit type annotation.

is_required property

is_required: bool

Whether the parameter is required (has no default value).

is_optional property

is_optional: bool

Whether the parameter is optional (has a default value).

has_default property

has_default: bool

Whether the parameter has a default value.

dict property

dict: dict[str, object]

Return a dictionary representation of the parameter.

get_infered_type

get_infered_type() -> TypeAnnotation

Infer the type of the parameter based on its default value.

Source code in objinspect/parameter.py
def get_infered_type(self) -> TypeAnnotation:
    """Infer the type of the parameter based on its default value."""
    if self.default is EMPTY:
        return EMPTY

    return type(self.default)

as_str

as_str(*, color: bool = True, theme: ParameterStrTheme | None = None) -> str

Return a string representation of the parameter.

Parameters:

Name Type Description Default
color bool

Whether to colorize the output.

True
theme ParameterStrTheme

Color theme to use. Default will be used if None.

None
Source code in objinspect/parameter.py
def as_str(self, *, color: bool = True, theme: ParameterStrTheme | None = None) -> str:
    """
    Return a string representation of the parameter.

    Args:
        color (bool, optional): Whether to colorize the output.
        theme (ParameterStrTheme, optional): Color theme to use. Default will be used if None.
    """
    if theme is None:
        theme = ParameterStrTheme()

    if self.is_typed:
        if color:
            type_str = colored_type(self.type, style=TextStyle(theme.type), simplify=False)
        else:
            type_str = type_name(self.type)
    else:
        type_str = ""

    default_str = f"{self.default}" if self.is_optional else ""
    if self.default is not EMPTY:
        if color:
            default_str = colored(default_str, theme.default)
        default_str = f" = {default_str}"
    else:
        default_str = ""

    name_str = self.name if not color else colored(self.name, theme.name)

    return f"{name_str}{type_str}{default_str}"

from_inspect_param classmethod

from_inspect_param(param: Parameter, description: str | None = None) -> Parameter

Create a Parameter instance from an inspect.Parameter object.

Parameters:

Name Type Description Default
param Parameter

The inspect.Parameter to convert.

required
description str | None

Optional description for the parameter.

None
Source code in objinspect/parameter.py
@classmethod
def from_inspect_param(
    cls,
    param: inspect.Parameter,
    description: str | None = None,
) -> "Parameter":
    """
    Create a Parameter instance from an inspect.Parameter object.

    Args:
        param (inspect.Parameter): The inspect.Parameter to convert.
        description (str | None): Optional description for the parameter.
    """
    return cls(
        name=param.name,
        annotation=param.annotation,
        default=param.default,
        description=description,
        kind=param.kind,
    )