<img src="https://raw.githubusercontent.com/tobias-kirschstein/silberstral/master/img/logo.png" width="150px"> Silberstral
===========
>Reveal the true shape of type vars
[//]: # (For more badges visit https://shields.io/)
[![PyPI](https://img.shields.io/pypi/v/silberstral?color=blue)](https://pypi.org/project/silberstral/)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/silberstral)](https://pypi.org/project/silberstral/)
Python's typing system is weak and lacks features well known in other languages.
For example, templating in C++ allows you to instantiate a new object of the templated class `T` via `T(..)` which is not possible in Python.
The **Silberstral** package provides remedy with a simple utility to obtain the actual *type* that a generic type var refers to:
#### C++
```cpp
template<typename T>
class DefaultContainer {
T get(int idx) {
defaultElement = T(); // <- in C++, we can access the actual class of T
...
}
}
```
#### Python
```python
_T = TypeVar('_T')
class DefaultContainer(Generic[_T]):
def get(self, idx: int) -> _T:
default_element = _T() # <- DOES NOT WORK
...
```
#### Python + Silberstral
```python
from silberstral import reveal_type_var
_T = TypeVar('_T')
class DefaultList(Generic[_T]):
def get(self, idx: int) -> _T:
T_cls = reveal_type_var(self, _T) # <- Reveals the actual class of _T, e.g., int, str, ...
default_element = T_cls()
...
```
## Installation
The package is available at [pypi](https://pypi.org/project/silberstral/):
```commandline
pip install silberstral
```
## Usage
`reveal_type_var(obj_or_cls, type_var)`: Finds the actual type that `type_var` was instantiated to in `obj_or_cls`.
Example:
```python
from typing import TypeVar, Generic
from silberstral import reveal_type_var
_T = TypeVar('_T')
class List(Generic[_T]):
pass
reveal_type_var(List[int], _T)
>>> int
str_list = List[str]()
reveal_type_var(str_list, _T)
>>> str
```
---
`reveal_type_vars(obj_or_cls)`: Lists all type vars and their corresponding instantiations of `obj_or_cls`
Example:
```python
from typing import TypeVar, Generic
from silberstral import reveal_type_vars
_K = TypeVar('_K')
_V = TypeVar('_V')
class Dict(Generic[_K, _V]):
pass
reveal_type_vars(Dict[int, str])
>>> {_K: int, _V: str}
```
---
`is_type_var_instantiated(obj_or_cls, type_var)`: Checks whether `type_var` was instantiated with an actual class in `obj_or_cls`
Example:
```python
from typing import TypeVar, Generic
from silberstral import is_type_var_instantiated
_T = TypeVar('_T')
class List(Generic[_T]):
pass
is_type_var_instantiated(List, _T)
>>> False
is_type_var_instantiated(List[int], _T)
>>> True
```
Raw data
{
"_id": null,
"home_page": "https://github.com/tobias-kirschstein/silberstral",
"name": "silberstral",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "typing,type var,instantiation,generics",
"author": "Tobias Kirschstein",
"author_email": "tobias.kirschstein@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/e5/92/2d53d05f69e0178dee2a2a4bf7cf2a72a02475cf4c65727c1f099f09a260/silberstral-0.2.3.tar.gz",
"platform": null,
"description": "\n<img src=\"https://raw.githubusercontent.com/tobias-kirschstein/silberstral/master/img/logo.png\" width=\"150px\"> Silberstral\n===========\n>Reveal the true shape of type vars\n\n[//]: # (For more badges visit https://shields.io/)\n[![PyPI](https://img.shields.io/pypi/v/silberstral?color=blue)](https://pypi.org/project/silberstral/)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/silberstral)](https://pypi.org/project/silberstral/)\n\nPython's typing system is weak and lacks features well known in other languages. \nFor example, templating in C++ allows you to instantiate a new object of the templated class `T` via `T(..)` which is not possible in Python.\nThe **Silberstral** package provides remedy with a simple utility to obtain the actual *type* that a generic type var refers to:\n\n#### C++\n```cpp\ntemplate<typename T>\nclass DefaultContainer {\n \n T get(int idx) {\n defaultElement = T(); // <- in C++, we can access the actual class of T\n ...\n }\n \n}\n```\n#### Python\n```python\n_T = TypeVar('_T')\nclass DefaultContainer(Generic[_T]):\n \n def get(self, idx: int) -> _T:\n default_element = _T() # <- DOES NOT WORK\n ...\n```\n\n#### Python + Silberstral\n\n```python\nfrom silberstral import reveal_type_var\n\n_T = TypeVar('_T')\nclass DefaultList(Generic[_T]):\n \n def get(self, idx: int) -> _T:\n T_cls = reveal_type_var(self, _T) # <- Reveals the actual class of _T, e.g., int, str, ...\n default_element = T_cls()\n ...\n```\n\n## Installation\nThe package is available at [pypi](https://pypi.org/project/silberstral/):\n```commandline\npip install silberstral\n```\n\n## Usage\n`reveal_type_var(obj_or_cls, type_var)`: Finds the actual type that `type_var` was instantiated to in `obj_or_cls`.\n\nExample:\n```python\nfrom typing import TypeVar, Generic\nfrom silberstral import reveal_type_var\n\n_T = TypeVar('_T')\nclass List(Generic[_T]):\n pass\n\nreveal_type_var(List[int], _T)\n>>> int\n\nstr_list = List[str]()\nreveal_type_var(str_list, _T)\n>>> str\n```\n---\n`reveal_type_vars(obj_or_cls)`: Lists all type vars and their corresponding instantiations of `obj_or_cls`\n\nExample:\n```python\nfrom typing import TypeVar, Generic\nfrom silberstral import reveal_type_vars\n\n_K = TypeVar('_K')\n_V = TypeVar('_V')\nclass Dict(Generic[_K, _V]):\n pass\n\nreveal_type_vars(Dict[int, str])\n>>> {_K: int, _V: str}\n```\n---\n`is_type_var_instantiated(obj_or_cls, type_var)`: Checks whether `type_var` was instantiated with an actual class in `obj_or_cls`\n\nExample:\n\n```python\nfrom typing import TypeVar, Generic\nfrom silberstral import is_type_var_instantiated\n\n_T = TypeVar('_T')\nclass List(Generic[_T]):\n pass\n\nis_type_var_instantiated(List, _T)\n>>> False\n\nis_type_var_instantiated(List[int], _T)\n>>> True\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Reveals the true shape of type vars",
"version": "0.2.3",
"project_urls": {
"Homepage": "https://github.com/tobias-kirschstein/silberstral"
},
"split_keywords": [
"typing",
"type var",
"instantiation",
"generics"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "5deda3c1679a842e92b3d208d4c00e5dbf52064fa1d31f42dc68a2299bfbfe2a",
"md5": "177cd14880043517f0d6941e97ef28cb",
"sha256": "4301ba017339cddabe7996616b726dc483ab23fdd8af85595711db32f28e1c1d"
},
"downloads": -1,
"filename": "silberstral-0.2.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "177cd14880043517f0d6941e97ef28cb",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 8592,
"upload_time": "2023-07-04T15:18:11",
"upload_time_iso_8601": "2023-07-04T15:18:11.523681Z",
"url": "https://files.pythonhosted.org/packages/5d/ed/a3c1679a842e92b3d208d4c00e5dbf52064fa1d31f42dc68a2299bfbfe2a/silberstral-0.2.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e5922d53d05f69e0178dee2a2a4bf7cf2a72a02475cf4c65727c1f099f09a260",
"md5": "fbef6a1f2836d2f19dba758e0af7ec62",
"sha256": "271f8bd165877d7a3b2371278030da5337d430dc5523cfaa5c5e3676d8702a47"
},
"downloads": -1,
"filename": "silberstral-0.2.3.tar.gz",
"has_sig": false,
"md5_digest": "fbef6a1f2836d2f19dba758e0af7ec62",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 11818,
"upload_time": "2023-07-04T15:18:13",
"upload_time_iso_8601": "2023-07-04T15:18:13.144181Z",
"url": "https://files.pythonhosted.org/packages/e5/92/2d53d05f69e0178dee2a2a4bf7cf2a72a02475cf4c65727c1f099f09a260/silberstral-0.2.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-07-04 15:18:13",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "tobias-kirschstein",
"github_project": "silberstral",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "silberstral"
}