feat(orm): modelos POO con anotaciones tipadas (Fase 1.5)#7
Merged
Merged
Conversation
Añade un ORM ligero sobre el `QueryBuilder` ya existente. La identidad
del producto: declarar un modelo es declarar tipos Python, no DSL.
API
---
```python
class User(Model):
__table__ = "users"
id: int = fields.PrimaryKey()
name: str
email: str = fields.String(unique=True)
age: int | None = None
settings: dict = fields.Json(default_factory=dict)
created_at: datetime = fields.DateTime(default_now=True)
shiba.set_default_connection(cx)
User.create_table()
user = User(name="John", email="j@x.com")
user.save() # INSERT, hidrata pk con LAST_INSERT_ID
User.find(1) # → User | None
User.where("age", ">", 18).order_by("created_at", "DESC").get() # → list[User]
```
Componentes
- `shiba/orm/fields.py` — `Field` + subclases semánticas
(PrimaryKey, String, Integer, BigInteger, Boolean, Text, Json,
DateTime, DateField, DecimalField, Enum, ForeignKey) y motor de
inferencia desde anotaciones (`int | None` → nullable, `dict` → JSON).
- `shiba/orm/model.py` — `Model` con metaclass que lee anotaciones por
MRO usando `inspect.get_annotations(eval_str=True)` para soportar
`from __future__ import annotations`. `ModelQuery[T]` hidrata las
filas del builder a instancias del modelo. Conexión global vía
`set_default_connection()` o override por clase con `__db__`.
Cambios menores
- `ErrorCode.raise_` se tipa como `NoReturn`, eliminando varias
estructuras unreachable que mypy no entendía.
- Re-exports en `shiba/__init__.py`: `Model`, `fields`,
`set_default_connection`.
Tests
- 16 tests nuevos cubren inferencia de tipos, save (INSERT/UPDATE),
delete, find, where con hidratación, JSON roundtrip, create_table.
- Total: 100/100. Lint y mypy estricto verde.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This was referenced May 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resumen
ORM ligero sobre el
QueryBuilderde #6. Estilo híbrido: las anotaciones Python dan el tipo,fields.X(...)añade metadata extra cuando hace falta.Highlights
shiba/orm/fields.pycon 12 subclases (PrimaryKey, String, Integer, Boolean, Json, DateTime, DecimalField, Enum, ForeignKey, ...) y motor de inferencia que entiendeT | None,dict,list, builtins ydatetime.Modelcon metaclass que lee anotaciones por MRO coninspect.get_annotations(eval_str=True)— soportafrom __future__ import annotations.ModelQuery[T]que delega alQueryBuildery hidrata cada fila al modelo.set_default_connection(cx)) o por clase (__db__).ErrorCode.raise_ahora se tipa comoNoReturn.Test plan
ruffymypy --strictlimpios🤖 Generated with Claude Code