{"id":16854582,"url":"https://github.com/bswck/runtime_generics","last_synced_at":"2025-08-09T05:16:00.673Z","repository":{"id":304266757,"uuid":"709877764","full_name":"bswck/runtime_generics","owner":"bswck","description":"Reuse type arguments explicitly passed at runtime to a generic class before instantiating.","archived":true,"fork":false,"pushed_at":"2024-06-10T08:48:50.000Z","size":1355,"stargazers_count":1,"open_issues_count":2,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-07-12T03:18:29.242Z","etag":null,"topics":["generic-class","generic-types","generics","python-oop"],"latest_commit_sha":null,"homepage":"https://bswck.github.io/runtime_generics/","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/bswck.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"bswck"}},"created_at":"2023-10-25T15:15:07.000Z","updated_at":"2025-06-27T19:55:29.000Z","dependencies_parsed_at":"2025-07-12T03:18:38.268Z","dependency_job_id":"87add769-af70-47ef-8b66-5c198d0302fb","html_url":"https://github.com/bswck/runtime_generics","commit_stats":null,"previous_names":["bswck/runtime_generics"],"tags_count":23,"template":false,"template_full_name":null,"purl":"pkg:github/bswck/runtime_generics","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bswck%2Fruntime_generics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bswck%2Fruntime_generics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bswck%2Fruntime_generics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bswck%2Fruntime_generics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bswck","download_url":"https://codeload.github.com/bswck/runtime_generics/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bswck%2Fruntime_generics/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266802654,"owners_count":23986384,"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-07-24T02:00:09.469Z","response_time":99,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"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":["generic-class","generic-types","generics","python-oop"],"created_at":"2024-10-13T13:56:02.791Z","updated_at":"2025-07-24T06:34:23.268Z","avatar_url":"https://github.com/bswck.png","language":"Python","readme":"# \u003cdiv align=\"center\"\u003eruntime_generics\u003cbr\u003e[![skeleton](https://img.shields.io/badge/0.0.2rc–244–g52a2805-skeleton?label=%F0%9F%92%80%20skeleton-ci/skeleton-python\u0026labelColor=black\u0026color=grey\u0026link=https%3A//github.com/skeleton-ci/skeleton-python)](https://github.com/skeleton-ci/skeleton-python/tree/0.0.2rc-244-g52a2805) [![Supported Python versions](https://img.shields.io/pypi/pyversions/runtime-generics.svg?logo=python\u0026label=Python)](https://pypi.org/project/runtime-generics/) [![Package version](https://img.shields.io/pypi/v/runtime-generics?label=PyPI)](https://pypi.org/project/runtime-generics/)\u003c/div\u003e\n\n[![Tests](https://github.com/bswck/runtime_generics/actions/workflows/test.yml/badge.svg)](https://github.com/bswck/runtime_generics/actions/workflows/test.yml)\n[![Coverage](https://coverage-badge.samuelcolvin.workers.dev/bswck/runtime_generics.svg)](https://coverage-badge.samuelcolvin.workers.dev/redirect/bswck/runtime_generics)\n[![Lifted?](https://tidelift.com/badges/package/pypi/runtime-generics)](https://tidelift.com/subscription/pkg/pypi-runtime-generics?utm_source=pypi-runtime-generics\u0026utm_medium=readme)\n\nHighly into type-safe Python code?\n\n_runtime_generics_ is a niche Python library that allows you to reuse type arguments explicitly passed at runtime\nto generic classes before instantiation.\n\nThe library does four things:\n- exposes utilities that allow to inspect C3-linearized MROs of runtime generics\n  and type-check them with variance support: `get_mro()`, `type_check()`;\n- makes it possible to retrieve the type arguments passed to the generic class at runtime\n  before the class was instantiated: `get_type_arguments()`, `get_alias()`;\n- offers facilities to find how parent classes are parametrized (\n  e.g. if `Foo[T]` inherits from `Dict[str, T]`,\n  finds that `Dict[str, int]` is a parent for `Foo[int]`\n  ): `get_parents()`;\n- given a parametrized generic class (generic alias),\n  makes every class method use generic alias `cls` instead of the origin class\n  (unless decorated with `@no_alias`).\n\n# A Simple Example\n3.12+ ([PEP 695](https://peps.python.org/pep-0695) syntax):\n```python\nfrom __future__ import annotations\n\nimport io\nfrom typing import TYPE_CHECKING\n\nfrom runtime_generics import get_alias, get_type_arguments, runtime_generic, type_check\n\nif TYPE_CHECKING:\n    from typing import IO, Literal, overload\n\n\n@runtime_generic\nclass IOWrapper[T: str | bytes]:\n    data_type: type[T]\n\n    def __init__(self, stream: IO[T]) -\u003e None:\n        (self.data_type,) = get_type_arguments(self)\n        self.stream = stream\n\n    if TYPE_CHECKING:\n        @overload\n        def is_binary(self: IOWrapper[bytes]) -\u003e Literal[True]: ...\n\n        @overload\n        def is_binary(self: IOWrapper[str]) -\u003e Literal[False]: ...\n\n    def is_binary(self) -\u003e bool:\n        # alternatively here: `self.data_type == bytes`\n        return type_check(self, IOWrapper[bytes])\n\n    def __repr__(self) -\u003e str:\n        return f\"\u003c{get_alias(self)} object at ...\u003e\"\n\n\nmy_binary_data = IOWrapper[bytes](io.BytesIO(b\"foo\"))\nassert my_binary_data.data_type is bytes\nassert my_binary_data.is_binary()\nassert repr(IOWrapper[str](io.StringIO())) == \"\u003c__main__.IOWrapper[str] object at ...\u003e\"\n```\n\n3.8+:\n\n```python\nfrom __future__ import annotations\n\nimport io\nfrom typing import TYPE_CHECKING, Generic, TypeVar\n\nfrom runtime_generics import get_alias, get_type_arguments, runtime_generic, type_check\n\nif TYPE_CHECKING:\n    from typing import IO, Literal, overload\n\nT = TypeVar(\"T\", str, bytes)\n\n\n@runtime_generic\nclass IOWrapper(Generic[T]):\n    data_type: type[T]\n\n    def __init__(self, stream: IO[T]) -\u003e None:\n        (self.data_type,) = get_type_arguments(self)\n        self.stream = stream\n\n    if TYPE_CHECKING:\n        @overload\n        def is_binary(self: IOWrapper[bytes]) -\u003e Literal[True]: ...\n\n        @overload\n        def is_binary(self: IOWrapper[str]) -\u003e Literal[False]: ...\n\n    def is_binary(self) -\u003e bool:\n        # alternatively here: `self.data_type == bytes`\n        return type_check(self, IOWrapper[bytes])\n\n    def __repr__(self) -\u003e str:\n        return f\"\u003c{get_alias(self)} object at ...\u003e\"\n\n\nmy_binary_data = IOWrapper[bytes](io.BytesIO(b\"foo\"))\nassert my_binary_data.data_type is bytes\nassert my_binary_data.is_binary()\nassert repr(IOWrapper[str](io.StringIO())) == \"\u003c__main__.IOWrapper[str] object at ...\u003e\"\n```\n\n\n# For Enterprise\n\n| [![Tidelift](https://nedbatchelder.com/pix/Tidelift_Logo_small.png)](https://tidelift.com/subscription/pkg/pypi-runtime-generics?utm_source=pypi-runtime-genericsutm_medium=referral\u0026utm_campaign=readme) | [Available as part of the Tidelift Subscription.](https://tidelift.com/subscription/pkg/pypi-runtime-generics?utm_source=pypi-runtime-generics\u0026\u0026utm_medium=referral\u0026utm_campaign=readme)\u003cbr\u003eThis project and the maintainers of thousands of other packages are working with Tidelift to deliver one enterprise subscription that covers all of the open source you use. [Learn more here](https://tidelift.com/subscription/pkg/pypi-runtime-generics?utm_source=pypi-runtime-generics\u0026utm_medium=referral\u0026utm_campaign=github). |\n| - | - |\n\nTo report a security vulnerability, please use the\n[Tidelift security contact](https://tidelift.com/security).\u003cbr\u003e\nTidelift will coordinate the fix and disclosure.\n\n# Installation\nYou might simply install it with pip:\n\n```shell\npip install runtime-generics\n```\n\nIf you use [Poetry](https://python-poetry.org/), then you might want to run:\n\n```shell\npoetry add runtime-generics\n```\n\n## For Contributors\n[![Poetry](https://img.shields.io/endpoint?url=https://python-poetry.org/badge/v0.json)](https://python-poetry.org/)\n[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)\n[![Pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit\u0026logoColor=white)](https://github.com/pre-commit/pre-commit)\n\u003c!--\nThis section was generated from skeleton-ci/skeleton-python@0.0.2rc-244-g52a2805.\nInstead of changing this particular file, you might want to alter the template:\nhttps://github.com/skeleton-ci/skeleton-python/tree/0.0.2rc-244-g52a2805/project/README.md.jinja\n--\u003e\n\u003e [!Note]\n\u003e If you use Windows, it is highly recommended to complete the installation in the way presented below through [WSL2](https://learn.microsoft.com/en-us/windows/wsl/install).\n1.  Fork the [runtime_generics repository](https://github.com/bswck/runtime_generics) on GitHub.\n\n1.  [Install Poetry](https://python-poetry.org/docs/#installation).\u003cbr/\u003e\n    Poetry is an amazing tool for managing dependencies \u0026 virtual environments, building packages and publishing them.\n    You might use [pipx](https://github.com/pypa/pipx#readme) to install it globally (recommended):\n\n    ```shell\n    pipx install poetry\n    ```\n\n    \u003csub\u003eIf you encounter any problems, refer to [the official documentation](https://python-poetry.org/docs/#installation) for the most up-to-date installation instructions.\u003c/sub\u003e\n\n    Be sure to have Python 3.8 installed—if you use [pyenv](https://github.com/pyenv/pyenv#readme), simply run:\n\n    ```shell\n    pyenv install 3.8\n    ```\n\n1.  Clone your fork locally and install dependencies.\n\n    ```shell\n    git clone https://github.com/your-username/runtime_generics path/to/runtime_generics\n    cd path/to/runtime_generics\n    poetry env use $(cat .python-version)\n    poetry install\n    ```\n\n    Next up, simply activate the virtual environment and install pre-commit hooks:\n\n    ```shell\n    poetry shell\n    pre-commit install\n    ```\n\nFor more information on how to contribute, check out [CONTRIBUTING.md](https://github.com/bswck/runtime_generics/blob/HEAD/CONTRIBUTING.md).\u003cbr/\u003e\nAlways happy to accept contributions! ❤️\n\n# Legal Info\n© Copyright by Bartosz Sławecki ([@bswck](https://github.com/bswck)).\n\u003cbr /\u003eThis software is licensed under the terms of [MIT License](https://github.com/bswck/runtime_generics/blob/HEAD/LICENSE).\n","funding_links":["https://github.com/sponsors/bswck","https://tidelift.com/badges/package/pypi/runtime-generics","https://tidelift.com/subscription/pkg/pypi-runtime-generics?utm_source=pypi-runtime-generics\u0026utm_medium=readme","https://tidelift.com/subscription/pkg/pypi-runtime-generics?utm_source=pypi-runtime-genericsutm_medium=referral\u0026utm_campaign=readme","https://tidelift.com/subscription/pkg/pypi-runtime-generics?utm_source=pypi-runtime-generics\u0026\u0026utm_medium=referral\u0026utm_campaign=readme","https://tidelift.com/subscription/pkg/pypi-runtime-generics?utm_source=pypi-runtime-generics\u0026utm_medium=referral\u0026utm_campaign=github","https://tidelift.com/security"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbswck%2Fruntime_generics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbswck%2Fruntime_generics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbswck%2Fruntime_generics/lists"}