Skip to main content

aod

Attr — a descriptor for instance attributes whose values are determined on demand on first access.

Installation

pip install aod

Requires Python 3.14 or later (uses lazily evaluated annotations, PEP 649).

Usage

The name of the method that will set the attribute values can be passed to the constructor. Several attributes can share the same fetch method, so a single call can populate them all at once:

import aod

class Person:
	firstname: aod.Attr[str] = aod.Attr("fetch")
	lastname: aod.Attr[str] = aod.Attr("fetch")

	def __init__(self, dbid):
		self.dbid = dbid

	def fetch(self):
		cursor = db.cursor()
		cursor.execute("select firstname, lastname from person where id=:id", id=self.dbid)
		row = cursor.fetchone()
		self.firstname = row.firstname
		self.lastname = row.lastname

p = Person(42)
print(p.firstname)   # triggers fetch, sets both firstname and lastname
print(p.lastname)    # already known, no fetch

Internally the value of an attribute foo will be stored in the instance dictionary as _foo. As long as the value hasn't been set yet, _foo will not be in the instance dictionary, and accessing the attribute will call the fetch method. The setter simply sets _foo in the instance dictionary, so the fetch method should set the attribute values via the setter.

Deleting an attribute forgets its cached value; the next access will fetch it again:

del p.firstname
print(p.firstname)   # fetches again (and refreshes lastname too)

Use Attr.known(instance) to check whether a value has already been computed without triggering a fetch.

An attribute can also be declared without a fetch method. Its value is always set explicitly (e.g. in __init__), and reading it before it has been set raises an AttributeError:

class Person:
	dbid: aod.Attr[int] = aod.Attr()
	firstname: aod.Attr[str] = aod.Attr("fetch")

	def __init__(self, dbid):
		self.dbid = dbid

For on-demand loading such an attribute is pointless, but it is declared like the others, so Attr.type, Attr.known() and code that inspects the attributes of a class (see "Types") treat it the same way. The same AttributeError is raised when the fetch method didn't set the attribute.

Note that using Attr descriptors only works for instances that have instance dictionaries.

Types

The type of an attribute can be given in the annotation (as above) or as a type parameter:

class Person:
	firstname: aod.Attr[str] = aod.Attr("fetch")   # annotation
	lastname = aod.Attr[str]("fetch")              # type parameter
	nickname = aod.Attr("fetch")                   # untyped

Both spellings tell type checkers that p.firstname is a str. Prefer the annotation: it declares the attribute, so Person.firstname is an Attr[str] and Person.firstname.known(p) type checks. With the type parameter alone, a type checker also takes the assignments in the fetch method as declarations of the attribute, so Person.lastname becomes Attr[str] | str and Person.lastname.known(p) is an error.

Attr.type returns the type of the attribute (typing.Any for an untyped one). The annotation is evaluated on the first access to Attr.type, so it may refer to classes that are defined after the class containing the attribute:

class Person:
	employer: aod.Attr[Company | None] = aod.Attr("fetch")

class Company:
	...

print(Person.employer.type)   # Company | None

Attr.type is meant for code that derives something from the attributes of a class, e.g. serialization models, documentation or forms.

License

MIT — see LICENSE.

Metadata

Release files for aod 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aod 0.4.0
File Size Uploaded Provenance Actions
aod-0.4.0.tar.gz 5.5 kB Without provenance Details for aod-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for aod 0.4.0
File Interpreter ABI Platform Actions
aod-0.4.0-py3-none-any.whl Python 3 none any Details for aod-0.4.0-py3-none-any.whl

Total release size: 11.4 kB

Release files / aod-0.4.0.tar.gz

Download URL aod-0.4.0.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
0247681db0a855a1de6e354273dd5737e1174555289c8655d701eff36c9e5e18
BLAKE2b-256 checksum
How to use checksums
fe4256c5579a0a70d7f90eec7218949775fbb89872c32f55958001d462ada339
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.10

Release files / aod-0.4.0-py3-none-any.whl

Download URL aod-0.4.0-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0851408945910591a9984afa6d492a1b5fa1f6080c73d9634380aa1793ecca1a
BLAKE2b-256 checksum
How to use checksums
145b251b09b7207c32ec62194f2e6c23f11db39be29021a4250eb10bb08b77ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.10

Security for aod 0.4.0

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page