runtime-keypath
Supports runtime key-path recording and accessing for Python.
English | 中文
Overview
A key-path is a chain of member accesses such as a.b.c. runtime-keypath lets you capture that chain at runtime — without relying on string literals — and re-apply it later to read or write the target value.
This is useful for scenarios where you need to refer to a "location" inside an object structure, for example when building declarative APIs, validation rules, or any situation where you want to decouple what to access from how to access it.
Installation
pip install runtime-keypath
Requires Python 3.8+.
Quick Start
>>> from runtime_keypath import KeyPath
>>> class C:
... pass
>>> class B:
... def __init__(self):
... self.c = C()
>>> class A:
... def __init__(self):
... self.b = B()
>>> a = A()
>>> key_path = KeyPath.of(a.b.c)
>>> key_path.base is a
True
>>> key_path.keys == ("b", "c")
True
>>> key_path() is a.b.c
True
>>> key_path.get() is a.b.c
True
>>> key_path.unsafe_set(None)
>>> a.b.c is None
True
Features
- Minimal API — a single
KeyPathclass is all you need. - No string literals — use the natural
a.b.csyntax; the library figures out the path by inspecting bytecode. This means IDE refactorings (renaming an attribute, moving a class, etc.) work out of the box — renamectodand yourKeyPath.of(a.b.d)call updates automatically, which would be impossible if you had written"b.c"as a string.
How It Works
KeyPath.of is defined as a property on the metaclass. This makes the call KeyPath.of(a.b.c) evaluate in two distinct phases:
-
Phase 1 — capture setup (before argument evaluation) Accessing
KeyPath.offires the metaclass property getter. It inspects the caller's bytecode, locates theLOAD_*instruction that loaded the base (a), and resolves the actual object from locals/globals/builtins. It then temporarily replacesa.__class__with aKeyRecorder— a class whose__getattribute__records every attribute name and returns itself, so no real attribute values are ever read. Finally, the property returns a callable. -
Phase 2 — argument evaluation + finalization Python now evaluates
a.b.c. Sincea.__class__isKeyRecorder, each.b,.cjust appends a string to the recorded key list. The callable returned in Phase 1 is then invoked, producingKeyPath(base=a, keys=("b", "c"))and restoringa.__class__to its original value.
Because the recorder is installed and cleaned up in the same thread, single-threaded usage is safe.
API Reference
KeyPath.of(value)
Capture the member chain used to reach value.
| Parameter | Description |
|---|---|
value |
A value obtained via chained member access such as a.b.c. |
Returns a KeyPath instance representing the base object and the key sequence.
KeyPath(base, keys)
Construct a KeyPath explicitly. keys may be a tuple/list of strings or a dot-separated string like "b.c".
Properties
key_path.base— the root object from which the path starts.key_path.keys— atuple[str, ...]of member names forming the chain.
Methods
key_path.get(*, default=MISSING)— walksbasethroughkeysand returns the final value. If any key is missing anddefaultis provided, returnsdefault; otherwise raisesAttributeError.key_path()— shorthand for.get()(no default).key_path.unsafe_set(value)— walks the path up to the second-to-last key and sets the final attribute tovalue.
Warnings
Thread-safety
KeyPath.of temporarily mutates the base object's __class__ during recording. If the same object is accessed from another thread at that moment, an exception may be raised. Single-threaded usage is always safe, and this is the expected and recommended way to use the library.
Limitations
KeyPath.of(a)— with no member access at all — is not supported.KeyPath.of(some_function(a.b.c))— anything that isn't a pure member chain will raise an error.KeyPath.ofrelies on CPython bytecode details. It works on CPython 3.8+ and may work on other Python implementations, but this is not guaranteed.
Project Links
License
README drafted by Seed-Code. Reviewed by Chris Fu.
Metadata
Release files for runtime-keypath 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| runtime_keypath-1.0.0.tar.gz | 13.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| runtime_keypath-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 25.9 kB
Release files / runtime_keypath-1.0.0.tar.gz
| Download URL | runtime_keypath-1.0.0.tar.gz |
|---|---|
| Size | 13.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5d8ed41c77146ce102307a6c5992ecfd028a7d17fb9ecde58820984b1fbe41b8
|
|
BLAKE2b-256 checksum How to use checksums |
2b387e2f03450b5b6b38faf6e9a519648cc54f3a6ffc790e7752df2fbc4c3d58
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 10, 2026.
Transparency logRelease files / runtime_keypath-1.0.0-py3-none-any.whl
| Download URL | runtime_keypath-1.0.0-py3-none-any.whl |
|---|---|
| Size | 12.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6b2cc7979bdd844117844ee57e7c84e55a8deed128f64faf912d0c9d8658ef96
|
|
BLAKE2b-256 checksum How to use checksums |
8629cef47e5083cc1dcf1fe9a737340de7be05c2f155d86ae72df53c0128164f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 10, 2026.
Transparency log