Skip to main content

Easy declaration of data structures with runtime type checking

Project description

easytypes

Easy typed structures for Python with optional field checking and runtime type checking. Based on class decorators and typeguard package for type checking.

What does the package implement

from easytypes import easy_type_decorator, safe_type, fast_type, unlimited_type
from easytypes import EasyTypesException
from easytypes import AttributeRequired, AttributeNotAllowed, AttributeTypeError
from easytypes import Required
from easytypes import disable_all_checks, enable_all_checks

How to define a simple data class

from easytypes import easy_type_decorator, safe_type, fast_type, unlimited_type
from easytypes import EasyTypesException
from easytypes import AttributeRequired, AttributeNotAllowed, AttributeTypeError
from easytypes import Required
from easytypes import disable_all_checks, enable_all_checks

@safe_type  # You cannot: add new attrs; not set (or delete) the required attrs; assign wrong types
class A:
    a: int  # Type checking, not required, no default value
    b: str = '5'  # Type checking, not required, the default value is '5'
    c: float = Required()  # Type checking, required, no default value
    d: Tuple[int, int] = Required(2, 3)  # Type checking, required, the default value is (2, 3)

...and how does the class work

va = A(a=2, b='b', c=1.0, d=(2,4))  # Ok
assert va.a == 2
va.a = 5
assert va.a == 5
va = A(c=1.0)  # Ok, using default values
va = A(c=1.0, q=2)  # raises AttributeNotAllowed 
va.q = 0  # raises AttributeNotAllowed
va.a = '5'  # raises AttributeTypeError (a should be int)
va = A(c='c')  # raises AttributeTypeError (c should be float)
va = (b='c')  # raises AttributeRequired (a is missing)

How to extend a class

@safe_type
class B(A):
    e: str = 5  # Is added to the fields defined in A

...and how to redefine a field

@safe_type
class C(A):
    a: int = 5  # Not recomended and not prohibited

How to disable all the checks at runtime

disable_all_checks()  # Does not affect all the previously created classes, so put it before the class declaration

...and how to re-enable them

enable_all_checks()  # Does not affect all the previously created classes, so put it before the class declaration

The other class decorators

unlimited_type  # Allows to add the attributes that are not declared at the class level
fast_type  # No checks at all, just a constructor implemetation accepting the attributes

...and how to get a customized decorator

easy_type_decorator(chk_required=True, chk_allowed=True, chk_types=True)  # Allows to activate only the desired checks

Project details


Download files

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

Source Distribution

easytypes-0.1.4.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

easytypes-0.1.4-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file easytypes-0.1.4.tar.gz.

File metadata

  • Download URL: easytypes-0.1.4.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for easytypes-0.1.4.tar.gz
Algorithm Hash digest
SHA256 37d287073731f917fedd76c41bfec6edd1abeaf0edd7a5d7517b5191a110baf0
MD5 29a4a54bfd9143873190f9c57ac7e6fe
BLAKE2b-256 872e8f87e01dd0b10d807e9c0c56fa4f6e4c0f9499a8b44ae0fb85fe8671ab56

See more details on using hashes here.

File details

Details for the file easytypes-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: easytypes-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for easytypes-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 88d510891a0c6d828dfd7f5630e43f70a9c2046a03f0f70a6e4822c57a347b4a
MD5 ebd1ffa264f232f238ef60887cf9e898
BLAKE2b-256 62cb0885457ef8f7329d90b3d0f25cd505d6b3a971e3beb8dc506f4ca0b92e34

See more details on using hashes here.

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