parent-lookup
Table of Contents
parent-lookup is a utility Python package enabling a child object to dynamically lookup its parent at runtime.
It comprises of a small set of decorators and methods allowing bidirectional parent-child relationships without hard-coded circular dependencies or circular imports.
Installation
pip install parent-lookup
Usage
API:
from parent_lookup import TParent, is_child_lookup, lookup_registry
class Child:
def __init__(self) -> None:
pass
@overload
def find_parent(self, parent_type: type[TParent]) -> TParent | None:
pass
@overload
def find_parent(self, parent_type: TParent) -> TParent | None:
pass
def find_parent(self, parent_type: type[TParent] | TParent) -> TParent | None:
return lookup_registry.lookup_parent(self, parent_type)
class Parent:
def __init__(self) -> None:
self._childs: list[Child] = []
def __new__(
cls,
) -> Parent:
instance = super().__new__(cls)
lookup_registry.register_parent(instance)
return instance
def add_child(self, child: Child) -> None:
self._childs.append(child)
@property
@is_child_lookup
def childs(self) -> list[Child]:
return self._childs
# Create two objects: A parent object and a child object
parent = Parent()
child = Child()
# Add child to parent
parent.add_child(child)
# Lookup parent from child
found_parent = child.find_parent(Parent)
assert found_parent is parent
Descriptor API (optional alternative):
from parent_lookup import ParentLookup, is_child_lookup, lookup_registry
class Parent:
def __init__(self) -> None:
self._childs: list[Child] = []
def __new__(cls) -> Parent:
instance = super().__new__(cls)
lookup_registry.register_parent(instance)
return instance
def add_child(self, child: Child) -> None:
self._childs.append(child)
@property
@is_child_lookup
def childs(self) -> list[Child]:
return self._childs
class Child:
parent: ParentLookup[Parent] = ParentLookup(Parent)
parent = Parent()
child = Child()
parent.add_child(child)
assert child.parent is parent
For more examples and usage, please refer to parent-lookup's documentation.
Development Setup
1. Install uv
This project uses uv as package manager.
If you haven't already, install uv, preferably using it's "Standalone installer" method:
..on Windows:
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
..on MacOS and Linux:
curl -LsSf https://astral.sh/uv/install.sh | sh
(see docs.astral.sh/uv for all / alternative installation methods.)
Once installed, you can update uv to its latest version, anytime, by running:
uv self update
2. Clone the repository
Clone the parent-lookup repository into your local development directory:
git clone https://github.com/ClaasRostock/parent-lookup path/to/your/dev/parent-lookup
Change into the project directory after cloning:
cd parent-lookup
3. Install dependencies
Run uv sync -U to create a virtual environment and install all project dependencies into it:
uv sync -U
Note: Using
--no-devwill omit installing development dependencies.
Explanation: The
-Uoption stands for--update. It forcesuvto fetch and install the latest versions of all dependencies, ensuring that your environment is up-to-date.
Note:
uvwill create a new virtual environment called.venvin the project root directory when runninguv sync -Uthe first time. Optionally, you can create your own virtual environment using e.g.uv venv, before runninguv sync -U.
4. (Optional) Activate the virtual environment
When using uv, there is in almost all cases no longer a need to manually activate the virtual environment.
uv will find the .venv virtual environment in the working directory or any parent directory, and activate it on the fly whenever you run a command via uv inside your project folder structure:
uv run <command>
However, you still can manually activate the virtual environment if needed.
When developing in an IDE, for instance, this can in some cases be necessary depending on your IDE settings.
To manually activate the virtual environment, run one of the "known" legacy commands:
..on Windows:
.venv\Scripts\activate.bat
..on Linux:
source .venv/bin/activate
5. Install pre-commit hooks
The .pre-commit-config.yaml file in the project root directory contains a configuration for pre-commit hooks.
To install the pre-commit hooks defined therein in your local git repository, run:
uv run pre-commit install
All pre-commit hooks configured in .pre-commit-config.yaml will now run each time you commit changes.
pre-commit can also manually be invoked, at anytime, using:
uv run pre-commit run --all-files
To skip the pre-commit validation on commits (e.g. when intentionally committing broken code), run:
uv run git commit -m <MSG> --no-verify
To update the hooks configured in .pre-commit-config.yaml to their newest versions, run:
uv run pre-commit autoupdate
6. Test that the installation works
To test that the installation works, run pytest in the project root folder:
uv run pytest
Meta
Copyright (c) 2026 Claas Rostock. All rights reserved.
Claas Rostock - @LinkedIn - claas.rostock@dnv.com
Distributed under the MIT license. See LICENSE for more information.
https://github.com/ClaasRostock/parent-lookup
Contributing
- Fork it (https://github.com/ClaasRostock/parent-lookup/fork)
- Create an issue in your GitHub repo
- Create your branch based on the issue number and type (
git checkout -b issue-name) - Evaluate and stage the changes you want to commit (
git add -i) - Commit your changes (
git commit -am 'place a descriptive commit message here') - Push to the branch (
git push origin issue-name) - Create a new Pull Request in GitHub
For your contribution, please make sure you follow the STYLEGUIDE before creating the Pull Request.
Metadata
Release files for parent-lookup 0.2.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| parent_lookup-0.2.4.tar.gz | 142.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| parent_lookup-0.2.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 153.7 kB
Release files / parent_lookup-0.2.4.tar.gz
| Download URL | parent_lookup-0.2.4.tar.gz |
|---|---|
| Size | 142.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6b3eb9d6e479754e5fcbf85eb89f45afc341d4aeb69a990b8d7fd10ef296ed1c
|
|
BLAKE2b-256 checksum How to use checksums |
b5a399241d734863bfae9bc97aecca283f4d7b71c2dbb5beb9b7359b4f5df053
|
| 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 15, 2026.
Transparency logRelease files / parent_lookup-0.2.4-py3-none-any.whl
| Download URL | parent_lookup-0.2.4-py3-none-any.whl |
|---|---|
| Size | 10.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ce017f2bc4f160332cee4cf10819acfb7c2047c28f07e33907d7d42d00071af5
|
|
BLAKE2b-256 checksum How to use checksums |
a43bf97c3d73ad8f350b08d2bc5b8c6916d483ff06a76b72f7ce29ee07b1758b
|
| 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 15, 2026.
Transparency log