Python Strict Typing Library
This is a library written in pure Python that enables runtime type-checking for classes and functions via the use of a single @strictly_typed decorator. Classes may enforce strict typing of their members and functions are able to have type-checked parameters and return values via type hints.
Installation
This library is available via PyPI. It may be installed with the following pip command:
pip install py-strict-typing
Usage
With Classes
Any class may declare itself as having strictly-typed members using the @strictly_typed decorator:
from strict_typing import *
@strictly_typed
class example_class:
a: int
b: int
print(example_class.a) # None
Upon assignment, any member of an instance of the class that provides a type hint will be type checked:
example_inst = example_class()
example_inst.a = 123
print(example_inst.a) # 123
example_inst.a = 'hello' # results in TypeError
Any members that do not have a type hint will not be checked. Also note that static assignments at the time of class declaration cannot be checked.
from strict_typing import *
@strictly_typed
class example_class:
a = 'hello'
b: list = 123 # valid. static assignment at declaration is not checked
inst = example_class()
inst.a = 123 # valid. 'a' has no hint so it is not checked
With Functions
Any Python function may declare its parameters and return type as strictly-typed using the @strictly_typed decorator:
from strict_typing import *
@strictly_typed
def test(a: int, b: str) -> str:
return str(a) + b
print(test(123, 'hello')) # returns '123hello'
print(test(10.0, 'invalid')) # results in TypeError
As with strictly-typed classes, any parameters that lack a hint will not be checked.
from strict_typing import *
@strictly_typed
def test(a, b: str) -> str:
return str(a) + b
print(test(10.0, 'not invalid')) # returns '10.0not invalid' as 'a' is no longer checked.
This rule also applies to the return type hint:
from strict_typing import *
@strictly_typed
def test(a: int):
return a + 123
@strictly_typed
def test2(a: int) -> str:
return a + 123
print(test(111)) # outputs '234'
print(test2(111)) # results in TypeError
Metadata
Release files for py-strict-typing 0.1.1
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 |
|---|---|---|---|---|
| py-strict-typing-0.1.1.tar.gz | 4.1 kB | Without provenance | Details for py-strict-typing-0.1.1.tar.gz |
Release files / py-strict-typing-0.1.1.tar.gz
| Download URL | py-strict-typing-0.1.1.tar.gz |
|---|---|
| Size | 4.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0da5afb9431eed172f183c66ac8a946ab44690591aea77005e5cc70b3114e5d2
|
|
BLAKE2b-256 checksum How to use checksums |
f56b3f640879bf0e0fbe89c1889269270b8d53bfee86e63e61b352a70b6fd9ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
|
Security for py-strict-typing 0.1.1
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.