Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explore the intersection between sqlite-utils and dataclasses #562

Open
simonw opened this issue Jul 2, 2023 · 2 comments
Open

Explore the intersection between sqlite-utils and dataclasses #562

simonw opened this issue Jul 2, 2023 · 2 comments
Labels

Comments

@simonw
Copy link
Owner

simonw commented Jul 2, 2023

Aside: this makes me think it might be cool if sqlite-utils had a way of working with dataclasses rather than just dicts, and knew how to create a SQLite table to match a dataclass and maybe how to code-generate dataclasses for a specific table schema (dynamically or even using code-generation that can be written to disk, for better editor integrations).

Originally posted by @simonw in simonw/llm#65 (comment)

@simonw simonw added the research label Jul 2, 2023
@simonw
Copy link
Owner Author

simonw commented Jul 2, 2023

Dataclasses were added in Python 3.7 and sqlite-utils was originally written for Python 3.6 - but both 3.6 and 3.7 are EOL now.

The thing that makes Dataclasses particularly interesting is the potential to use type annotations with them to help specify the types of the related SQLite columns.

Example for https://datasette.io/content/users

CREATE TABLE [users] (
   [login] TEXT,
   [id] INTEGER PRIMARY KEY,
   [node_id] TEXT,
   [avatar_url] TEXT,
   [gravatar_id] TEXT,
   [html_url] TEXT,
   [type] TEXT,
   [site_admin] INTEGER,
   [name] TEXT
);

And the dataclass:

from dataclasses import dataclass

@dataclass
class User:
    id: int
    login: str
    node_id: str
    avatar_url: str
    gravatar_id: str
    html_url: str
    type: str
    site_admin: int
    name: str

@tobych
Copy link

tobych commented Apr 29, 2024

I'd suggest not bothering with this, because so many choose Pydantic over Python's built-in dataclasses.

And there are a few packages that help with that already:

So sure, it's not dataclasses, but big picture this would duplicated the efforts of others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants