Reveals the true shape of type vars
Project description
Silberstral
Reveal the true shape of type vars
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++
template<typename T>
class DefaultContainer {
T get(int idx) {
defaultElement = T(); // <- in C++, we can access the actual class of T
...
}
}
Python
_T = TypeVar('_T')
class DefaultContainer(Generic[_T]):
def get(self, idx: int) -> _T:
default_element = _T() # <- DOES NOT WORK
...
Python + Silberstral
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:
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:
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:
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:
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
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 silberstral-0.2.3.tar.gz.
File metadata
- Download URL: silberstral-0.2.3.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
271f8bd165877d7a3b2371278030da5337d430dc5523cfaa5c5e3676d8702a47
|
|
| MD5 |
fbef6a1f2836d2f19dba758e0af7ec62
|
|
| BLAKE2b-256 |
e5922d53d05f69e0178dee2a2a4bf7cf2a72a02475cf4c65727c1f099f09a260
|
File details
Details for the file silberstral-0.2.3-py3-none-any.whl.
File metadata
- Download URL: silberstral-0.2.3-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4301ba017339cddabe7996616b726dc483ab23fdd8af85595711db32f28e1c1d
|
|
| MD5 |
177cd14880043517f0d6941e97ef28cb
|
|
| BLAKE2b-256 |
5deda3c1679a842e92b3d208d4c00e5dbf52064fa1d31f42dc68a2299bfbfe2a
|