python-generics


Namepython-generics JSON
Version 0.0.2 PyPI version JSON
download
home_page
SummaryA package to determine the values of generic classes through instances or subclasses
upload_time2023-09-18 06:40:56
maintainer
docs_urlNone
author
requires_python>=3.11
licenseMIT
keywords generics python
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Python-Generics

![Unittests status badge](https://github.com/Hochfrequenz/python-generics/workflows/Unittests/badge.svg)
![Coverage status badge](https://github.com/Hochfrequenz/python-generics/workflows/Coverage/badge.svg)
![Linting status badge](https://github.com/Hochfrequenz/python-generics/workflows/Linting/badge.svg)
![Black status badge](https://github.com/Hochfrequenz/python-generics/workflows/Formatting/badge.svg)

This package provides functionalities to determine the values of generic type variables in Python.
As of now, it only supports two functions: `get_type_vars` and `get_filled_type`. These functions work also
with pydantic generic models (only tested with pydantic > v2.3.0).

### Installation
The package is [available on PyPI](https://pypi.org/project/python-generics/):
```bash
pip install python-generics
```

### Usage

The `get_type_vars` function returns a tuple of all type variables for a given generic type. The `TypeVar`s are
determined by `Generic` if the type is a subclass of `Generic`. Otherwise, they are determined by the indexed
supertypes (the order of the returned tuple is the lexicographical in the list of the supertypes).

```python
from typing import Generic, TypeVar
from generics import get_type_vars

T = TypeVar("T")
U = TypeVar("U")
V = TypeVar("V")

class A(Generic[T, U]):
    pass

class B(A[T, U], Generic[U, T]):
    pass

class C(B[T, U], A[T, V]):
    pass

assert get_type_vars(A) == (T, U)
assert get_type_vars(B) == (U, T)
assert get_type_vars(C) == (T, U, V)
```

The `get_filled_type` function determines for a single `TypeVar` the value if defined somewhere.
To determine the value, you have to pass a type or an instance of a type that is a subclass of a generic type
of which you want to determine the value of the `TypeVar`.

Instead of supplying the `TypeVar` itself, you can define the integer position of the `TypeVar` in the tuple of
`TypeVar`s of the generic type.

```python
from typing import Generic, TypeVar
from generics import get_filled_type

T = TypeVar("T")
U = TypeVar("U")
V = TypeVar("V")

class A(Generic[T, U]):
    pass

class B(A[str, U]):
    pass

assert get_filled_type(A[str, U], A, T) == str
assert get_filled_type(B[int](), A, 0) == str
```

The `get_filled_type` function is especially useful if you have generic super types in which you want to determine
the value of a `TypeVar` inside methods.

```python
from typing import Generic, TypeVar, Any
from generics import get_filled_type

T = TypeVar("T")

class MySuperType(Generic[T]):
    def get_type(self) -> Any:
        return get_filled_type(self, MySuperType, 0)

class MySubType(MySuperType[str]):
    pass

assert MySubType().get_type() == str
```

## How to use this Repository on Your Machine

Follow the instructions in our [Python template repository](https://github.com/Hochfrequenz/python_template_repository#how-to-use-this-repository-on-your-machine).

## Contribute

You are very welcome to contribute to this template repository by opening a pull request against the main branch.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "python-generics",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": "",
    "keywords": "generics,python",
    "author": "",
    "author_email": "Hochfrequenz Unternehmensberatung GmbH <info@hochfrequenz.de>",
    "download_url": "https://files.pythonhosted.org/packages/0b/77/d8d825c85d71d7507096d308866c14a6a4f71559a301a397bcf25fc6743d/python_generics-0.0.2.tar.gz",
    "platform": null,
    "description": "# Python-Generics\n\n![Unittests status badge](https://github.com/Hochfrequenz/python-generics/workflows/Unittests/badge.svg)\n![Coverage status badge](https://github.com/Hochfrequenz/python-generics/workflows/Coverage/badge.svg)\n![Linting status badge](https://github.com/Hochfrequenz/python-generics/workflows/Linting/badge.svg)\n![Black status badge](https://github.com/Hochfrequenz/python-generics/workflows/Formatting/badge.svg)\n\nThis package provides functionalities to determine the values of generic type variables in Python.\nAs of now, it only supports two functions: `get_type_vars` and `get_filled_type`. These functions work also\nwith pydantic generic models (only tested with pydantic > v2.3.0).\n\n### Installation\nThe package is [available on PyPI](https://pypi.org/project/python-generics/):\n```bash\npip install python-generics\n```\n\n### Usage\n\nThe `get_type_vars` function returns a tuple of all type variables for a given generic type. The `TypeVar`s are\ndetermined by `Generic` if the type is a subclass of `Generic`. Otherwise, they are determined by the indexed\nsupertypes (the order of the returned tuple is the lexicographical in the list of the supertypes).\n\n```python\nfrom typing import Generic, TypeVar\nfrom generics import get_type_vars\n\nT = TypeVar(\"T\")\nU = TypeVar(\"U\")\nV = TypeVar(\"V\")\n\nclass A(Generic[T, U]):\n    pass\n\nclass B(A[T, U], Generic[U, T]):\n    pass\n\nclass C(B[T, U], A[T, V]):\n    pass\n\nassert get_type_vars(A) == (T, U)\nassert get_type_vars(B) == (U, T)\nassert get_type_vars(C) == (T, U, V)\n```\n\nThe `get_filled_type` function determines for a single `TypeVar` the value if defined somewhere.\nTo determine the value, you have to pass a type or an instance of a type that is a subclass of a generic type\nof which you want to determine the value of the `TypeVar`.\n\nInstead of supplying the `TypeVar` itself, you can define the integer position of the `TypeVar` in the tuple of\n`TypeVar`s of the generic type.\n\n```python\nfrom typing import Generic, TypeVar\nfrom generics import get_filled_type\n\nT = TypeVar(\"T\")\nU = TypeVar(\"U\")\nV = TypeVar(\"V\")\n\nclass A(Generic[T, U]):\n    pass\n\nclass B(A[str, U]):\n    pass\n\nassert get_filled_type(A[str, U], A, T) == str\nassert get_filled_type(B[int](), A, 0) == str\n```\n\nThe `get_filled_type` function is especially useful if you have generic super types in which you want to determine\nthe value of a `TypeVar` inside methods.\n\n```python\nfrom typing import Generic, TypeVar, Any\nfrom generics import get_filled_type\n\nT = TypeVar(\"T\")\n\nclass MySuperType(Generic[T]):\n    def get_type(self) -> Any:\n        return get_filled_type(self, MySuperType, 0)\n\nclass MySubType(MySuperType[str]):\n    pass\n\nassert MySubType().get_type() == str\n```\n\n## How to use this Repository on Your Machine\n\nFollow the instructions in our [Python template repository](https://github.com/Hochfrequenz/python_template_repository#how-to-use-this-repository-on-your-machine).\n\n## Contribute\n\nYou are very welcome to contribute to this template repository by opening a pull request against the main branch.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A package to determine the values of generic classes through instances or subclasses",
    "version": "0.0.2",
    "project_urls": {
        "Changelog": "https://github.com/Hochfrequenz/python-generics/releases",
        "Homepage": "https://github.com/Hochfrequenz/python-generics"
    },
    "split_keywords": [
        "generics",
        "python"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e56d36b7cd9adfea77e6f1e52734508c67dc4c3032ea036c37aeb0b0627cdb0f",
                "md5": "7575e5e53d4f081049790b70d9bb0975",
                "sha256": "8db8dea7a9c795424225c2e42cf9776ce038e841918b04a7988d284aa6910656"
            },
            "downloads": -1,
            "filename": "python_generics-0.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7575e5e53d4f081049790b70d9bb0975",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 5618,
            "upload_time": "2023-09-18T06:40:54",
            "upload_time_iso_8601": "2023-09-18T06:40:54.435477Z",
            "url": "https://files.pythonhosted.org/packages/e5/6d/36b7cd9adfea77e6f1e52734508c67dc4c3032ea036c37aeb0b0627cdb0f/python_generics-0.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0b77d8d825c85d71d7507096d308866c14a6a4f71559a301a397bcf25fc6743d",
                "md5": "b6e5ba07b684422f441eeae66ddadf7d",
                "sha256": "253572e9f19d512c654219acca56f5bebee7872d1b5e341c8e032d3360d9f770"
            },
            "downloads": -1,
            "filename": "python_generics-0.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "b6e5ba07b684422f441eeae66ddadf7d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 11519,
            "upload_time": "2023-09-18T06:40:56",
            "upload_time_iso_8601": "2023-09-18T06:40:56.601071Z",
            "url": "https://files.pythonhosted.org/packages/0b/77/d8d825c85d71d7507096d308866c14a6a4f71559a301a397bcf25fc6743d/python_generics-0.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-18 06:40:56",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Hochfrequenz",
    "github_project": "python-generics",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "tox": true,
    "lcname": "python-generics"
}
        
Elapsed time: 0.11937s