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
Metadata
Release files for silberstral 0.2.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| silberstral-0.2.3.tar.gz | 11.8 kB | Without provenance | Details for silberstral-0.2.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| silberstral-0.2.3-py3-none-any.whl | Python 3 | none | any | Details for silberstral-0.2.3-py3-none-any.whl |
Total release size: 20.4 kB
Release files / silberstral-0.2.3.tar.gz
| Download URL | silberstral-0.2.3.tar.gz |
|---|---|
| Size | 11.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
271f8bd165877d7a3b2371278030da5337d430dc5523cfaa5c5e3676d8702a47
|
|
BLAKE2b-256 checksum How to use checksums |
e5922d53d05f69e0178dee2a2a4bf7cf2a72a02475cf4c65727c1f099f09a260
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.0
|
Release files / silberstral-0.2.3-py3-none-any.whl
| Download URL | silberstral-0.2.3-py3-none-any.whl |
|---|---|
| Size | 8.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4301ba017339cddabe7996616b726dc483ab23fdd8af85595711db32f28e1c1d
|
|
BLAKE2b-256 checksum How to use checksums |
5deda3c1679a842e92b3d208d4c00e5dbf52064fa1d31f42dc68a2299bfbfe2a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.0
|
Security for silberstral 0.2.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.