Skip to content

Eval(parameters=...) type annotation should use Mapping instead of dict #280

@willfrey

Description

@willfrey

Problem

The parameters argument on Eval(), EvalAsync(), and the Evaluator dataclass is typed as:

parameters: dict[str, PromptParameter | ModelParameter | type[object] | None] | RemoteEvalParameters | None = None

dict is invariant in both its key and value types. This means callers who construct a dict containing only ModelParameter values get a mypy error:

from braintrust import Eval
from braintrust.parameters import ModelParameter

params: dict[str, ModelParameter] = {
    "model": {"type": "model", "default": "openai:gpt-4o"},
}

Eval(
    "my-eval",
    data=...,
    task=...,
    scores=[...],
    parameters=params,  # error: Argument "parameters" has incompatible type
                        # "dict[str, ModelParameter]"; expected
                        # "dict[str, PromptParameter | ModelParameter | ...]"
)

mypy even suggests the fix in its error note:

"dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
Consider using "Mapping" instead, which is covariant in the value type

Proposed fix

Change the annotation from dict to Mapping (from collections.abc). Mapping is covariant in the value type, so Mapping[str, ModelParameter] is assignable to Mapping[str, ModelParameter | PromptParameter | ...].

I confirmed that the SDK only ever reads the parameter dict (via .items(), .values(), .get()). It is never mutated after being passed in — _validate_local_parameters, is_eval_parameter_schema, and serialize_eval_parameters all build new result dicts rather than writing to the input. So Mapping is both correct and strictly more permissive.

The affected locations are:

  • Eval() function signature (framework.py)
  • EvalAsync() function signature (framework.py)
  • Evaluator dataclass field (framework.py)
  • EvalParameters type alias (parameters.py)

Happy to open a PR if you'd prefer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions