Python package enabling a child object to dynamically lookup its parent at runtime.
Project description
parent-lookup
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 Example
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
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. Install Python
This project requires Python 3.10 or later.
If you don't already have a compatible version installed on your machine, the probably most comfortable way to install Python is through uv:
uv python install
This will install the latest stable version of Python into the uv Python directory, i.e. as a uv-managed version of Python.
Alternatively, and if you want a standalone version of Python on your machine, you can install Python either via winget:
winget install --id Python.Python
or you can download and install Python from the python.org website.
3. 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
4. Install dependencies
Run uv sync to create a virtual environment and install all project dependencies into it:
uv sync
Note: Using
--no-devwill omit installing development dependencies.
Note:
uvwill create a new virtual environment called.venvin the project root directory when runninguv syncthe first time. Optionally, you can create your own virtual environment using e.g.uv venv, before runninguv sync.
6. (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
7. 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
8. Test that the installation works
To test that the installation works, run pytest in the project root folder:
uv run pytest
Meta
Copyright (c) 2025 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.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file parent_lookup-0.1.0.tar.gz.
File metadata
- Download URL: parent_lookup-0.1.0.tar.gz
- Upload date:
- Size: 16.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6bef9b74abdfcd181652c2b1f698fc1313beaf4642e777ee5deed438b04a75b1
|
|
| MD5 |
b3046cb14772ed006cdfdc6d1e9c8590
|
|
| BLAKE2b-256 |
babf363a95a786f50f0b3080e689fd9487fb835f4e40da979c986d278c96580b
|
Provenance
The following attestation bundles were made for parent_lookup-0.1.0.tar.gz:
Publisher:
publish_release.yml on ClaasRostock/parent-lookup
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
parent_lookup-0.1.0.tar.gz -
Subject digest:
6bef9b74abdfcd181652c2b1f698fc1313beaf4642e777ee5deed438b04a75b1 - Sigstore transparency entry: 168292735
- Sigstore integration time:
-
Permalink:
ClaasRostock/parent-lookup@65b58a071dda2f1c433a1ef50e57889a2c48f1e4 -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/ClaasRostock
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_release.yml@65b58a071dda2f1c433a1ef50e57889a2c48f1e4 -
Trigger Event:
push
-
Statement type:
File details
Details for the file parent_lookup-0.1.0-py3-none-any.whl.
File metadata
- Download URL: parent_lookup-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c1d5441cca03b1b9624f3c2a4c1b18abc89e5bf4fceb9a1b37f5801cb66cfc19
|
|
| MD5 |
74e9dc6721a7bf5232fddfde3431af58
|
|
| BLAKE2b-256 |
785710cd926323643a04cec6b6644344b8b21886c17417c34e6afd4ad4e06be3
|
Provenance
The following attestation bundles were made for parent_lookup-0.1.0-py3-none-any.whl:
Publisher:
publish_release.yml on ClaasRostock/parent-lookup
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
parent_lookup-0.1.0-py3-none-any.whl -
Subject digest:
c1d5441cca03b1b9624f3c2a4c1b18abc89e5bf4fceb9a1b37f5801cb66cfc19 - Sigstore transparency entry: 168292737
- Sigstore integration time:
-
Permalink:
ClaasRostock/parent-lookup@65b58a071dda2f1c433a1ef50e57889a2c48f1e4 -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/ClaasRostock
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_release.yml@65b58a071dda2f1c433a1ef50e57889a2c48f1e4 -
Trigger Event:
push
-
Statement type: