Skip to content

Override tries to cast to typing.Any #161

@gazpachoking

Description

@gazpachoking

I'm trying to override types in sqlite to store and retrieve dates as whenever.Instant. I declare the type in the schema as JULIANDAY, (which properly gets the 'real' affinity as the database storage type,) and have an override in my config so that that column type gets associated with whenever.Instant. The problem is for functions where it's writing to the database it tries to cast the whenever.Instant to typing.Any, which isn't valid, and explicitly casting the row from the db result to whenever.Instant, which I'm already handling at the db level. I have set up sqlite with an adapter and converter so that it properly stores and loads the julian days into the whenever.Instant, but the cast is blocking the sqlc generated functions from working properly. Is there a way around that, or is it just a bug?
sqlc config

sql:
  - engine: "sqlite"
    schema: "db/migrations"
    queries: "db/queries"
    codegen:
      - out: "fsi/dbgen"
        plugin: python
        options:
          package: "fsi.dbgen"
          emit_init_file: true
          sql_driver: aiosqlite
          model_type: msgspec
          overrides:
            - db_type: "JULIANDAY"
              py_type:
                import: "whenever"
                type: "whenever.Instant"

db schema

CREATE TABLE facilities (
    id INTEGER PRIMARY KEY,
    name TEXT,
    updated_at JULIANDAY
);

generated function (this is where I'm having trouble)

async def create_facility(conn: aiosqlite.Connection, *, name: str | None, created_at: whenever.Instant | None) -> models.Facility | None:
    # The cast here is just invalid
    row = await (await conn.execute(CREATE_FACILITY, (name, typing.Any(created_at)))).fetchone()
    if row is None:
        return None
    # the cast here may be desired sometimes, but it's being handled by the sqlite driver in my case
    return models.Facility(id=row[0], name=row[1], updated_at=whenever.Instant(row[2]))

The model comes out fine

class Facility(msgspec.Struct):
    id: int
    name: str | None
    created_at: whenever.Instant | None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions