Typing support for PyDAL
Project description
TypeDAL
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")
)TypedRows
: can be used as the return type of .select() and subscribed with the actual table class, so e.g.rows: TypedRows[SomeTable]
. If you're lazy,list[SomeTable]
works fine too but that misses hinting possibilities such as.first()
.
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 | pydal alternative | typedal | typedal alternative(s) | ... |
Setup |
from pydal import DAL, Field
db = DAL(...)
|
from src.typedal import TypeDAL, TypedTable, TypedField, TypedRows
from src.typedal.fields import TextField
from typing import Optional
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")
|
db.table_name.insert(fieldname="value")
|
TableName.insert(fieldname="value")
|
||
(quick) Select |
rows = db(db.table_name).select() # -> Any (Rows)
row = db.table_name(id=1) # -> Any (Row)
|
rows: TypedRows[TableName] = db(db.table_name).select() # -> TypedRows[TableName]
row: TableName = db.table_name(id=1) # -> TableName
|
rows: TypedRows[TableName] = db(TableName).select() # -> TypedRows[TableName]
row = TableName(id=1) # -> TableName
|
All Types
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 setrequired=False
as an argument yourself. This is also a reason whytyping.get_type_hints
is not a solution for the first caveat.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file typedal-2.0.0b6.tar.gz
.
File metadata
- Download URL: typedal-2.0.0b6.tar.gz
- Upload date:
- Size: 1.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.25.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d6e53015c95cd19affc63d96c8783a4c0092d4c96b47d0b85f2a0f05efbec87e |
|
MD5 | 9652d15fbd210587ce561666c0715955 |
|
BLAKE2b-256 | f0727a952b60e138b501a5d8662f698c850edb6340a936442e586c31505442f5 |
Provenance
File details
Details for the file typedal-2.0.0b6-py3-none-any.whl
.
File metadata
- Download URL: typedal-2.0.0b6-py3-none-any.whl
- Upload date:
- Size: 29.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.25.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a180f019a6901438cc3d34b76df4849cf43f4bc7ebcb158ba7a7fea0dd9cc94 |
|
MD5 | 9516a338c1c2edd1a86dfe8535bc0e74 |
|
BLAKE2b-256 | 46546142152c6b3f674cfca42908f9650b8f5d2e6a5a2d2f314d3cfc56870bdb |