Skip to main content

runtime-keypath

PyPI - Python Version License

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 KeyPath class is all you need.
  • No string literals — use the natural a.b.c syntax; 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 — rename c to d and your KeyPath.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:

  1. Phase 1 — capture setup (before argument evaluation) Accessing KeyPath.of fires the metaclass property getter. It inspects the caller's bytecode, locates the LOAD_* instruction that loaded the base (a), and resolves the actual object from locals/globals/builtins. It then temporarily replaces a.__class__ with a KeyRecorder — 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.

  2. Phase 2 — argument evaluation + finalization Python now evaluates a.b.c. Since a.__class__ is KeyRecorder, each .b, .c just appends a string to the recorded key list. The callable returned in Phase 1 is then invoked, producing KeyPath(base=a, keys=("b", "c")) and restoring a.__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 — a tuple[str, ...] of member names forming the chain.

Methods

  • key_path.get(*, default=MISSING) — walks base through keys and returns the final value. If any key is missing and default is provided, returns default; otherwise raises AttributeError.
  • 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 to value.

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.of relies on CPython bytecode details. It works on CPython 3.8+ and may work on other Python implementations, but this is not guaranteed.

License

MIT


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)

Source distribution for runtime-keypath 1.0.0
File Size Uploaded
runtime_keypath-1.0.0.tar.gz 13.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for runtime-keypath 1.0.0
File Interpreter ABI Platform
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 log

Release 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

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

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