Skip to main content

Typing support for PyDAL

Project description

TypeDAL

PyPI - Version PyPI - Python Version
Code style: black License: MIT
su6 checks coverage.svg

Typing support for PyDAL. This package aims to improve the typing support for PyDAL. By using classes instead of the define_table method, type hinting the result of queries can improve the experience while developing. In the background, the queries are still generated and executed by pydal itself, this package only proves some logic to properly pass calls from class methods to the underlying db.define_table pydal Tables.

  • TypeDAL is the replacement class for DAL that manages the code on top of DAL.
  • TypedTable must be the parent class of any custom Tables you define (e.g. class SomeTable(TypedTable))
  • TypedField can be used instead of Python native types when extra settings (such as default) are required ( e.g. name = TypedField(str, default="John Doe")). It can also be used in an annotation (name: TypedField[str]) to improve editor support over only annotating with str.
  • TypedRows: can be used as the return type annotation of pydal's .select() and subscribed with the actual table class, so e.g. rows: TypedRows[SomeTable] = db(...).select(). When using the QueryBuilder, a TypedRows instance is returned by .collect().

Version 2.0 also introduces more ORM-like funcionality. Most notably, a Typed Query Builder that sees your table classes as models with relationships to each other. See 3. Building Queries for more details.

Quick Overview

Below you'll find a quick overview of translation from pydal to TypeDAL. For more info, see the docs.

Translations from pydal to typedal

Description pydal typedal typedal alternative(s) ...
Setup
from pydal import DAL, Field

db = DAL(...)
from typedal import TypeDAL, TypedTable, TypedField

db = TypeDAL(...)
Table Definitions
db.define_table("table_name",
                Field("fieldname", "string", required=True),
                Field("otherfield", "float"),
                Field("yet_another", "text", default="Something")
                )
@db.define
class TableName(TypedTable):
    fieldname: str
    otherfield: float | None
    yet_another = TypedField(str, type="text", default="something", required=False)
import typing


class TableName(TypedTable):
    fieldname: TypedField[str]
    otherfield: TypedField[typing.Optional[float]]
    yet_another = TextField(default="something", required=False)


db.define(TableName)
Insert
db.table_name.insert(fieldname="value")
TableName.insert(fieldname="value")
# the old syntax is also still supported:
db.table_name.insert(fieldname="value")
(quick) Select
# all:
all_rows = db(db.table_name).select()  # -> Any (Rows)
# some:
rows = db((db.table_name.id > 5) & (db.table_name.id < 50)).select(db.table_name.id)
# one:
row = db.table_name(id=1)  # -> Any (Row)
# all:
all_rows = TableName.collect()  # or .all()
# some:
# order of select and where is interchangable here
rows = TableName.select(Tablename.id).where(TableName.id > 5).where(TableName.id < 50).collect()
# one:
row = TableName(id=1)  # or .where(...).first()
# you can also still use the old syntax and type hint on top of it;
# all:
all_rows: TypedRows[TableName] = db(db.table_name).select()
# some:
rows: TypedRows[TableName] = db((db.table_name.id > 5) & (db.table_name.id < 50)).select(db.table_name.id)
# one:
row: TableName = db.table_name(id=1)

All Types

See 2. Defining Tables

Roadmap

This section contains a non-exhaustive list of planned features for future feature releases:

  • 2.1
    • Caching: adding a .cache() operation to the query builder which loads a repeated execution from cache (via pickling of the final object). Optionally, dependency tracking could be added to automatically expire cache items when one of the underlying objects have changed in the database.
  • 2.2
    • Migrations: currently, you can use pydal's automatic migrations or disable those and manage them yourself, but adding something like edwh-migrate with pydal2sql as an option could make this project more production-friendly.

Caveats

  • This package depends heavily on the current implementation of annotations (which are computed when the class is defined). PEP 563 (Postponed Evaluation of Annotations, accepted) aims to change this behavior ( and from __future__ import annotations already does) in a way that this module currently can not handle: all annotations are converted to string representations. This makes it very hard to re-evaluate the annotation into the original type, since the variable scope is lost (and thus references to variables or other classes are ambiguous or simply impossible to find).
  • TypedField limitations; Since pydal implements some magic methods to perform queries, some features of typing will not work on a typed field: typing.Optional or a union (Field() | None) will result in errors. The only way to make a typedfield optional right now, would be to set required=False as an argument yourself. This is also a reason why typing.get_type_hints is not a solution for the first caveat.

Project details


Release history Release notifications | RSS feed

This version

2.0.2

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

typedal-2.0.2.tar.gz (1.2 MB view details)

Uploaded Source

Built Distribution

typedal-2.0.2-py3-none-any.whl (30.6 kB view details)

Uploaded Python 3

File details

Details for the file typedal-2.0.2.tar.gz.

File metadata

  • Download URL: typedal-2.0.2.tar.gz
  • Upload date:
  • Size: 1.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.25.0

File hashes

Hashes for typedal-2.0.2.tar.gz
Algorithm Hash digest
SHA256 09b551a73c36fafc07cfbf82525cfee53e0e30aa5984c8c3833cface2e075ba2
MD5 ff508d5424f0840fc7e57971c8b070c3
BLAKE2b-256 8d0325594e629ed648934b91f91a5110f18d5843ec6d2dace71afa82fa79e6ab

See more details on using hashes here.

Provenance

File details

Details for the file typedal-2.0.2-py3-none-any.whl.

File metadata

  • Download URL: typedal-2.0.2-py3-none-any.whl
  • Upload date:
  • Size: 30.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.25.0

File hashes

Hashes for typedal-2.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fbf6644d3d531cd5b6cf53ca4baed0515f087622dc154d7da6f4a86681917cf6
MD5 0951d3cb6efa089bc63c07b065c2d00c
BLAKE2b-256 ff572f9ba1d177570b3839badd2adc53485897e71027f055ac265e4b597aab0b

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page