---
name: coerce_types
kind: function
lang: py
domain: core
version: "1.0.0"
purity: pure
signature: "def coerce_types(data: dict, schema: dict[str, str]) -> tuple[dict, list[str]]"
description: "Convierte valores de un dict a los tipos esperados segun un schema declarativo. Soporta int, float, str, bool, datetime, list[str]. Util para normalizar datos de CSV, JSON o query params. Nunca muta el original. Coerciones imposibles generan warning y mantienen el valor original."
tags: [coercion, types, normalization, pure, core, csv, json, pendiente-usar]
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: ""
imports: [datetime]
params:
  - name: data
    desc: "dict con valores a coercionar (tipicamente strings)"
  - name: schema
    desc: "dict con campos como keys y tipos esperados como values (int, float, str, bool, datetime, list[str])"
output: "tupla (resultado_coercionado, lista_de_warnings)"
tested: true
tests:
  - "string 42 a int 42"
  - "string 3.14 a float 3.14"
  - "string true a bool true"
  - "string iso8601 a datetime"
  - "coercion fallida genera warning sin crash"
  - "dict con mix de tipos ya correctos y strings"
  - "campo ausente en schema pass through sin tocar"
  - "string lista a list str"
test_file_path: "python/functions/core/coerce_types_test.py"
file_path: "python/functions/core/coerce_types.py"
---

## Ejemplo

```python
data = {"age": "25", "score": "9.5", "active": "yes", "tags": "go, python"}
schema = {"age": "int", "score": "float", "active": "bool", "tags": "list[str]"}

result, warnings = coerce_types(data, schema)
# result = {"age": 25, "score": 9.5, "active": True, "tags": ["go", "python"]}
# warnings = []

# Coercion fallida — mantiene original y avisa
result2, warnings2 = coerce_types({"n": "abc"}, {"n": "int"})
# result2 = {"n": "abc"}
# warnings2 = ["n: cannot coerce 'abc' to int: could not convert string to float: 'abc'"]
```

## Notas

Funcion pura. Solo usa `datetime` de la stdlib. No muta el dict original — retorna uno nuevo. Schema es flat (no anidado); para validacion de estructura compleja combinar con `validate_json_schema`. Lossy coercions (float "3.7" → int 3) generan warning adicional. Campo ausente en schema se copia sin tocar.
