Skip to main content

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.

Translations from pydal to typedal

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

db = DAL(...)
from typedal import TypeDAL, TypedTable, TypedField
from 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: str
    otherfield: 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
row = db.table_name(id=1)  # -> Any
row: TableName = db.table_name(id=1)  # -> TableName
row = TableName(id=1)  # -> TableName

All Types

pydal typedal (native python type) typedal (using TypedField) typedal (using specific Field)
Field('name', 'string') name: str name: TypedField(str) name: StringField()
Field('name', 'text') × name: TypedField(str, type="text") name: TextField()
Field('name', 'blob') name: bytes name: TypedField(bytes) name: BlobField()
Field('name', 'boolean') name: bool name: TypedField(bool) name: BooleanField()
Field('name', 'integer') name: int name: TypedField(int) name: IntegerField()
Field('name', 'double') name: float name: TypedField(float) name: DoubleField()
Field('name', 'decimal(n,m)') name: decimal.Decimal name: TypedField(decimal.Decimal) name: DecimalField(n=n, m=m)
Field('name', 'date') name: datetime.date name: TypedField(datetime.date) name: DateField()
Field('name', 'time') name: datetime.time name: TypedField(datetime.time) name: TimeField()
Field('name', 'datetime') name: datetime.datetime name: TypedField(datetime.datetime) name: DatetimeField()
Field('name', 'password') × name: TypedField(str, type="password") name: PasswordField()
Field('name', 'upload') × name: TypedField(str, type="upload) name: UploadField()
Field('name', 'reference <table>') name: Table name: TypedField(Table) name: ReferenceField('table')
Field('name', 'list:string') name: list[str] name: TypedField(list[str]) name: ListStringField()
Field('name', 'list:integer') name: list[int] name: TypedField() name: ListIntegerField()
Field('name', 'list:reference <table>') name: list[Table] name: TypedField() name: ListReferenceField('table')
Field('name', 'json') × name: TypedField() name: JSONField()
Field('name', 'bigint') × name: TypedField() name: BigintField()
Field('name', 'big-id') × × ×
Field('name', 'big-reference') × × ×

Making a field required/optional

pydal typedal (native python type) typedal (using TypedField) typedal (using specific Field)
Field('name', 'string', required=True) name: str name: TypedField(str, required=True) name: StringField(required=True)
Field('name', 'text', required=False) name: typing.Optional[str] or
name: str | None
name: TypedField(str, type="text", required=False) name: StringField(required=False)

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

0.4.0

Download files

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

Source Distribution

TypeDal-0.4.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

TypeDal-0.4.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file TypeDal-0.4.0.tar.gz.

File metadata

  • Download URL: TypeDal-0.4.0.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for TypeDal-0.4.0.tar.gz
Algorithm Hash digest
SHA256 e0cb679dec319feb2a1f2139d7966f22401b617287f4581060967d6978b7a3db
MD5 2f09be69375283a42438fadf0cd085b6
BLAKE2b-256 94d3e1f16aab68d240cf97914ed7d8a46f2d4e419628128fae624aa8a572e0c4

See more details on using hashes here.

Provenance

File details

Details for the file TypeDal-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: TypeDal-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for TypeDal-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 42273d08080833e22bda4cfdbb7771a0fd44a254be55d4bc81b0a6845f586246
MD5 55870510a65156828067bb205c7634f1
BLAKE2b-256 bfbc4c8c232314b7731b88a02f12c544f26b7a9acd46c913e91c2a10c5ccad56

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