typedobject
Define real, inheritable, and efficient Python classes using TypedDict-like
syntax.
Getting started
Install from pip.
python -m pip install typedobject
Then, annotate your class with typedobject and declare its member fields
using variable annotations.
from typedobject import typedobject
@typedobject
class Point:
x: int
y: int
Then construct the object using either positional or keyword arguments.
pt1 = Point(10, 20)
pt2 = Point(x=10, y=20)
assert pt1 == pt2
You can define methods, including __init__.
@typedobject
class Rectangle:
pt1: Point
pt2: Point
def __init__(self, x1, y1, x2, y2):
self.pt1 = Point(x1, y1)
self.pt2 = Point(x2, y2)
def area(self):
return (self.pt2.x - self.pt1.x) * (self.pt2.y - self.pt1.y)
rect = Rectangle(1, 1, 3, 3)
assert rect.area() == 4
Unlike TypedDict, the defined classes are actually fresh new classes,
not dicts.
assert isinstance(rect, Rectangle)
assert not isinstance(rect, Point)
Inheritance works just fine.
@typedobject
class RoundedRect(Rectangle):
corner_radius: int
rr = RoundedRect(Point(1, 1), Point(3, 3), 1)
Note that the derived class overrides __init__, unless you use .no_init.
@typedobject.no_init
class RoundedRect2(Rectangle):
corner_radius: int
rr = RoundedRect2(1, 1, 3, 3)
assert not hasattr(rr, 'corner_radius')
Typed object classes, unlike dataclass, specify __slots__, which makes
access to attributes faster and makes objects take less memory, but prevents
adding new fields dynamically.
They also prevent multiple inheritance.
# raises TypeError
class RectangleWithAPoint(Rectangle, Point):
pass
You can, however, inject mixins.
class TwoDObjectMixin:
def area(self):
return self.width() * self.height()
@typedobject
class Rectangle2(TwoDObjectMixin):
pt1: Point
pt2: Point
Metadata
Release files for typedobject 1.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| typedobject-1.1.1.tar.gz | 7.9 kB | Without provenance | Details for typedobject-1.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| typedobject-1.1.1-py3-none-any.whl | Python 3 | none | any | Details for typedobject-1.1.1-py3-none-any.whl |
Total release size: 14.8 kB
Release files / typedobject-1.1.1.tar.gz
| Download URL | typedobject-1.1.1.tar.gz |
|---|---|
| Size | 7.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7881c7d5994be8b3c5ec2e774ea1d34aa35c8e3787fe352e04b0b8c4a35dee66
|
|
BLAKE2b-256 checksum How to use checksums |
cc4679d1674c0adfbc9846b78c05c194cd6bca9c79a807a31785fc5770445c60
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2
|
Release files / typedobject-1.1.1-py3-none-any.whl
| Download URL | typedobject-1.1.1-py3-none-any.whl |
|---|---|
| Size | 6.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e7ff5532c90976c0572a831089d6c9e32c804a10d9d9a49b801258131964d0d2
|
|
BLAKE2b-256 checksum How to use checksums |
8548846f27baec3446a51f0a694dde9c97ce6cc0f14fb700af2fadeebe83ea2d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2
|
Security for typedobject 1.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.