{"id":19775551,"url":"https://github.com/rgryta/cyclic-classes","last_synced_at":"2025-09-12T04:42:24.420Z","repository":{"id":229862346,"uuid":"777265447","full_name":"rgryta/Cyclic-Classes","owner":"rgryta","description":"Enable relations between different classes across modules, skipping issues with cyclic imports","archived":false,"fork":false,"pushed_at":"2024-05-13T10:10:12.000Z","size":36,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-01T20:53:07.557Z","etag":null,"topics":["caching","classes-and-inheritance","classes-python","cyclic","import","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rgryta.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-03-25T14:23:32.000Z","updated_at":"2024-05-13T10:08:41.000Z","dependencies_parsed_at":"2024-03-26T17:26:37.202Z","dependency_job_id":"164aba04-3806-4bfb-ac83-7dc45dabffe3","html_url":"https://github.com/rgryta/Cyclic-Classes","commit_stats":null,"previous_names":["rgryta/cyclic-classes"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/rgryta/Cyclic-Classes","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rgryta%2FCyclic-Classes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rgryta%2FCyclic-Classes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rgryta%2FCyclic-Classes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rgryta%2FCyclic-Classes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rgryta","download_url":"https://codeload.github.com/rgryta/Cyclic-Classes/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rgryta%2FCyclic-Classes/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274754615,"owners_count":25343056,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-12T02:00:09.324Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["caching","classes-and-inheritance","classes-python","cyclic","import","python"],"created_at":"2024-11-12T05:16:47.984Z","updated_at":"2025-09-12T04:42:24.372Z","avatar_url":"https://github.com/rgryta.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\u003c/p\u003e\n\u003ch2 align=\"center\"\u003eCyclic Classes\u003c/h2\u003e\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://github.com/rgryta/Cyclic-Classes/actions/workflows/main.yml\"\u003e\u003cimg alt=\"Python package\" src=\"https://github.com/rgryta/Cyclic-Classes/actions/workflows/main.yml/badge.svg?branch=main\"\u003e\u003c/a\u003e\n\u003ca href=\"https://pypi.org/project/cyclic-classes/\"\u003e\u003cimg alt=\"PyPI\" src=\"https://img.shields.io/pypi/v/cyclic-classes\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/psf/black\"\u003e\u003cimg alt=\"Code style: black\" src=\"https://img.shields.io/badge/code%20style-black-000000.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/PyCQA/pylint\"\u003e\u003cimg alt=\"pylint\" src=\"https://img.shields.io/badge/linting-pylint-yellowgreen\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/rgryta/NoPrint\"\u003e\u003cimg alt=\"NoPrint\" src=\"https://img.shields.io/badge/NoPrint-enabled-blueviolet\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n## About\n\nEver had a situation when you wanted to create a cyclic reference between two objects, but don't want to maintain a large singular file?\nYou're tired of seeing \"cyclic-import\" errors? This package is for you!\nSimply \"register\" a class you want to share between different modules and you're pretty much good to go!\n\nSee the [documentation](https://github.com/rgryta/Cyclic-Classes/#Usage) for more information.\n\n## Requirements\n\nThere's ***NONE!***\n\n## Installation\n\nPull straight from this repo to install manually or just use pip: `pip install cyclic-classes` will do the trick.\n\n## Usage\n\nConsider a package `myk8s` with two modules, maybe one is to reference a k8s deployment and the other one is for pods within that deployment.\n\n\n`deployment.py`\n```python\nfrom .pod import Pod\n\nclass Deployment:\n    def __init__(self, name: str):\n        self.name = name\n    \n    @property\n    def pods(self):\n        return [Pod(f\"{self.name}-pod-{i}\") for i in range(3)]\n```\n\nNow, if you want to create a back-reference for Deployment within Pod, like this:\n`pod.py`\n```python\nfrom .deployment import Deployment\n\nclass Pod:\n    def __init__(self, name: str):\n        self.name = name\n        \n    @property\n    def deployment(self):\n        return Deployment(self.name.split(\"-\")[0])\n```\n\nIn the above example, you would get a cyclic-import error. To avoid this, you can use `cyclic_classes` to register the classes you want to share between modules.\nDo so like this:\n\n`deployment.py`\n```python\nfrom cyclic_classes import register, cyclic_import\n\nwith cyclic_import():  # This is required to avoid cyclic-import errors - you're actually importing a registered class underneath, but IDE will think it's your actual class\n    from .pod import Pod\n\n@register  # You have to register the class you want to access so that Pod will also be able to use it in the pod.py file\nclass Deployment:\n    def __init__(self, name: str):\n        self.name = name\n    \n    @property\n    def pods(self):\n        return [Pod(f\"{self.name}-pod-{i}\") for i in range(3)]\n```\n\nNow, if you want to create a back-reference for Deployment within Pod, like this:\n`pod.py`\n```python\nfrom cyclic_classes import register, cyclic_import\n\nwith cyclic_import():\n    from .deployment import Deployment\n\n@register  # Making Pod available to cyclic_import\nclass Pod:\n    def __init__(self, name: str):\n        self.name = name\n        \n    @property\n    def deployment(self):\n        return Deployment(self.name.split(\"-\")[0])\n```\n\nAnd that's it! You can now use the classes in your code without any issues.\n\n### Note\n\nCyclic import supports multiple ways of imports, including relative imports, absolute imports, and even module imports.\nAs such, in the `cyclic_import` context you can use either of the following:\n\n```python\nimport myk8s.deployment as depl  # And later use depl.Deployment\nfrom .deployment import Deployment\nfrom myk8s.deployment import Deployment\n\n# Not recommended, but also possible\n# Reason: Such imports wouldn't work even if there was no cyclic import issue, but it works with cyclic import (you'll get a warning though) and some IDEs think it's correct \nfrom deployment import Deployment\nimport deployment\n```\n\nThis also works with aliases! So feel free to use `import ... as ...` or `from ... import ... as ...` as you wish.\n\n## Development\n\n### Installation\n\nInstall virtual environment and cyclic_classes package in editable mode with dev dependencies.\n\n```bash\npython -m venv venv\nsource venv/bin/activate\npip install -e .[dev]\n```\n\n### How to?\n\nAutomate as much as we can, see configuration in `pyproject.toml` file to see what are the flags used.\n\n```bash\nstaging format  # Reformat the code\nstaging lint    # Check for linting issues\nstaging test    # Run unit tests and coverage report\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frgryta%2Fcyclic-classes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frgryta%2Fcyclic-classes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frgryta%2Fcyclic-classes/lists"}