Add on_use_handlers dict field for backward compatibility with cogames - #1
Add on_use_handlers dict field for backward compatibility with cogames#1aaln wants to merge 1 commit into
Conversation
cogames passes on_use_handlers as a dict[str, Handler] in many places (extractors, hubs, junctions, cognitive substrate evals, overcogged). The standalone mettagrid repo only has the singular on_use_handler field, causing a pydantic ValidationError on import. Adds on_use_handlers as a convenience field that gets merged into on_use_handler via firstMatch() in the model validator. Made-with: Cursor
|
This PR adds backward compatibility for the on_use_handlers dict API that cogames relies on extensively. The standalone mettagrid repo introduced composite handler types (FirstMatch/AllOf) and replaced on_use_handlers: dict[str, Handler] with on_use_handler: AnyHandler | None. However, cogames still uses the dict form in ~20+ places (extractors, hubs, junctions, cognitive substrate evals, overcogged, gear stations). This causes a pydantic ValidationError: Extra inputs are not permitted on any cogames import. The fix is 8 lines: it adds on_use_handlers back as a convenience field and merges it into on_use_handler via firstMatch() in the existing model validator. Single-entry dicts collapse to a plain Handler, multi-entry dicts become a FirstMatch composite, and if both fields are provided they merge correctly. No behavior change for anyone already using the singular on_use_handler API. |
Summary
on_use_handlers: dict[str, Handler]field toGridObjectConfigfor backward compatibility with cogameson_use_handlerfield viafirstMatch()in the existing model validatorpydantic_core._pydantic_core.ValidationError: Extra inputs are not permittedforon_use_handlersContext
The standalone mettagrid repo replaced
on_use_handlers(dict) withon_use_handler(singularAnyHandler | None) using compositeFirstMatch/AllOfhandler types. However, cogames extensively uses the dict form (on_use_handlers={...}) across extractors, hubs, junctions, cognitive substrate evals, and overcogged game configs.This adds the dict field back as a convenience that bridges to the new singular API — the model validator merges the dict entries into a
FirstMatchcomposite, preserving the same runtime behavior.Test plan
GridObjectConfig(on_use_handlers={"solve": Handler()})correctly merges intoon_use_handlerHandler(viafirstMatchflattening)on_use_handlerasNoneon_use_handlerandon_use_handlersmerge correctlyAgentConfig(inherits fromGridObjectConfig) also worksMade with Cursor