Skip to main content

Silberstral

Reveal the true shape of type vars

PyPI PyPI - Python Version

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)

Source distribution for silberstral 0.2.3
File Size Uploaded
silberstral-0.2.3.tar.gz 11.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for silberstral 0.2.3
File Interpreter ABI Platform
silberstral-0.2.3-py3-none-any.whl Python 3 none any Details

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

Release history Release notifications | RSS feed

This release

0.2.3 This release

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page